Class: Farseer::Or

Inherits:
Object
  • Object
show all
Includes:
MapFactory
Defined in:
lib/farseer/or.rb

Instance Method Summary collapse

Methods included from MapFactory

#map

Constructor Details

#initialize(*parsers) ⇒ Or

Returns a new instance of Or.



7
8
9
10
# File 'lib/farseer/or.rb', line 7

def initialize(*parsers)
  @parsers = parsers.flatten
  freeze
end

Instance Method Details

#parse(input) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/farseer/or.rb', line 12

def parse(input)
  case @parsers.length
  when 0 then Maybe.none
  when 1 then @parsers.first.parse(input)
  else parse_helper(input)
  end
end

#parse_helper(input) ⇒ Object



20
21
22
23
24
# File 'lib/farseer/or.rb', line 20

def parse_helper(input)
  @parsers.reduce do |acc, parser|
    acc.parse(input).bind_none { parser.parse(input) }
  end
end