Skip to content

Commit

Permalink
Merge pull request #2 from stantoncbradley/develop
Browse files Browse the repository at this point in the history
add BigDecimal conversion for formatted currency strings
  • Loading branch information
Brad Urani committed Oct 23, 2014
2 parents 2ec350a + 86c2ca0 commit e0c42cb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/rails_param/param.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ def coerce(param, type, options = {})
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 (/(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
rescue ArgumentError
raise InvalidParameterError, "'#{param}' is not a valid #{type}"
Expand Down
7 changes: 7 additions & 0 deletions spec/rails_param/param_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ def params; end
controller.param! :foo, BigDecimal, precision: 6
expect(controller.params["foo"]).to eql 12345.7
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

describe "booleans" do
Expand Down

0 comments on commit e0c42cb

Please sign in to comment.