From 61916e408d86d19d1659cd8042de6503aecc6c98 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Fri, 12 Mar 2010 14:34:13 -0800 Subject: Add a bunch of specs for attribute type casting. --- lib/arel/algebra/attributes/attribute.rb | 13 +++++-------- lib/arel/algebra/attributes/boolean.rb | 11 ++++++----- 2 files changed, 11 insertions(+), 13 deletions(-) (limited to 'lib') diff --git a/lib/arel/algebra/attributes/attribute.rb b/lib/arel/algebra/attributes/attribute.rb index f4cec828e3..03cf44a552 100644 --- a/lib/arel/algebra/attributes/attribute.rb +++ b/lib/arel/algebra/attributes/attribute.rb @@ -160,16 +160,13 @@ module Arel def type_cast_to_numeric(value, method) return unless value if value.respond_to?(:to_str) - if value.to_str =~ /\A(-?(?:0|[1-9]\d*)(?:\.\d+)?|(?:\.\d+))\z/ - $1.send(method) - else - value - end + str = value.to_str.strip + return if str.empty? + return $1.send(method) if str =~ /\A(-?(?:0|[1-9]\d*)(?:\.\d+)?|(?:\.\d+))\z/ elsif value.respond_to?(method) - value.send(method) - else - raise typecast_error(value) + return value.send(method) end + raise typecast_error(value) end def typecast_error(value) diff --git a/lib/arel/algebra/attributes/boolean.rb b/lib/arel/algebra/attributes/boolean.rb index 0ca7cd6d24..d69f2465df 100644 --- a/lib/arel/algebra/attributes/boolean.rb +++ b/lib/arel/algebra/attributes/boolean.rb @@ -3,11 +3,12 @@ module Arel class Boolean < Attribute def type_cast(value) case value - when true, false then value - when nil then options[:allow_nil] ? nil : false - when 1 then true - when 0 then false - else + when true, false then value + # when nil then options[:allow_nil] ? nil : false + when nil then false + when 1 then true + when 0 then false + else case value.to_s.downcase.strip when 'true' then true when 'false' then false -- cgit v1.2.3