Class: Funkr::Matchers::SafeMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/funkr/adt/matcher.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(match) ⇒ SafeMatcher

Returns a new instance of SafeMatcher.



8
9
10
11
12
# File 'lib/funkr/adt/matcher.rb', line 8

def initialize(match)
  @const, *@data = match
  @runner = nil
  @undefined = self.class.constructs.clone
end

Class Method Details

.build_matchers(constructs) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/funkr/adt/matcher.rb', line 14

def self.build_matchers(constructs)
  @constructs = constructs
  constructs.each do |c|
    name, *data = c
    define_method(name) do |&b|
      @undefined.delete(name)
      if @const == name then
        @runner = b
      end
    end
  end
end

.constructsObject



27
# File 'lib/funkr/adt/matcher.rb', line 27

def self.constructs; @constructs; end

.match_with(normal_form) {|m| ... } ⇒ Object

&block

Yields:

  • (m)


29
30
31
32
33
# File 'lib/funkr/adt/matcher.rb', line 29

def self.match_with(normal_form) # &block
  m = self.new(normal_form)
  yield m
  m.run_match
end

Instance Method Details

#run_matchObject



35
36
37
38
39
40
# File 'lib/funkr/adt/matcher.rb', line 35

def run_match
  if @undefined.any? then
    raise "Incomplete match, missing : #{@undefined.join(" ")}"
  end
  @runner.call(*@data)
end