Class: Remap::Rule::Embed

Inherits:
Unit
  • Object
show all
Defined in:
lib/remap/rule/embed.rb

Overview

Embed mappers into each other

Examples:

Embed Mapper A into B

class Car < Remap::Base
  define do
    map :name, to: :model
  end
end

class Person < Remap::Base
  define do
    to :person do
      to :car do
        embed Car
      end
    end
  end
end

Person.call({name: "Volvo"}) # => { person: { car: { model: "Volvo" } } }

Instance Method Summary collapse

Instance Method Details

#call(state, &error) ⇒ State<U>

Evaluates input against mapper and returns the result

Parameters:

Returns:



36
37
38
39
40
41
42
43
44
# File 'lib/remap/rule/embed.rb', line 36

def call(state, &error)
  unless error
    raise ArgumentError, "A block is required to evaluate the embed"
  end

  mapper.call!(state.set(mapper: mapper)) do |failure|
    return error[failure]
  end.except(:mapper, :scope)
end

#mapper#call!

Returns:



29
# File 'lib/remap/rule/embed.rb', line 29

attribute :mapper, Types::Mapper