lib/braintree/validation_error_collection.rb in braintree-1.0.1 vs lib/braintree/validation_error_collection.rb in braintree-1.1.0
- old
+ new
@@ -18,29 +18,29 @@
# #=> [#<Braintree::ValidationError (81715) Credit card number is invalid.>]
# result.errors.for(:customer).for(:credit_card).for(:billing_address).on(:country_name)
# #=> [#<Braintree::ValidationError (91803) Country name is not an accepted country.>]
class ValidationErrorCollection
include Enumerable
-
+
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
-
+
# Accesses the error at the given index.
def [](index)
@errors[index]
end
def deep_size # :nodoc:
size + @nested.values.inject(0) { |count, error_collection| count + error_collection.deep_size }
end
-
+
# Iterates over errors at the current level. Nested errors will not be yielded.
def each(&block)
@errors.each(&block)
end
@@ -56,15 +56,15 @@
# Returns an array of ValidationError objects on the given attribute.
def on(attribute)
@errors.select { |error| error.attribute == attribute.to_s }
end
-
+
# The number of errors at this level. This does not include nested errors.
def size
@errors.size
- end
+ end
def _inner_inspect(scope = []) # :nodoc:
all = []
scope_string = scope.join("/")
if @errors.any?
@@ -73,8 +73,8 @@
@nested.each do |key, values|
all << values._inner_inspect(scope + [key])
end
all.join(", ")
end
- end
+ end
end