Class: Remap::Mapper::Or

Inherits:
Binary show all
Defined in:
lib/remap/mapper/or.rb

Overview

Represents two mappers that are combined with the | operator

Examples:

Combine two mappers

class Mapper1 < Remap::Base
  contract do
    required(:a1)
  end
end

class Mapper2 < Remap::Base
  contract do
    required(:a2)
  end
end

state = Remap::State.call({ a2: 2 })
result = (Mapper1 | Mapper2).call!(state)
result.fetch(:value) # => { a2: 2 }

Instance Method Summary collapse

Methods inherited from Binary

#validate?

Methods included from API

#call, #validate?

Methods included from Operations

#&, #^, #|

Instance Method Details

#call!(state) {|if| ... } ⇒ Result

Succeeds if left or right succeeds Returns which ever succeeds first

Parameters:

Yield Parameters:

Yield Returns:

Returns:

  • (Result)


35
36
37
38
39
40
41
# File 'lib/remap/mapper/or.rb', line 35

def call!(state, &error)
  left.call!(state) do |failure1|
    return right.call!(state) do |failure2|
      return error[failure1.merge(failure2)]
    end
  end
end

#inspectString Also known as: to_s

Returns:

  • (String)


44
45
46
# File 'lib/remap/mapper/or.rb', line 44

def inspect
  "%s | %s" % [left, right]
end