lib/lotus/validations/attribute.rb in lotus-validations-0.3.3 vs lib/lotus/validations/attribute.rb in lotus-validations-0.4.0
- old
+ new
@@ -83,19 +83,21 @@
# Validates acceptance of the value.
#
# This passes if the value is "truthy", it fails if not.
#
# Truthy examples: `Object.new`, `1`, `"1"`, `true`.
- # Falsey examples: `nil`, `0`, `"0"`, `false`.
+ # Falsy examples: `nil`, `0`, `"0"`, `false`, `""`.
#
# @see Lotus::Validations::ClassMethods#attribute
# @see http://www.rubydoc.info/gems/lotus-utils/Lotus/Utils/Kernel#Boolean-class_method
#
# @since 0.2.0
# @api private
def acceptance
- _validate(__method__) { Lotus::Utils::Kernel.Boolean(@value) }
+ _validate(__method__) do
+ !blank_value? && Lotus::Utils::Kernel.Boolean(@value)
+ end
end
# Validates format of the value.
#
# Coerces the value to a string and then check if it satisfies the defined
@@ -104,11 +106,11 @@
# @see Lotus::Validations::ClassMethods#attribute
#
# @since 0.2.0
# @api private
def format
- _validate(__method__) {|matcher| @value.to_s.match(matcher) }
+ _validate(__method__) {|matcher| @value.to_s.match(matcher) } unless blank_value?
end
# Validates inclusion of the value in the defined collection.
#
# The collection is an objects which implements `#include?`.
@@ -169,19 +171,23 @@
# If the quantity is a Numeric, the size of the value MUST be exactly the
# same.
#
# If the quantity is a Range, the size of the value MUST be included.
#
+ # If the attribute's value is blank, the size will not be considered
+ #
# The value is an object which implements `#size`.
#
# @raise [ArgumentError] if the defined quantity isn't a Numeric or a
# collection
#
# @see Lotus::Validations::ClassMethods#attribute
#
# @since 0.2.0
# @api private
def size
+ return if blank_value?
+
_validate(__method__) do |validator|
case validator
when Numeric, ->(v) { v.respond_to?(:to_int) }
value.size == validator.to_int
when Range