Commits

Sean Cribbs committed 4f1a244

Use blank? where possible to prevent strings from casting where inappropriate, fix discrepancy with Symbol casting to Integer in 1.8/1.9

Comments (0)

Files changed (2)

ripple/lib/ripple/core_ext/casting.rb

 #    See the License for the specific language governing permissions and
 #    limitations under the License.
 
+require 'active_support/core_ext/object/blank'
+require 'active_support/core_ext/object/to_json'
+
 # @private
 class Object
   def self.ripple_cast(value)
 # @private
 class Symbol
   def self.ripple_cast(value)
-    return nil if value.nil?
+    return nil if value.blank?
     value.respond_to?(:to_s) && value.to_s.intern or raise Ripple::PropertyTypeMismatch.new(self, value)
   end
 end
 # @private
 class Numeric
   def self.ripple_cast(value)
-    return nil if value.nil? || value == ""
+    return nil if value.blank?
     raise Ripple::PropertyTypeMismatch.new(self,value) unless value.respond_to?(:to_i) && value.respond_to?(:to_f)
     float_value = value.to_f
     int_value = value.to_i
 # @private
 class Integer
   def self.ripple_cast(value)
-    return nil if value.nil? || value == ""
-    value.respond_to?(:to_i) && value.to_i or raise Ripple::PropertyTypeMismatch.new(self, value)
+    return nil if value.nil? || (String === value && value.blank?)
+    !value.is_a?(Symbol) && value.respond_to?(:to_i) && value.to_i or raise Ripple::PropertyTypeMismatch.new(self, value)
   end
 end
 
 # @private
 class Float
   def self.ripple_cast(value)
-    return nil if value.nil? || value == ""
+    return nil if value.nil? || (String === value && value.blank?)
     value.respond_to?(:to_f) && value.to_f or raise Ripple::PropertyTypeMismatch.new(self, value)
   end
 end
   end
 
   def self.ripple_cast(value)
-    return nil if value.nil?
+    return nil if value.blank?
     value.respond_to?(:to_time) && value.to_time or raise Ripple::PropertyTypeMismatch.new(self, value)
   end
 end
   end
 
   def self.ripple_cast(value)
-    return nil if value.nil?
+    return nil if value.blank?
     value.respond_to?(:to_date) && value.to_date or raise Ripple::PropertyTypeMismatch.new(self, value)
   end
 end
   end
 
   def self.ripple_cast(value)
-    return nil if value.nil?
+    return nil if value.blank?
     value.respond_to?(:to_datetime) && value.to_datetime or raise Ripple::PropertyTypeMismatch.new(self, value)
   end
 end

ripple/spec/ripple/properties_spec.rb

         end
       end
 
-      it "should not cast the empty string" do
+      it "should not cast the blank string" do
         @prop.type_cast("").should be_nil
+        @prop.type_cast("   ").should be_nil
       end
 
-      [true, false, [], ["something else"]].each do |v|
+      [true, false, [], {}, :symbol, ["something else"]].each do |v|
         it "should raise an error casting #{v.inspect}" do
           lambda { @prop.type_cast(v) }.should raise_error(Ripple::PropertyTypeMismatch)
         end
         end
       end
 
-      it "should not cast the empty string" do
+      it "should not cast the blank string" do
         @prop.type_cast("").should be_nil
+        @prop.type_cast("   ").should be_nil
       end
 
       [true, false, :symbol, [], {}].each do |v|
         end
       end
 
-      it "should not cast the empty string" do
+      it "should not cast the blank string" do
         @prop.type_cast("").should be_nil
+        @prop.type_cast("   ").should be_nil
       end
-
     end
 
     describe "when type is a Time type" do
           @prop.type_cast(v).should == Time.utc(2010,03,16,12)
         end
       end
+
+      it "should not cast blank types" do
+        @prop.type_cast([]).should be_nil
+        @prop.type_cast({}).should be_nil
+        @prop.type_cast("").should be_nil
+        @prop.type_cast("  ").should be_nil
+      end
     end
 
     describe "when type is a Date type" do
           @prop.type_cast(v).should == Date.civil(2010,3,16)
         end
       end
+
+      it "should not cast blank types" do
+        @prop.type_cast([]).should be_nil
+        @prop.type_cast({}).should be_nil
+        @prop.type_cast("").should be_nil
+        @prop.type_cast("  ").should be_nil
+      end
     end
   end
 end