Class: Remap::Constructor::Argument

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

Overview

Allows a class (target) to be called with a regular argument

Instance Method Summary collapse

Instance Method Details

#call(state) ⇒ State

Uses the Remap::Constructor#method method to initialize Remap::Constructor#target with state Target is only called if state is defined

Used by Base to define constructors for mapped data

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

Examples:

Initialize a target with a state

target = ::Struct.new(:foo)
constructor = Remap::Constructor.call(strategy: :argument, target: target, method: :new)
state = Remap::State.call(:bar)
new_state = constructor.call(state)
new_state.fetch(:value).foo # => :bar

Parameters:

Returns:



30
31
32
33
34
35
36
37
38
39
# File 'lib/remap/constructor/argument.rb', line 30

def call(state)
  super.fmap do |input|
    target.public_send(id, input)
  rescue ArgumentError => e
    raise e.exception("Failed to create [%p] with input [%s] (%s)" % [
      target, input,
      input.class
    ])
  end
end

#strategy:argument

Returns:

  • (:argument)


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

attribute :strategy, Value(:argument), default: :argument