Permalink
Browse files

Remove coercion of octal, binary, hex for Integer properties

* This extra behavior is probably unecessary and would be little used.
  In addition it would cause "0123" to be treated as 83 rather than 123,
  which I think is probably more expected to anyone familiar with how
  String#to_i works.  It's only really the Kernel::Integer() method that
  treats "0123" as an octal, and it's in far less frequent use than
  String#to_i.

  If specific octal, binary or hex coercion is necessary then I would
  probably suggest using a custom type rather than an Integer property.

  I am removing this for now, since if I'm wrong it will be easy to add
  back in, but it would be really difficult to remove behavior like this
  after a release.
  • Loading branch information...
1 parent cbce204 commit 24abf9f4475c3a873a9ca295b81d770bb06c1a98 @dkubb dkubb committed Jun 17, 2009
Showing with 1 addition and 23 deletions.
  1. +1 −6 lib/dm-core/property.rb
  2. +0 −17 spec/public/property_spec.rb
View
@@ -687,12 +687,7 @@ def typecast(value)
# TODO: optimize this using a Hash lookup table
if primitive == Integer
# only typecast a String that looks like a number
- case value.to_s
- when /\A(-?(?:0|[1-9]\d*)(?:\.\d+)?)\z/ then $1.to_i # integer or float
- when /\A(0(?:\d+|b[01]+|x[a-fA-F\d]+))\z/ then Integer($1) # octal, binary or hex
- else
- value
- end
+ value.to_s =~ /\A(-?(?:0+|[1-9]\d*)(?:\.\d+)?)\z/ ? $1.to_i : value
elsif primitive == String then value.to_s
elsif primitive == TrueClass then %w[ true 1 t ].include?(value.to_s.downcase)
elsif primitive == BigDecimal then BigDecimal(value.to_s)
@@ -330,29 +330,12 @@ class ::Image
Image.properties[:width].typecast("-24").should == -24
end
- describe "and value is a string representation of a hex, binary or octal integer" do
- it 'returns 36' do
- Image.properties[:width].typecast("0x24").should == 36
- end
-
- it 'returns 5' do
- Image.properties[:width].typecast("0b101").should == 5
- end
-
- it 'returns 19' do
- Image.properties[:width].typecast("023").should == 19
- end
-
- end
-
describe "and it has various valid presentations of 0" do
it { Image.properties[:width].typecast(0).should == 0 }
it { Image.properties[:width].typecast(0.0).should == 0 }
it { Image.properties[:width].typecast("0").should == 0 }
it { Image.properties[:width].typecast("0.0").should == 0 }
it { Image.properties[:width].typecast("00").should == 0 }
- it { Image.properties[:width].typecast("0b0").should == 0 }
- it { Image.properties[:width].typecast("0x0").should == 0 }
it { Image.properties[:width].typecast(BigDecimal("0.0")).should == 0 }
it { Image.properties[:width].typecast(Rational(0,1)).should == 0 }
end

0 comments on commit 24abf9f

Please sign in to comment.