Class: Rudelo::Matchers::SetLogic
- Inherits:
-
Rufus::Decision::Matcher
- Object
- Rufus::Decision::Matcher
- Rudelo::Matchers::SetLogic
- Defined in:
- lib/rudelo/matchers/set_logic.rb
Constant Summary collapse
- SYNTAX_EXPR =
%r{^\$\([^)]*\)|\$in|#[><=]}
Instance Attribute Summary collapse
-
#force ⇒ Object
if true, will raise if a cell is not valid set logic syntax.
- #short_circuit ⇒ Object
Instance Method Summary collapse
- #ast(cell) ⇒ Object
- #asts ⇒ Object
- #can_match?(cell) ⇒ Boolean
- #cell_substitution? ⇒ Boolean
- #logic_parser ⇒ Object
- #logic_transform ⇒ Object
- #matches?(cell, value) ⇒ Boolean
- #return_on_cant_match ⇒ Object
- #value_parser ⇒ Object
- #value_transform ⇒ Object
Instance Attribute Details
#force ⇒ Object
if true, will raise if a cell is not valid set logic syntax
34 35 36 |
# File 'lib/rudelo/matchers/set_logic.rb', line 34 def force @force end |
#short_circuit ⇒ Object
37 38 39 |
# File 'lib/rudelo/matchers/set_logic.rb', line 37 def short_circuit defined?(@short_circuit) ? @short_circuit : true end |
Instance Method Details
#ast(cell) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/rudelo/matchers/set_logic.rb', line 86 def ast(cell) asts[cell] ||= begin logic_transform.apply(logic_parser.parse(cell)) rescue raise if force nil end end |
#asts ⇒ Object
66 67 68 |
# File 'lib/rudelo/matchers/set_logic.rb', line 66 def asts @asts ||= {} end |
#can_match?(cell) ⇒ Boolean
45 46 47 |
# File 'lib/rudelo/matchers/set_logic.rb', line 45 def can_match?(cell) ! (cell =~ SYNTAX_EXPR).nil? end |
#cell_substitution? ⇒ Boolean
62 63 64 |
# File 'lib/rudelo/matchers/set_logic.rb', line 62 def cell_substitution? true end |
#logic_parser ⇒ Object
70 71 72 |
# File 'lib/rudelo/matchers/set_logic.rb', line 70 def logic_parser @logic_parser ||= Rudelo::Parsers::SetLogicParser.new end |
#logic_transform ⇒ Object
74 75 76 |
# File 'lib/rudelo/matchers/set_logic.rb', line 74 def logic_transform @logic_transform ||= Rudelo::Parsers::SetLogicTransform.new end |
#matches?(cell, value) ⇒ Boolean
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rudelo/matchers/set_logic.rb', line 49 def matches?(cell, value) # puts "\n matches? `#{cell}` => `#{value}`" return false unless force || can_match?(cell) evaluator = ast(cell) return return_on_cant_match if evaluator.nil? in_set = nil in_set = value if value.is_a?(Set) in_set = Set.new if value == '' in_set ||= value_transform.apply(value_parser.parse(value)) result = evaluator.eval(in_set) return result ? true : return_on_cant_match end |
#return_on_cant_match ⇒ Object
41 42 43 |
# File 'lib/rudelo/matchers/set_logic.rb', line 41 def return_on_cant_match short_circuit ? :break : false end |
#value_parser ⇒ Object
78 79 80 |
# File 'lib/rudelo/matchers/set_logic.rb', line 78 def value_parser @value_parser ||= Rudelo::Parsers::SetValueParser.new end |
#value_transform ⇒ Object
82 83 84 |
# File 'lib/rudelo/matchers/set_logic.rb', line 82 def value_transform @value_transform ||= Rudelo::Parsers::SetLogicTransform.new end |