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



49
50
51
# File 'lib/dry/validation/schema/rule.rb', line 49

def add_rule(rule)
  target.add_rule(rule)
end

#and(other) ⇒ Object Also known as: &



81
82
83
# File 'lib/dry/validation/schema/rule.rb', line 81

def and(other)
  new([:and, [node, other.to_ast]])
end

#checksObject



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

def checks
  target.checks
end

#classObject



73
74
75
# File 'lib/dry/validation/schema/rule.rb', line 73

def class
  Schema::Rule
end

#each(*predicates, &block) ⇒ Object



44
45
46
47
# File 'lib/dry/validation/schema/rule.rb', line 44

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



101
102
103
104
105
106
# File 'lib/dry/validation/schema/rule.rb', line 101

def infer_predicates(predicates)
  predicates.map do |predicate|
    name, *args = ::Kernel.Array(predicate).first
    key(name, args)
  end
end

#inspectObject Also known as: to_s



16
17
18
# File 'lib/dry/validation/schema/rule.rb', line 16

def inspect
  to_ast.inspect
end

#maybe(*predicates) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/dry/validation/schema/rule.rb', line 33

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



77
78
79
# File 'lib/dry/validation/schema/rule.rb', line 77

def not
  new([:not, node])
end

#or(other) ⇒ Object Also known as: |



86
87
88
# File 'lib/dry/validation/schema/rule.rb', line 86

def or(other)
  new([:or, [node, other.to_ast]])
end

#required(*predicates) ⇒ Object



27
28
29
30
31
# File 'lib/dry/validation/schema/rule.rb', line 27

def required(*predicates)
  rule = ([key(:filled?)] + infer_predicates(predicates)).reduce(:and)

  add_rule(__send__(type, rule))
end

#rule_astObject



61
62
63
# File 'lib/dry/validation/schema/rule.rb', line 61

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

#rulesObject



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

def rules
  target.rules
end

#schema(other = nil, &block) ⇒ Object



21
22
23
24
25
# File 'lib/dry/validation/schema/rule.rb', line 21

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: >



96
97
98
# File 'lib/dry/validation/schema/rule.rb', line 96

def then(other)
  new([:implication, [node, other.to_ast]])
end

#to_astObject



65
66
67
68
69
70
71
# File 'lib/dry/validation/schema/rule.rb', line 65

def to_ast
  if deps.empty?
    node
  else
    [:guard, [deps, node]]
  end
end

#with(new_options) ⇒ Object



108
109
110
# File 'lib/dry/validation/schema/rule.rb', line 108

def with(new_options)
  self.class.new(node, options.merge(new_options))
end

#xor(other) ⇒ Object Also known as: ^



91
92
93
# File 'lib/dry/validation/schema/rule.rb', line 91

def xor(other)
  new([:xor, [node, other.to_ast]])
end