class Reform::Contract::Result::Pointer

Note: this class will be redundant in Reform 3, where the public API allows/enforces to pass options to errors (e.g. errors(locale: “br”)) which means we don’t have to “lazy-handle” that with “pointers”. :private:

Public Class Methods

new(result, path) click to toggle source
# File lib/reform/result.rb, line 52
def initialize(result, path)
  @result, @path = result, path
end

Public Instance Methods

advance(*path) click to toggle source
# File lib/reform/result.rb, line 64
def advance(*path)
  path = @path + path.compact # remove index if nil.
  traverse = traverse(@result.errors, path)
  # when returns {} is because no errors are found
  # when returns a String is because an error has been found on the main key not in the nested one.
  #   Collection with custom rule will return a String here and does not need to be considered
  #   as a nested error.
  # when return an Array without an index is same as String but it's a property with a custom rule.
  # Check test/validation/dry_validation_test.rb:685
  return if traverse == {} || traverse.is_a?(String) || (traverse.is_a?(Array) && path.compact.size == 1)

  Pointer.new(@result, path)
end
errors(*args) click to toggle source
# File lib/reform/result.rb, line 58
def errors(*args);   traverse_for(:errors, *args) end
hints(*args) click to toggle source
# File lib/reform/result.rb, line 62
def hints(*args);    traverse_for(:hints, *args) end
messages(*args) click to toggle source
# File lib/reform/result.rb, line 60
def messages(*args); traverse_for(:messages, *args) end

Private Instance Methods

traverse(hash, path) click to toggle source
# File lib/reform/result.rb, line 80
def traverse(hash, path)
  path.inject(hash) { |errs, segment| errs[segment] || {} } # FIXME. test if all segments present.
end
traverse_for(method, *args) click to toggle source
# File lib/reform/result.rb, line 84
def traverse_for(method, *args)
  traverse(@result.public_send(method, *args), @path) # TODO: return [] if nil
end