Class: Dry::Logic::RuleCompiler
- Inherits:
-
Object
- Object
- Dry::Logic::RuleCompiler
- Defined in:
- lib/dry/logic/rule_compiler.rb
Instance Attribute Summary collapse
-
#predicates ⇒ Object
readonly
Returns the value of attribute predicates.
Instance Method Summary collapse
- #call(ast) ⇒ Object
-
#initialize(predicates) ⇒ RuleCompiler
constructor
A new instance of RuleCompiler.
- #visit(node) ⇒ Object
- #visit_and(node) ⇒ Object
- #visit_attr(node) ⇒ Object
- #visit_check(node) ⇒ Object
- #visit_each(node) ⇒ Object
- #visit_implication(node) ⇒ Object
- #visit_key(node) ⇒ Object
- #visit_not(node) ⇒ Object
- #visit_or(node) ⇒ Object
- #visit_predicate(node) ⇒ Object
- #visit_set(node) ⇒ Object
- #visit_xor(node) ⇒ Object
Constructor Details
#initialize(predicates) ⇒ RuleCompiler
Returns a new instance of RuleCompiler.
8 9 10 |
# File 'lib/dry/logic/rule_compiler.rb', line 8 def initialize(predicates) @predicates = predicates end |
Instance Attribute Details
#predicates ⇒ Object (readonly)
Returns the value of attribute predicates.
6 7 8 |
# File 'lib/dry/logic/rule_compiler.rb', line 6 def predicates @predicates end |
Instance Method Details
#call(ast) ⇒ Object
12 13 14 |
# File 'lib/dry/logic/rule_compiler.rb', line 12 def call(ast) ast.map { |node| visit(node) } end |
#visit(node) ⇒ Object
16 17 18 19 |
# File 'lib/dry/logic/rule_compiler.rb', line 16 def visit(node) name, nodes = node send(:"visit_#{name}", nodes) end |
#visit_and(node) ⇒ Object
60 61 62 63 |
# File 'lib/dry/logic/rule_compiler.rb', line 60 def visit_and(node) left, right = node visit(left).and(visit(right)) end |
#visit_attr(node) ⇒ Object
35 36 37 38 |
# File 'lib/dry/logic/rule_compiler.rb', line 35 def visit_attr(node) name, predicate = node Operations::Attr.new(visit(predicate), name: name) end |
#visit_check(node) ⇒ Object
21 22 23 24 |
# File 'lib/dry/logic/rule_compiler.rb', line 21 def visit_check(node) keys, predicate = node Operations::Check.new(visit(predicate), keys: keys) end |
#visit_each(node) ⇒ Object
44 45 46 |
# File 'lib/dry/logic/rule_compiler.rb', line 44 def visit_each(node) Operations::Each.new(visit(node)) end |
#visit_implication(node) ⇒ Object
75 76 77 78 |
# File 'lib/dry/logic/rule_compiler.rb', line 75 def visit_implication(node) left, right = node visit(left).then(visit(right)) end |
#visit_key(node) ⇒ Object
30 31 32 33 |
# File 'lib/dry/logic/rule_compiler.rb', line 30 def visit_key(node) name, predicate = node Operations::Key.new(visit(predicate), name: name) end |
#visit_not(node) ⇒ Object
26 27 28 |
# File 'lib/dry/logic/rule_compiler.rb', line 26 def visit_not(node) Operations::Negation.new(visit(node)) end |
#visit_or(node) ⇒ Object
65 66 67 68 |
# File 'lib/dry/logic/rule_compiler.rb', line 65 def visit_or(node) left, right = node visit(left).or(visit(right)) end |
#visit_predicate(node) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/dry/logic/rule_compiler.rb', line 48 def visit_predicate(node) name, params = node predicate = Rule::Predicate.build(predicates[name]) if params.size > 1 args = params.map(&:last).reject { |val| val == Undefined } predicate.curry(*args) else predicate end end |
#visit_set(node) ⇒ Object
40 41 42 |
# File 'lib/dry/logic/rule_compiler.rb', line 40 def visit_set(node) Operations::Set.new(*call(node)) end |
#visit_xor(node) ⇒ Object
70 71 72 73 |
# File 'lib/dry/logic/rule_compiler.rb', line 70 def visit_xor(node) left, right = node visit(left).xor(visit(right)) end |