Class: Braintree::ValidationErrorCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/braintree/validation_error_collection.rb

Overview

Instance Method Summary (collapse)

Constructor Details

- (ValidationErrorCollection) initialize(data)

:nodoc:



6
7
8
9
10
11
12
13
# File 'lib/braintree/validation_error_collection.rb', line 6

def initialize(data) # :nodoc:
  @errors = data[:errors].map { |hash| Braintree::ValidationError.new(hash) }
  @nested = {}
  data.keys.each do |key|
    next if key == :errors
    @nested[key] = ValidationErrorCollection.new(data[key])
  end
end

Instance Method Details

- (Object) [](index)

Accesses the error at the given index.



16
17
18
# File 'lib/braintree/validation_error_collection.rb', line 16

def [](index)
  @errors[index]
end

- (Object) _inner_inspect(scope = [])

:nodoc:



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/braintree/validation_error_collection.rb', line 64

def _inner_inspect(scope = []) # :nodoc:
  all = []
  scope_string = scope.join("/")
  if @errors.any?
    all << "#{scope_string}:[" + @errors.map { |e| "(#{e.code}) #{e.message}" }.join(", ") + "]"
  end
  @nested.each do |key, values|
    all << values._inner_inspect(scope + [key])
  end
  all.join(", ")
end

- (Object) deep_errors

Returns an array of ValidationError objects at this level and all nested levels in the error hierarchy



22
23
24
# File 'lib/braintree/validation_error_collection.rb', line 22

def deep_errors
  ([@errors] + @nested.values.map { |error_collection| error_collection.deep_errors }).flatten
end

- (Object) deep_size



26
27
28
# File 'lib/braintree/validation_error_collection.rb', line 26

def deep_size
  size + @nested.values.inject(0) { |count, error_collection| count + error_collection.deep_size }
end

- (Object) each(&block)

Iterates over errors at the current level. Nested errors will not be yielded.



31
32
33
# File 'lib/braintree/validation_error_collection.rb', line 31

def each(&block)
  @errors.each(&block)
end

- (Object) for(nested_key)

Returns a ValidationErrorCollection of errors nested under the given nested_key. Returns nil if there are not any errors nested under the given key.



37
38
39
# File 'lib/braintree/validation_error_collection.rb', line 37

def for(nested_key)
  @nested[nested_key]
end

- (Object) for_index(index)



41
42
43
# File 'lib/braintree/validation_error_collection.rb', line 41

def for_index(index)
  self.for("index_#{index}".to_sym)
end

- (Object) inspect

:nodoc:



45
46
47
# File 'lib/braintree/validation_error_collection.rb', line 45

def inspect # :nodoc:
  "#<#{self.class} errors#{_inner_inspect}>"
end

- (Object) on(attribute)

Returns an array of ValidationError objects on the given attribute.



50
51
52
# File 'lib/braintree/validation_error_collection.rb', line 50

def on(attribute)
  @errors.select { |error| error.attribute == attribute.to_s }
end

- (Object) shallow_errors

Returns an array of ValidationError objects at the given level in the error hierarchy



55
56
57
# File 'lib/braintree/validation_error_collection.rb', line 55

def shallow_errors
  @errors.dup
end

- (Object) size

The number of errors at this level. This does not include nested errors.



60
61
62
# File 'lib/braintree/validation_error_collection.rb', line 60

def size
  @errors.size
end