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
- #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
51 52 53 |
# File 'lib/dry/validation/schema/rule.rb', line 51 def add_rule(rule) target.add_rule(rule) end |
#and(other) ⇒ Object Also known as: &
79 80 81 |
# File 'lib/dry/validation/schema/rule.rb', line 79 def and(other) new([:and, [node, other.to_ast]]) end |
#checks ⇒ Object
59 60 61 |
# File 'lib/dry/validation/schema/rule.rb', line 59 def checks target.checks end |
#class ⇒ Object
71 72 73 |
# File 'lib/dry/validation/schema/rule.rb', line 71 def class Schema::Rule end |
#each(*predicates, &block) ⇒ Object
46 47 48 49 |
# File 'lib/dry/validation/schema/rule.rb', line 46 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
99 100 101 102 103 104 |
# File 'lib/dry/validation/schema/rule.rb', line 99 def infer_predicates(predicates) predicates.map do |predicate| name, *args = ::Kernel.Array(predicate).first key(name, args) end end |
#maybe(*predicates) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/dry/validation/schema/rule.rb', line 35 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
75 76 77 |
# File 'lib/dry/validation/schema/rule.rb', line 75 def not new([:not, node]) end |
#or(other) ⇒ Object Also known as: |
84 85 86 |
# File 'lib/dry/validation/schema/rule.rb', line 84 def or(other) new([:or, [node, other.to_ast]]) end |
#required(*predicates) ⇒ Object
29 30 31 32 33 |
# File 'lib/dry/validation/schema/rule.rb', line 29 def required(*predicates) rule = ([key(:filled?)] + infer_predicates(predicates)).reduce(:and) add_rule(__send__(type, rule)) end |
#rules ⇒ Object
55 56 57 |
# File 'lib/dry/validation/schema/rule.rb', line 55 def rules target.rules end |
#schema(other = nil, &block) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/dry/validation/schema/rule.rb', line 16 def schema(other = nil, &block) schema = other ? ::Class.new(other.class) : Validation.Schema( target.schema_class, parent: target, build: false, &block ) schema.config.path = [name] if other schema.config.input_processor = :noop rule = __send__(type, key(:hash?).and(key(schema))) add_rule(rule) end |
#then(other) ⇒ Object Also known as: >
94 95 96 |
# File 'lib/dry/validation/schema/rule.rb', line 94 def then(other) new([:implication, [node, other.to_ast]]) end |
#to_ast ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/dry/validation/schema/rule.rb', line 63 def to_ast if deps.empty? node else [:guard, [deps, node]] end end |
#with(new_options) ⇒ Object
106 107 108 |
# File 'lib/dry/validation/schema/rule.rb', line 106 def with() self.class.new(node, .merge()) end |
#xor(other) ⇒ Object Also known as: ^
89 90 91 |
# File 'lib/dry/validation/schema/rule.rb', line 89 def xor(other) new([:xor, [node, other.to_ast]]) end |