Class: Dry::Validation::Schema::Rule

Inherits:
BasicObject
Defined in:
lib/dry/validation/schema/rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @node = node
  @type = options.fetch(:type, :and)
  @deps = options.fetch(:deps, [])
  @name = options.fetch(:name)
  @target = options.fetch(:target)
  @options = options
end

Instance Attribute Details

#depsObject (readonly)

Returns the value of attribute deps.



5
6
7
# File 'lib/dry/validation/schema/rule.rb', line 5

def deps
  @deps
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/dry/validation/schema/rule.rb', line 5

def name
  @name
end

#nodeObject (readonly)

Returns the value of attribute node.



5
6
7
# File 'lib/dry/validation/schema/rule.rb', line 5

def node
  @node
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/dry/validation/schema/rule.rb', line 5

def options
  @options
end

#targetObject (readonly)

Returns the value of attribute target.



5
6
7
# File 'lib/dry/validation/schema/rule.rb', line 5

def target
  @target
end

#typeObject (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

#checksObject



52
53
54
# File 'lib/dry/validation/schema/rule.rb', line 52

def checks
  target.checks
end

#classObject



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

#notObject



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_astObject



56
57
58
# File 'lib/dry/validation/schema/rule.rb', line 56

def rule_ast
  rules.size > 0 ? target.rule_ast : [to_ast]
end

#rulesObject



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_astObject



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(new_options)
  self.class.new(node, options.merge(new_options))
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