literal icon indicating copy to clipboard operation
literal copied to clipboard

feat: Make data structs implicitly Hash coercible

Open ixti opened this issue 7 months ago • 0 comments

This allows using Literal::Data (and its friends) like so:

class Person < Literal::Data
  prop :first_name, String
  prop :last_name,  String
end

john = Person.new(first_name: "John", last_name: "Doe")
jane = Person.new(**john, first_name: "Jane")

This is especially useful when we use Data/Struct as configuration options data:

class Shrine
  module Plugins
    module Example
      class Options < Literal::Data
        # ...
      end

      DEFAULT_OPTIONS = { ... }

      def self.configure(uploader, **options)
        uploader.opts[:example] = Options.new(
          **DEFAULT_OPTIONS,
          **uploader.opts[:example],
          **options
        )
      end
    end

    register_plugin(:example, Example)
  end
end

ixti avatar May 01 '25 00:05 ixti