Class: Dry::Validation::Schema::Rule
- Inherits:
- BasicObject
- Defined in:
- lib/dry/validation/schema/rule.rb
Instance Attribute Summary collapse
-
#deps ⇒ Object
readonly
Returns the value of attribute deps.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #add_rule(rule) ⇒ Object
- #and(other) ⇒ Object (also: #&)
- #checks ⇒ Object
- #class ⇒ Object
- #each(*predicates, &block) ⇒ Object
- #infer_predicates(predicates) ⇒ Object
-
#initialize(node, options = {}) ⇒ Rule
constructor
A new instance of Rule.
- #maybe(*predicates) ⇒ Object
- #not ⇒ Object
- #or(other) ⇒ Object (also: #|)
- #required(*predicates) ⇒ Object
- #rule_ast ⇒ Object
- #rules ⇒ Object
- #schema(other = nil, &block) ⇒ Object
- #then(other) ⇒ Object (also: #>)
- #to_ast ⇒ Object
- #with(new_options) ⇒ Object
- #xor(other) ⇒ Object (also: #^)
Constructor Details
#initialize(node, options = {}) ⇒ Rule
Returns a new instance of Rule.
7 8 9 10 11 12 13 14 |
# File 'lib/dry/validation/schema/rule.rb', line 7 def initialize(node, = {}) @node = node @type = .fetch(:type, :and) @deps = .fetch(:deps, []) @name = .fetch(:name) @target = .fetch(:target) @options = end |
Instance Attribute Details
#deps ⇒ Object (readonly)
Returns the value of attribute deps.
5 6 7 |
# File 'lib/dry/validation/schema/rule.rb', line 5 def deps @deps end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/dry/validation/schema/rule.rb', line 5 def name @name end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
5 6 7 |
# File 'lib/dry/validation/schema/rule.rb', line 5 def node @node end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/dry/validation/schema/rule.rb', line 5 def @options end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
5 6 7 |
# File 'lib/dry/validation/schema/rule.rb', line 5 def target @target end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
5 6 7 |
# File 'lib/dry/validation/schema/rule.rb', line 5 def type @type end |
Instance Method Details
#add_rule(rule) ⇒ Object
44 45 46 |
# File 'lib/dry/validation/schema/rule.rb', line 44 def add_rule(rule) target.add_rule(rule) end |
#and(other) ⇒ Object Also known as: &
76 77 78 |
# File 'lib/dry/validation/schema/rule.rb', line 76 def and(other) new([:and, [node, other.to_ast]]) end |
#checks ⇒ Object
52 53 54 |
# File 'lib/dry/validation/schema/rule.rb', line 52 def checks target.checks end |
#class ⇒ Object
68 69 70 |
# File 'lib/dry/validation/schema/rule.rb', line 68 def class Schema::Rule end |
#each(*predicates, &block) ⇒ Object
39 40 41 42 |
# File 'lib/dry/validation/schema/rule.rb', line 39 def each(*predicates, &block) rule = target.each(*predicates, &block) add_rule(__send__(type, new([target.type, [name, rule.to_ast]]))) end |
#infer_predicates(predicates) ⇒ Object
96 97 98 99 100 101 |
# File 'lib/dry/validation/schema/rule.rb', line 96 def infer_predicates(predicates) predicates.map do |predicate| name, *args = ::Kernel.Array(predicate).first key(name, args) end end |
#maybe(*predicates) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/dry/validation/schema/rule.rb', line 28 def maybe(*predicates) rule = if predicates.size > 0 key(:none?).or(infer_predicates(predicates).reduce(:and)) else key(:none?).or(key(:filled?)) end add_rule(__send__(type, rule)) end |
#not ⇒ Object
72 73 74 |
# File 'lib/dry/validation/schema/rule.rb', line 72 def not new([:not, node]) end |
#or(other) ⇒ Object Also known as: |
81 82 83 |
# File 'lib/dry/validation/schema/rule.rb', line 81 def or(other) new([:or, [node, other.to_ast]]) end |
#required(*predicates) ⇒ Object
22 23 24 25 26 |
# File 'lib/dry/validation/schema/rule.rb', line 22 def required(*predicates) rule = ([key(:filled?)] + infer_predicates(predicates)).reduce(:and) add_rule(__send__(type, rule)) end |
#rule_ast ⇒ Object
56 57 58 |
# File 'lib/dry/validation/schema/rule.rb', line 56 def rule_ast rules.size > 0 ? target.rule_ast : [to_ast] end |
#rules ⇒ Object
48 49 50 |
# File 'lib/dry/validation/schema/rule.rb', line 48 def rules target.rules end |
#schema(other = nil, &block) ⇒ Object
16 17 18 19 20 |
# File 'lib/dry/validation/schema/rule.rb', line 16 def schema(other = nil, &block) schema = Schema.create_class(target, other, &block) rule = __send__(type, key(:hash?).and(key(schema))) add_rule(rule) end |
#then(other) ⇒ Object Also known as: >
91 92 93 |
# File 'lib/dry/validation/schema/rule.rb', line 91 def then(other) new([:implication, [node, other.to_ast]]) end |
#to_ast ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/dry/validation/schema/rule.rb', line 60 def to_ast if deps.empty? node else [:guard, [deps, node]] end end |
#with(new_options) ⇒ Object
103 104 105 |
# File 'lib/dry/validation/schema/rule.rb', line 103 def with() self.class.new(node, .merge()) end |
#xor(other) ⇒ Object Also known as: ^
86 87 88 |
# File 'lib/dry/validation/schema/rule.rb', line 86 def xor(other) new([:xor, [node, other.to_ast]]) end |