Class: RuleTable::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/rule_table.rb

Instance Method Summary collapse

Constructor Details

#initializeTable

Returns a new instance of Table.



19
20
21
# File 'lib/rule_table.rb', line 19

def initialize
  @rules = []
end

Instance Method Details

#add_rule_for(target, *matchers) ⇒ Object



23
24
25
# File 'lib/rule_table.rb', line 23

def add_rule_for(target, *matchers)
  @rules << [ target, matchers ]
end

#match(object) ⇒ Object



27
28
29
30
31
# File 'lib/rule_table.rb', line 27

def match(object)
  @rules.find { |(_target, matchers)|
    matchers.all? { |m| m.matches?(object) }
  }.first
end

#match_with_trace(object) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rule_table.rb', line 33

def match_with_trace(object)
  trace = []
  result = @rules.find { |(target, matchers)|
    partial_trace = { target: target, matched: [] }
    matchers.all? { |m|
      m.matches?(object).tap { |match_result|
        partial_trace[:matched] << m.matcher_name if match_result
      }
    }.tap {
      trace << partial_trace
    }
  }.first
  [ result, trace ]
end