Commit d80c0aec by cole bradley

add big decimal conversion

add spec for big decimal conversion
parent c6d98432
module RailsParam
module Param
DEFAULT_PRECISION = 14
class InvalidParameterError < StandardError
attr_accessor :param, :options
end
......@@ -54,6 +56,7 @@ module RailsParam
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
return nil
rescue ArgumentError
raise InvalidParameterError, "'#{param}' is not a valid #{type}"
......
......@@ -100,6 +100,20 @@ describe RailsParam::Param do
expect(controller.params["foo"]).to eql(DateTime.parse("2014-08-07T12:25:00.000+02:00"))
end
describe "BigDecimals" do
it "converts to BigDecimal using default precision" do
allow(controller).to receive(:params).and_return({ "foo" => 12345.67890123456})
controller.param! :foo, BigDecimal
expect(controller.params["foo"]).to eql 12345.678901235
end
it "converts to BigDecimal using precision option" do
allow(controller).to receive(:params).and_return({ "foo" => 12345.6789 })
controller.param! :foo, BigDecimal, precision: 6
expect(controller.params["foo"]).to eql 12345.7
end
end
describe "booleans" do
it "converts 1/0" do
allow(controller).to receive(:params).and_return({ "foo" => "1" })
......
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