Class: Dry::Validation::Schema::Key

Inherits:
DSL
  • Object
show all
Defined in:
lib/dry/validation/schema/key.rb

Instance Attribute Summary collapse

Attributes inherited from DSL

#checks, #name, #options, #registry, #rules

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DSL

[], #add_check, #add_rule, #initialize, #inspect, #not, #optional, #path, #predicate, #predicate?, #rule_ast, #to_rule, #with

Methods included from Deprecations

format, #logger, #warn

Constructor Details

This class inherits a constructor from Dry::Validation::Schema::DSL

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dry/validation/schema/key.rb', line 55

def method_missing(meth, *args, &block)
  registry.ensure_valid_predicate(meth, args)
  predicate = predicate(meth, args)

  if block
    val = value.instance_eval(&block)
    add_rule(create_rule([:and, [predicate, val.to_ast]]))
  else
    rule = create_rule([type, [name, predicate]])
    add_rule(rule)
    rule
  end
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

Class Method Details

.typeObject



9
10
11
# File 'lib/dry/validation/schema/key.rb', line 9

def self.type
  :key
end

Instance Method Details

#classObject



13
14
15
# File 'lib/dry/validation/schema/key.rb', line 13

def class
  Key
end

#each(*predicates, &block) ⇒ Object



25
26
27
# File 'lib/dry/validation/schema/key.rb', line 25

def each(*predicates, &block)
  create_rule([type, [name, value.each(*predicates, &block).to_ast]])
end

#hash?(&block) ⇒ Boolean

Returns:

  • (Boolean)


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

def hash?(&block)
  predicate = predicate(:hash?)

  if block
    val = value.instance_eval(&block)

    rule = create_rule(predicate)
      .and(create_rule([type, [name, val.to_ast]]))

    add_rule(rule)
    rule
  else
    add_rule(create_rule(predicate))
  end
end

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



29
30
31
# File 'lib/dry/validation/schema/key.rb', line 29

def schema(other = nil, &block)
  create_rule([type, [name, value.schema(other, &block).to_ast]])
end

#to_astObject



21
22
23
# File 'lib/dry/validation/schema/key.rb', line 21

def to_ast
  [type, [name, super]]
end

#typeObject



17
18
19
# File 'lib/dry/validation/schema/key.rb', line 17

def type
  self.class.type
end

#valueObject



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

def value
  Value[name, registry: registry]
end