Class: Rudelo::Matchers::SetLogic

Inherits:
Rufus::Decision::Matcher
  • Object
show all
Defined in:
lib/rudelo/matchers/set_logic.rb

Constant Summary collapse

SYNTAX_EXPR =
%r{^\$\([^)]*\)|\$in|#[><=]}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#forceObject

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_circuitObject



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

#astsObject



66
67
68
# File 'lib/rudelo/matchers/set_logic.rb', line 66

def asts
  @asts ||= {}
end

#can_match?(cell) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


62
63
64
# File 'lib/rudelo/matchers/set_logic.rb', line 62

def cell_substitution?
  true
end

#logic_parserObject



70
71
72
# File 'lib/rudelo/matchers/set_logic.rb', line 70

def logic_parser
  @logic_parser ||= Rudelo::Parsers::SetLogicParser.new
end

#logic_transformObject



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

Returns:

  • (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_matchObject



41
42
43
# File 'lib/rudelo/matchers/set_logic.rb', line 41

def return_on_cant_match
  short_circuit ? :break : false
end

#value_parserObject



78
79
80
# File 'lib/rudelo/matchers/set_logic.rb', line 78

def value_parser
  @value_parser ||= Rudelo::Parsers::SetValueParser.new
end

#value_transformObject



82
83
84
# File 'lib/rudelo/matchers/set_logic.rb', line 82

def value_transform
  @value_transform ||= Rudelo::Parsers::SetLogicTransform.new
end