Marco campana avatar Marco campana committed 94751d0

Not casting the empty string for Numeric, Integer and Float Property types

Comments (0)

Files changed (2)

ripple/lib/ripple/core_ext/casting.rb

 # @private
 class Numeric
   def self.ripple_cast(value)
-    return nil if value.nil?
+    return nil if value.nil? || value == ""
     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?
+    return nil if value.nil? || value == ""
     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?
+    return nil if value.nil? || value == ""
     value.respond_to?(:to_f) && value.to_f or raise Ripple::PropertyTypeMismatch.new(self, value)
   end
 end

ripple/spec/ripple/properties_spec.rb

         end
       end
 
-      [0.0, "0", "     000", ""].each do |v|
+      [0.0, "0", "     000"].each do |v|
         it "should cast #{v.inspect} to 0" do
           @prop.type_cast(v).should == 0
         end
       end
 
+      it "should not cast the empty string" do
+        @prop.type_cast("").should be_nil
+      end
+
       [true, false, [], ["something else"]].each do |v|
         it "should raise an error casting #{v.inspect}" do
           lambda { @prop.type_cast(v) }.should raise_error(Ripple::PropertyTypeMismatch)
         @prop = Ripple::Property.new(:foo, Float)
       end
 
-      [0, "0", "0.0", "    0.0", ""].each do |v|
+      [0, "0", "0.0", "    0.0"].each do |v|
         it "should cast #{v.inspect} to 0.0" do
           @prop.type_cast(v).should == 0.0
         end
         end
       end
 
+      it "should not cast the empty string" do
+        @prop.type_cast("").should be_nil
+      end
+
       [true, false, :symbol, [], {}].each do |v|
         it "should raise an error casting #{v.inspect}" do
           lambda { @prop.type_cast(v) }.should raise_error(Ripple::PropertyTypeMismatch)
           @prop.type_cast(v).should be_kind_of(Float)
         end
       end
+
+      it "should not cast the empty string" do
+        @prop.type_cast("").should be_nil
+      end
+
     end
 
     describe "when type is a Time type" do
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.