Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
rails_param
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
ikcrm_common
rails_param
Commits
2ec350a8
Commit
2ec350a8
authored
Oct 23, 2014
by
Brad Urani
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1 from stantoncbradley/develop
add big decimal conversion
parents
c6d98432
d80c0aec
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
0 deletions
+17
-0
lib/rails_param/param.rb
+3
-0
spec/rails_param/param_spec.rb
+14
-0
No files found.
lib/rails_param/param.rb
View file @
2ec350a8
module
RailsParam
module
RailsParam
module
Param
module
Param
DEFAULT_PRECISION
=
14
class
InvalidParameterError
<
StandardError
class
InvalidParameterError
<
StandardError
attr_accessor
:param
,
:options
attr_accessor
:param
,
:options
end
end
...
@@ -54,6 +56,7 @@ module RailsParam
...
@@ -54,6 +56,7 @@ 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
return
nil
return
nil
rescue
ArgumentError
rescue
ArgumentError
raise
InvalidParameterError
,
"'
#{
param
}
' is not a valid
#{
type
}
"
raise
InvalidParameterError
,
"'
#{
param
}
' is not a valid
#{
type
}
"
...
...
spec/rails_param/param_spec.rb
View file @
2ec350a8
...
@@ -100,6 +100,20 @@ describe RailsParam::Param do
...
@@ -100,6 +100,20 @@ describe RailsParam::Param do
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
DateTime
.
parse
(
"2014-08-07T12:25:00.000+02:00"
))
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
DateTime
.
parse
(
"2014-08-07T12:25:00.000+02:00"
))
end
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
describe
"booleans"
do
it
"converts 1/0"
do
it
"converts 1/0"
do
allow
(
controller
).
to
receive
(
:params
).
and_return
({
"foo"
=>
"1"
})
allow
(
controller
).
to
receive
(
:params
).
and_return
({
"foo"
=>
"1"
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment