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

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

Constant Summary collapse

INVALID_PREDICATES =
{
  value: [],
  maybe: [:empty?, :none?],
  filled: [:empty?, :filled?],
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Deprecations

format, #logger, #warn

Constructor Details

#initialize(node, options = {}) ⇒ Rule

Returns a new instance of Rule.



17
18
19
20
21
22
23
24
# File 'lib/dry/validation/schema/rule.rb', line 17

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)



184
185
186
187
188
189
190
# File 'lib/dry/validation/schema/rule.rb', line 184

def method_missing(meth, *args, &block)
  if target.predicate?(meth)
    target.__send__(meth, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#depsObject (readonly)

Returns the value of attribute deps.



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

def deps
  @deps
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#nodeObject (readonly)

Returns the value of attribute node.



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

def node
  @node
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#targetObject (readonly)

Returns the value of attribute target.



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

def target
  @target
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#add_rule(rule) ⇒ Object



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

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

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



142
143
144
# File 'lib/dry/validation/schema/rule.rb', line 142

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

#checksObject



116
117
118
# File 'lib/dry/validation/schema/rule.rb', line 116

def checks
  target.checks
end

#classObject



134
135
136
# File 'lib/dry/validation/schema/rule.rb', line 134

def class
  Schema::Rule
end

#each(*predicates, &block) ⇒ Object



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

def each(*predicates, &block)
  rule = target.each(*predicates, &block)
  add_rule(__send__(type, new([target.type, [name, rule.to_ast]])))
end

#filled(*predicates, &block) ⇒ Object



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

def filled(*predicates, &block)
  left = ([key(:filled?)] + infer_predicates(predicates, :filled)).reduce(:and)

  rule =
    if block
      left.and(Key[name, registry: registry].instance_eval(&block))
    else
      left
    end

  add_rule(__send__(type, rule))
end

#infer_predicates(predicates, macro = nil) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/dry/validation/schema/rule.rb', line 162

def infer_predicates(predicates, macro = nil)
  predicates.flat_map(&::Kernel.method(:Array)).map do |predicate|
    name, *args = ::Kernel.Array(predicate)

    if macro && INVALID_PREDICATES[macro].include?(name)
      ::Kernel.raise InvalidSchemaError, "you can't use #{name} predicate with #{macro} macro"
    else
      key(name, args)
    end
  end
end

#inspectObject Also known as: to_s



26
27
28
# File 'lib/dry/validation/schema/rule.rb', line 26

def inspect
  to_ast.inspect
end

#maybe(*predicates, &block) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/dry/validation/schema/rule.rb', line 90

def maybe(*predicates, &block)
  left = key(:none?).not

  from_predicates = infer_predicates(predicates, :maybe).reduce(:and)
  from_block = block ? Key[name, registry: registry].instance_eval(&block) : nil

  right = [from_predicates, from_block].compact.reduce(:and) || key(:filled?)

  rule = left.then(right)

  add_rule(__send__(type, rule))
end

#notObject



138
139
140
# File 'lib/dry/validation/schema/rule.rb', line 138

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

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



147
148
149
# File 'lib/dry/validation/schema/rule.rb', line 147

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

#registryObject



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

def registry
  target.registry
end

#required(*predicates) ⇒ Object



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

def required(*predicates)
  warn 'required is deprecated - use filled instead.'

  filled(*predicates)
end

#rule_astObject



120
121
122
# File 'lib/dry/validation/schema/rule.rb', line 120

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

#rulesObject



112
113
114
# File 'lib/dry/validation/schema/rule.rb', line 112

def rules
  target.rules
end

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



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

def schema(other = nil, &block)
  schema = Schema.create_class(target, other, &block)

  if schema.config.type_specs
    target.type_map[name] = schema.type_map
  end

  rule = __send__(type, key(:hash?).and(key(schema)))
  add_rule(rule)
end

#schema?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/dry/validation/schema/rule.rb', line 42

def schema?
  target.schema?
end

#then(other) ⇒ Object Also known as: >



157
158
159
# File 'lib/dry/validation/schema/rule.rb', line 157

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

#to_astObject



124
125
126
127
128
129
130
131
132
# File 'lib/dry/validation/schema/rule.rb', line 124

def to_ast
  rule_node = name ? [:rule, [name, node]] : node

  if deps.empty?
    rule_node
  else
    [:guard, [deps, rule_node]]
  end
end

#to_ruleObject



178
179
180
# File 'lib/dry/validation/schema/rule.rb', line 178

def to_rule
  self
end

#type_mapObject



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

def type_map
  target.type_map
end

#type_map?Boolean

Returns:

  • (Boolean)


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

def type_map?
  target.type_map?
end

#value(*predicates, &block) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dry/validation/schema/rule.rb', line 77

def value(*predicates, &block)
  if predicates.empty? && !block
    ::Kernel.raise ::ArgumentError, "wrong number of arguments (given 0, expected at least 1)"
  end

  from_predicates = infer_predicates(predicates, :value).reduce(:and)
  from_block = block ? Key[name, registry: registry].instance_eval(&block) : nil

  rule = [from_predicates, from_block].compact.reduce(:and)

  add_rule(__send__(type, rule))
end

#with(new_options) ⇒ Object



174
175
176
# File 'lib/dry/validation/schema/rule.rb', line 174

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

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



152
153
154
# File 'lib/dry/validation/schema/rule.rb', line 152

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