Class: Dry::Validation::RuleCompiler
- Inherits:
-
Object
- Object
- Dry::Validation::RuleCompiler
- Defined in:
- lib/dry/validation/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_each(node) ⇒ Object
- #visit_group(node) ⇒ Object
- #visit_implication(node) ⇒ Object
- #visit_key(node) ⇒ Object
- #visit_or(node) ⇒ Object
- #visit_predicate(node) ⇒ Object
- #visit_set(node) ⇒ Object
- #visit_val(node) ⇒ Object
Constructor Details
#initialize(predicates) ⇒ RuleCompiler
Returns a new instance of RuleCompiler.
8 9 10 |
# File 'lib/dry/validation/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/validation/rule_compiler.rb', line 6 def predicates @predicates end |
Instance Method Details
#call(ast) ⇒ Object
12 13 14 |
# File 'lib/dry/validation/rule_compiler.rb', line 12 def call(ast) ast.map { |node| visit(node) } end |
#visit(node) ⇒ Object
16 17 18 19 |
# File 'lib/dry/validation/rule_compiler.rb', line 16 def visit(node) name, nodes = node send(:"visit_#{name}", nodes) end |
#visit_and(node) ⇒ Object
46 47 48 49 |
# File 'lib/dry/validation/rule_compiler.rb', line 46 def visit_and(node) left, right = node visit(left) & visit(right) end |
#visit_each(node) ⇒ Object
36 37 38 39 |
# File 'lib/dry/validation/rule_compiler.rb', line 36 def visit_each(node) name, rule = node Rule::Each.new(name, visit(rule)) end |
#visit_group(node) ⇒ Object
61 62 63 64 |
# File 'lib/dry/validation/rule_compiler.rb', line 61 def visit_group(node) identifier, predicate = node Rule::Group.new(identifier, visit(predicate)) end |
#visit_implication(node) ⇒ Object
56 57 58 59 |
# File 'lib/dry/validation/rule_compiler.rb', line 56 def visit_implication(node) left, right = node visit(left) > visit(right) end |
#visit_key(node) ⇒ Object
21 22 23 24 |
# File 'lib/dry/validation/rule_compiler.rb', line 21 def visit_key(node) name, predicate = node Rule::Key.new(name, visit(predicate)) end |
#visit_or(node) ⇒ Object
51 52 53 54 |
# File 'lib/dry/validation/rule_compiler.rb', line 51 def visit_or(node) left, right = node visit(left) | visit(right) end |
#visit_predicate(node) ⇒ Object
41 42 43 44 |
# File 'lib/dry/validation/rule_compiler.rb', line 41 def visit_predicate(node) name, args = node predicates[name].curry(*args) end |