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
d169d11f
Commit
d169d11f
authored
Jan 16, 2015
by
stantoncbradley
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
array validation for primative datatypes
array validation for hashes
parent
c6d98432
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
4 deletions
+37
-4
lib/rails_param/param.rb
+37
-4
spec/rails_param/param_spec.rb
+0
-0
No files found.
lib/rails_param/param.rb
View file @
d169d11f
module
RailsParam
module
Param
DEFAULT_PRECISION
=
14
class
InvalidParameterError
<
StandardError
attr_accessor
:param
,
:options
end
def
param!
(
name
,
type
,
options
=
{})
name
=
name
.
to_s
class
MockController
include
RailsParam
::
Param
attr_accessor
:params
end
def
param!
(
name
,
type
,
options
=
{},
&
block
)
name
=
name
.
to_s
unless
name
.
is_a?
Integer
# keep index for validating elements
return
unless
params
.
member?
(
name
)
||
options
[
:default
].
present?
||
options
[
:required
]
...
...
@@ -15,8 +22,23 @@ module RailsParam
params
[
name
]
=
(
options
[
:default
].
call
if
options
[
:default
].
respond_to?
(
:call
))
||
options
[
:default
]
if
params
[
name
].
nil?
and
options
[
:default
]
params
[
name
]
=
options
[
:transform
].
to_proc
.
call
(
params
[
name
])
if
params
[
name
]
and
options
[
:transform
]
validate!
(
params
[
name
],
options
)
if
block_given?
if
type
==
Array
params
[
name
].
each_with_index
do
|
element
,
i
|
if
element
.
is_a?
(
Hash
)
recurse
element
,
&
block
else
params
[
name
][
i
]
=
recurse
({
i
=>
element
},
i
,
&
block
)
# supply index as key unless value is hash
end
end
else
recurse
params
[
name
],
&
block
end
end
params
[
name
]
rescue
InvalidParameterError
=>
exception
exception
.
param
,
exception
.
options
=
name
,
options
exception
.
param
||=
name
exception
.
options
||=
options
raise
exception
end
end
...
...
@@ -41,6 +63,13 @@ module RailsParam
private
def
recurse
(
params
,
index
=
nil
)
raise
InvalidParameterError
,
'no block given'
unless
block_given?
controller
=
RailsParam
::
Param
::
MockController
.
new
controller
.
params
=
params
yield
(
controller
,
index
)
end
def
coerce
(
param
,
type
,
options
=
{})
begin
return
nil
if
param
.
nil?
...
...
@@ -52,8 +81,12 @@ module RailsParam
return
Time
.
parse
(
param
)
if
type
==
Time
return
DateTime
.
parse
(
param
)
if
type
==
DateTime
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
if
type
==
BigDecimal
param
=
param
.
delete
(
'$,'
).
strip
.
to_f
if
param
.
is_a?
(
String
)
return
BigDecimal
.
new
(
param
,
(
options
[
:precision
]
||
DEFAULT_PRECISION
))
end
return
nil
rescue
ArgumentError
raise
InvalidParameterError
,
"'
#{
param
}
' is not a valid
#{
type
}
"
...
...
spec/rails_param/param_spec.rb
View file @
d169d11f
This diff is collapsed.
Click to expand it.
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