lib/lotus/validations/errors.rb in lotus-validations-0.2.0 vs lib/lotus/validations/errors.rb in lotus-validations-0.2.1
- old
+ new
@@ -21,14 +21,27 @@
# Check if the set is empty
#
# @return [TrueClass,FalseClass] the result of the check
#
# @since 0.1.0
+ #
+ # @see Lotus::Validations::Errors#any?
def empty?
@errors.empty?
end
+ # Check if the set has any entry
+ #
+ # @return [TrueClass,FalseClass] the result of the check
+ #
+ # @since 0.2.0
+ #
+ # @see Lotus::Validations::Errors#empty?
+ def any?
+ @errors.any?
+ end
+
# Returns how many validations have failed
#
# @return [Fixnum] the count
#
# @since 0.1.0
@@ -72,16 +85,16 @@
end
# Add an error to the set
#
# @param attribute [Symbol] the name of the attribute
- # @param validation [Symbol] the name of the validation
- # @param expected [Object] the expected value
- # @param actual [Object] the actual value
+ # @param errors [Array] a collection of errors
#
# @since 0.1.0
# @api private
+ #
+ # @see Lotus::Validations::Error
def add(attribute, *errors)
@errors[attribute].push(*errors) if errors.any?
end
# Return the errors for the given attribute
@@ -105,9 +118,27 @@
other.is_a?(self.class) &&
other.errors == errors
end
alias_method :eql?, :==
+
+ # Return a serializable Hash representation of the errors.
+ #
+ # @return [Lotus::Utils::Hash] the Hash
+ #
+ # @since 0.2.1
+ def to_h
+ Utils::Hash.new(@errors).deep_dup
+ end
+
+ # Return a flat collection of errors.
+ #
+ # @return [Array]
+ #
+ # @since 0.2.1
+ def to_a
+ errors.dup
+ end
protected
# A flatten set of errors for all the attributes
#
# @since 0.1.0