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
38
39
40
41
42
43
44
# File 'lib/remap/constructor/keyword.rb', line 29

def call(state)
  super.fmap do |input, &error|
    unless input.is_a?(Hash)
      return error["Input is not a hash"]
    end

    target.public_send(id, **input)
  rescue ArgumentError => e
    raise e.exception("Failed to create [%p] with input [%s] (%s}) using method %s" % [
      target,
      input,
      input.class,
      id
    ])
  end
end

#strategy:keyword

Returns:

  • (:keyword)


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

attribute :strategy, Value(:keyword)