lib/lotus/validations/errors.rb in lotus-validations-0.2.3 vs lib/lotus/validations/errors.rb in lotus-validations-0.2.4

- old
+ new

@@ -13,11 +13,11 @@ # Initialize the errors # # @since 0.1.0 # @api private def initialize - @errors = Hash.new {|h,k| h[k] = [] } + @errors = Hash.new end # Check if the set is empty # # @return [TrueClass,FalseClass] the result of the check @@ -92,19 +92,22 @@ # @since 0.1.0 # @api private # # @see Lotus::Validations::Error def add(attribute, *errors) - @errors[attribute].push(*errors) if errors.any? + if errors.any? + @errors[attribute] ||= [] + @errors[attribute].push(*errors) + end end # Return the errors for the given attribute # # @param attribute [Symbol] the name of the attribute # # @since 0.1.0 def for(attribute) - @errors[attribute] + @errors.fetch(attribute) { [] } end # Check if the current set of errors equals to the one who belongs to # `other`. #