Class: Dry::Validation::Schema::Value

Inherits:
BasicObject
Includes:
Definition
Defined in:
lib/dry/validation/schema/value.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Definition

#attr, #confirmation, #key, #optional, #rule, #schema, #value

Constructor Details

#initialize(name) ⇒ Value

Returns a new instance of Value.



9
10
11
12
13
14
# File 'lib/dry/validation/schema/value.rb', line 9

def initialize(name)
  @name = name
  @rules = []
  @groups = []
  @checks = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dry/validation/schema/value.rb', line 31

def method_missing(meth, *args, &block)
  rule = Schema::Rule.new(name, [:val, [name, [:predicate, [meth, args]]]])

  if block
    val_rule = yield

    if val_rule.is_a?(Schema::Rule)
      rule & val_rule
    else
      Schema::Rule.new(name, [:and, [rule.to_ary, [:set, [name, rules.map(&:to_ary)]]]])
    end
  else
    rule
  end
end

Instance Attribute Details

#checksObject (readonly)

Returns the value of attribute checks.



7
8
9
# File 'lib/dry/validation/schema/value.rb', line 7

def checks
  @checks
end

#groupsObject (readonly)

Returns the value of attribute groups.



7
8
9
# File 'lib/dry/validation/schema/value.rb', line 7

def groups
  @groups
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/dry/validation/schema/value.rb', line 7

def name
  @name
end

#rulesObject (readonly)

Returns the value of attribute rules.



7
8
9
# File 'lib/dry/validation/schema/value.rb', line 7

def rules
  @rules
end

Instance Method Details

#each(&block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dry/validation/schema/value.rb', line 16

def each(&block)
  val_rule = yield(self)

  each_rule =
    if val_rule.is_a?(Schema::Rule)
      val_rule.to_ary
    else
      [:set, [name, rules.map(&:to_ary)]]
    end

  Schema::Rule.new(name, [:each, [name, each_rule]])
end