Class: Braintree::ValidationErrorCollection

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

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ ValidationErrorCollection

:nodoc:



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

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

#[](index) ⇒ Object

Accesses the error at the given index.



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

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

#_inner_inspect(scope = []) ⇒ Object

:nodoc:



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

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

#deep_errorsObject

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



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

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

#deep_sizeObject



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

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

#each(&block) ⇒ Object

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



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

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

#for(nested_key) ⇒ Object

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



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

def for(nested_key)
  @nested[nested_key]
end

#for_index(index) ⇒ Object



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

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

#inspectObject

:nodoc:



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

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

#on(attribute) ⇒ Object

Returns an array of ValidationError objects on the given attribute.



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

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

#shallow_errorsObject

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



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

def shallow_errors
  @errors.dup
end

#sizeObject

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



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

def size
  @errors.size
end