Class: Dry::Validation::Values

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dry/validation/values.rb

Overview

A convenient wrapper for data processed by schemas

Values are available within the rule blocks. They act as hash-like objects and expose a convenient API for accessing data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Values

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Values.



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

def initialize(data)
  @data = data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



64
65
66
67
68
69
70
# File 'lib/dry/validation/values.rb', line 64

def method_missing(meth, *args, &block)
  if data.respond_to?(meth)
    data.public_send(meth, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#dataHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Schema’s result output

Returns:

  • (Hash)


23
24
25
# File 'lib/dry/validation/values.rb', line 23

def data
  @data
end

Instance Method Details

#[](*args) ⇒ Object

Read from the provided key

Examples:

rule(:age) do
  key.failure('must be > 18') if values[:age] <= 18
end

Parameters:

  • key (Symbol)

Returns:

  • (Object)


42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dry/validation/values.rb', line 42

def [](*args)
  if args.size.equal?(1)
    case (key = args[0])
    when Symbol then data[key]
    when String then self[*key.split(DOT).map(&:to_sym)]
    when Array then self[*key]
    else
      raise ArgumentError, '+key+ must be a symbol, string, array, or a list of keys for dig'
    end
  else
    data.dig(*args)
  end
end

#respond_to_missing?(meth, include_private = false) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

def respond_to_missing?(meth, include_private = false)
  super || data.respond_to?(meth, include_private)
end