Skip to content

Type coercer can take second argument for the initialized instance

Compare
Choose a tag to compare
@nepalez nepalez released this 19 Sep 08:16

This allows to wrap assigned value to the object that refers back
to the initializer instance. More verbose example:

class Location < String
  attr_reader :parameter # refers back to its parameter

  def initialize(name, parameter)
    super(name)
    @parameter = parameter
  end
end

class Parameter
  extend Dry::Initializer
  param :name
  param :location, ->(value, param) { Location.new(value, param) }
end

offset = Parameter.new "offset", location: "query"
offset.name     # => "offset"
offset.location # => "query"
offset.location.parameter == offset # true