Class: Dry::Validation::Rust::Contract::Values

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Values

Wraps validated output data for rule and result access.



14
15
16
# File 'lib/dry/validation/rust/contract/values.rb', line 14

def initialize(data)
  @data = data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object (private)



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

def method_missing(name, ...)
  return data.public_send(name, ...) if data.respond_to?(name)

  super
end

Instance Attribute Details

#dataHash (readonly)

Returns validated output data.

Returns:

  • (Hash)

    validated output data.



11
12
13
# File 'lib/dry/validation/rust/contract/values.rb', line 11

def data
  @data
end

Instance Method Details

#[](*args) ⇒ Object

Reads a value by key, path, or supported multi-path specification.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dry/validation/rust/contract/values.rb', line 19

def [](*args)
  return data.dig(*args) if args.length > 1

  spec = args.fetch(0)
  if spec.is_a?(Hash) && spec.values.first.is_a?(Array)
    head = spec.keys.first
    return spec.values.first.map { |tail| Path.fetch(data, [head, *Path.parse(tail)], nil) }
  end

  value = Path.fetch(data, spec)
  value.equal?(Path::Undefined) ? nil : value
end

#deconstruct_keys(keys) ⇒ Object

Supports Hash pattern matching against output data.



53
54
55
# File 'lib/dry/validation/rust/contract/values.rb', line 53

def deconstruct_keys(keys)
  keys ? data.slice(*keys) : data
end

#eachObject

Iterates through the underlying output Hash.



38
39
40
# File 'lib/dry/validation/rust/contract/values.rb', line 38

def each(&)
  data.each(&)
end

#fetchObject

Fetches a value using Hash#fetch semantics.



43
44
45
# File 'lib/dry/validation/rust/contract/values.rb', line 43

def fetch(*, &)
  data.fetch(*, &)
end

#key?(key) ⇒ Boolean

Returns whether data contains a key or path.

Returns:

  • (Boolean)


33
34
35
# File 'lib/dry/validation/rust/contract/values.rb', line 33

def key?(key)
  Path.key?(data, key)
end

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

Reports public Hash methods delegated to underlying data.

Returns:

  • (Boolean)


58
59
60
# File 'lib/dry/validation/rust/contract/values.rb', line 58

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

#to_hObject

Returns the underlying output Hash.



48
49
50
# File 'lib/dry/validation/rust/contract/values.rb', line 48

def to_h
  data
end