lib/saneitized/converter.rb in saneitized-1.1.1 vs lib/saneitized/converter.rb in saneitized-1.2.0
- old
+ new
@@ -4,45 +4,60 @@
module Saneitized
def self.convert(unknown)
return Saneitized::Hash.new(unknown) if unknown.is_a? ::Hash
return Saneitized::Array.new(unknown) if unknown.is_a? ::Array
return unknown unless unknown.is_a? String #Only attempt to convert string
- return true if unknown == 'true'
- return false if unknown == 'false'
- if value = Converter.integer?(unknown) then return value end
- if value = Converter.float?(unknown) then return value end
- if value = Converter.json?(unknown) then return convert(value) end
- if value = Converter.time?(unknown) then return value end
+ %w(true false nil integer float json time).each do |type|
+ value = Converter.send(type + '?', unknown)
+ next if value == :nope
+ return (type == 'json') ? convert(value) : value
+ end
+ #if value = Converter.integer?(unknown) then return value end
+ #if value = Converter.float?(unknown) then return value end
+ #if (value = Converter.json?(unknown)) != :nope then return convert(value) end
+ #if value = Converter.time?(unknown) then return value end
+
unknown
end
module Converter
extend self
+ def true?(unknown)
+ (unknown == 'true') ? true : :nope
+ end
+
+ def false?(unknown)
+ (unknown == 'false') ? false : :nope
+ end
+
+ def nil?(unknown)
+ (%w(nil null NULL).include? unknown) ? nil : :nope
+ end
+
def json?(unknown)
JSON.parse(unknown)
rescue JSON::ParserError, TypeError
- false
+ :nope
end
def integer?(unknown)
Integer(unknown)
rescue ArgumentError, TypeError
- false
+ :nope
end
def float?(unknown)
Float(unknown)
rescue ArgumentError, TypeError
- false
+ :nope
end
def time?(unknown)
- Chronic.parse(unknown)
- rescue ArgumentError, TypeError
- false
+ value = Chronic.parse(unknown)
+ value.nil? ? :nope : value
end
end
end