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



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

#checksObject



59
60
61
# File 'lib/dry/validation/schema/rule.rb', line 59

def checks
  target.checks
end

#classObject



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

#notObject



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

#rulesObject



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_astObject



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