lib/symmetric_encryption/coerce.rb in symmetric-encryption-3.9.1 vs lib/symmetric_encryption/coerce.rb in symmetric-encryption-4.0.0.beta3
- old
+ new
@@ -11,12 +11,12 @@
date: Date
}
# Coerce given value into given type
# Does not coerce json or yaml values
- def self.coerce(value, type, from_type=nil)
- return if blank?(value)
+ def self.coerce(value, type, from_type = nil)
+ return value if value.nil? || (value == '')
from_type ||= value.class
case type
when :json
value
@@ -30,11 +30,12 @@
# Uses coercible gem to coerce values from strings into the target type
# Note: if the type is :string, then the value is returned as is, and the
# coercible gem is not used at all.
def self.coerce_from_string(value, type)
- return if value.nil?
+ return value if value.nil? || (value == '')
+
case type
when :string
value
when :json
JSON.load(value)
@@ -47,11 +48,11 @@
# Uses coercible gem to coerce values to strings from the specified type
# Note: if the type is :string, and value is not nil, then #to_s is called
# on the value and the coercible gem is not used at all.
def self.coerce_to_string(value, type)
- return if value.nil?
+ return value if value.nil? || (value == '')
case type
when :string
value.to_s
when :json
@@ -70,23 +71,7 @@
else
TYPE_MAP[symbol]
end
end
- private
-
- BLANK_RE = /\A[[:space:]]*\z/
-
- # Returns [true|false] whether the supplied value is blank?
- def self.blank?(value)
- return true if value.nil?
- if value.is_a?(String)
- return true if value.empty?
- # When Binary data is supplied that cannot convert to UTF-8 it is clearly not blank
- return false unless value.dup.force_encoding(SymmetricEncryption::UTF8_ENCODING).valid_encoding?
- (value =~ BLANK_RE) == 0
- else
- false
- end
- end
end
end