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
8f3c9b43
Commit
8f3c9b43
authored
Jul 08, 2015
by
PiYa
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed setting default and throwing errors case boolean
parent
36e10375
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
16 additions
and
4 deletions
+16
-4
.ruby-version
+1
-1
lib/rails_param/param.rb
+4
-3
spec/rails_param/param_spec.rb
+11
-0
No files found.
.ruby-version
View file @
8f3c9b43
2.1.
2
2.1.
1
lib/rails_param/param.rb
View file @
8f3c9b43
...
@@ -15,11 +15,12 @@ module RailsParam
...
@@ -15,11 +15,12 @@ module RailsParam
def
param!
(
name
,
type
,
options
=
{},
&
block
)
def
param!
(
name
,
type
,
options
=
{},
&
block
)
name
=
name
.
to_s
unless
name
.
is_a?
Integer
# keep index for validating elements
name
=
name
.
to_s
unless
name
.
is_a?
Integer
# keep index for validating elements
return
unless
params
.
member?
(
name
)
||
options
[
:default
].
present?
||
options
[
:required
]
return
unless
params
.
member?
(
name
)
||
(
not
options
[
:default
].
nil?
)
||
options
[
:required
]
begin
begin
params
[
name
]
=
coerce
(
params
[
name
],
type
,
options
)
params
[
name
]
=
coerce
(
params
[
name
],
type
,
options
)
params
[
name
]
=
(
options
[
:default
].
call
if
options
[
:default
].
respond_to?
(
:call
))
||
options
[
:default
]
if
params
[
name
].
nil?
and
options
[
:default
]
params
[
name
]
=
(
options
[
:default
].
call
if
options
[
:default
].
respond_to?
(
:call
))
||
options
[
:default
]
if
params
[
name
].
nil?
and
(
not
options
[
:default
].
nil?
)
params
[
name
]
=
options
[
:transform
].
to_proc
.
call
(
params
[
name
])
if
params
[
name
]
and
options
[
:transform
]
params
[
name
]
=
options
[
:transform
].
to_proc
.
call
(
params
[
name
])
if
params
[
name
]
and
options
[
:transform
]
validate!
(
params
[
name
],
options
)
validate!
(
params
[
name
],
options
)
...
@@ -84,7 +85,7 @@ module RailsParam
...
@@ -84,7 +85,7 @@ module RailsParam
return
DateTime
.
parse
(
param
)
if
type
==
DateTime
return
DateTime
.
parse
(
param
)
if
type
==
DateTime
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
:
(
raise
ArgumentError
)
))
if
type
==
TrueClass
||
type
==
FalseClass
||
type
==
:boolean
if
type
==
BigDecimal
if
type
==
BigDecimal
param
=
param
.
delete
(
'$,'
).
strip
.
to_f
if
param
.
is_a?
(
String
)
param
=
param
.
delete
(
'$,'
).
strip
.
to_f
if
param
.
is_a?
(
String
)
return
BigDecimal
.
new
(
param
,
(
options
[
:precision
]
||
DEFAULT_PRECISION
))
return
BigDecimal
.
new
(
param
,
(
options
[
:precision
]
||
DEFAULT_PRECISION
))
...
...
spec/rails_param/param_spec.rb
View file @
8f3c9b43
...
@@ -172,12 +172,23 @@ describe RailsParam::Param do
...
@@ -172,12 +172,23 @@ describe RailsParam::Param do
controller
.
param!
:foo
,
TrueClass
controller
.
param!
:foo
,
TrueClass
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
false
)
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
false
)
end
end
it
"return InvalidParameterError if value not boolean"
do
allow
(
controller
).
to
receive
(
:params
).
and_return
({
"foo"
=>
"1111"
})
expect
{
controller
.
param!
:foo
,
:boolean
}.
to
raise_error
(
RailsParam
::
Param
::
InvalidParameterError
)
end
it
"set default boolean"
do
allow
(
controller
).
to
receive
(
:params
).
and_return
({})
controller
.
param!
:foo
,
:boolean
,
default:
false
expect
(
controller
.
params
[
"foo"
]).
to
eql
(
false
)
end
end
end
it
"raises InvalidParameterError if the value is invalid"
do
it
"raises InvalidParameterError if the value is invalid"
do
allow
(
controller
).
to
receive
(
:params
).
and_return
({
"foo"
=>
"1984-01-32"
})
allow
(
controller
).
to
receive
(
:params
).
and_return
({
"foo"
=>
"1984-01-32"
})
expect
{
controller
.
param!
:foo
,
Date
}.
to
raise_error
(
RailsParam
::
Param
::
InvalidParameterError
)
expect
{
controller
.
param!
:foo
,
Date
}.
to
raise_error
(
RailsParam
::
Param
::
InvalidParameterError
)
end
end
end
end
describe
'validating nested hash'
do
describe
'validating nested hash'
do
...
...
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