Commit e0c42cb8 by Brad Urani

Merge pull request #2 from stantoncbradley/develop

add BigDecimal conversion for formatted currency strings
parents 2ec350a8 86c2ca0a
...@@ -56,7 +56,10 @@ module RailsParam ...@@ -56,7 +56,10 @@ module RailsParam
return Array(param.split(options[:delimiter] || ",")) if type == Array return Array(param.split(options[:delimiter] || ",")) if type == Array
return Hash[param.split(options[:delimiter] || ",").map{|c| c.split(options[:separator] || ":")}] if type == Hash return Hash[param.split(options[:delimiter] || ",").map{|c| c.split(options[:separator] || ":")}] if type == Hash
return (/(false|f|no|n|0)$/i === param.to_s ? false : (/(true|t|yes|y|1)$/i === param.to_s ? true : nil)) if type == TrueClass || type == FalseClass || type == :boolean return (/(false|f|no|n|0)$/i === param.to_s ? false : (/(true|t|yes|y|1)$/i === param.to_s ? true : nil)) if type == TrueClass || type == FalseClass || type == :boolean
return BigDecimal.new(param, (options[:precision] || DEFAULT_PRECISION)) if type == BigDecimal if type == BigDecimal
param = param.delete('$,').strip.to_f if param.is_a?(String)
return BigDecimal.new(param, (options[:precision] || DEFAULT_PRECISION)) if type == BigDecimal
end
return nil return nil
rescue ArgumentError rescue ArgumentError
raise InvalidParameterError, "'#{param}' is not a valid #{type}" raise InvalidParameterError, "'#{param}' is not a valid #{type}"
......
...@@ -112,6 +112,13 @@ describe RailsParam::Param do ...@@ -112,6 +112,13 @@ describe RailsParam::Param do
controller.param! :foo, BigDecimal, precision: 6 controller.param! :foo, BigDecimal, precision: 6
expect(controller.params["foo"]).to eql 12345.7 expect(controller.params["foo"]).to eql 12345.7
end end
it "converts formatted currency string to big decimal" do
allow(controller).to receive(:params).and_return({ "foo" => "$100,000"})
controller.param! :foo, BigDecimal
expect(controller.params["foo"]).to eql 100000.0
end
end end
describe "booleans" do describe "booleans" do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment