Class: Remap::Constructor::Keyword

Inherits:
Concrete
  • Object
show all
Defined in:
lib/remap/constructor/keyword.rb

Overview

Allows a class (target) to be called with keyword arguments

Instance Method Summary collapse

Instance Method Details

#call(state) ⇒ State

Calls Remap::Constructor#target as with keyword arguments

Fails if Remap::Constructor#target does not respond to Remap::Constructor#method Fails if Remap::Constructor#target cannot be called with state

Used by Base to define constructors for mapped data

Examples:

Initialize a target with a state

target = OpenStruct
constructor = Remap::Constructor.call(strategy: :keyword, target: target, method: :new)
state = Remap::State.call({ foo: :bar })
new_state = constructor.call(state)
new_state.fetch(:value).foo # => :bar

Parameters:

Returns:



29
30
31
32
33
34
35
36
37
# File 'lib/remap/constructor/keyword.rb', line 29

def call(state)
  super.fmap do |input|
    unless input.is_a?(Hash)
      raise ArgumentError, "Expected Hash, got #{input.class}"
    end

    target.public_send(id, **input)
  end
end

#strategy:keyword

Returns:

  • (:keyword)


10
# File 'lib/remap/constructor/keyword.rb', line 10

attribute :strategy, Value(:keyword)