param_checker 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +31 -4
- data/lib/param_checker.rb +2 -0
- data/lib/param_checker/hash_ext.rb +36 -0
- data/lib/param_checker/version.rb +1 -1
- data/spec/lib/param_checker_spec.rb +53 -0
- data/spec/spec_helper.rb +1 -0
- metadata +5 -4
data/README.rdoc
CHANGED
@@ -39,13 +39,13 @@ There are currently 5 supported functions:
|
|
39
39
|
* +param+ is the string parameter to check.
|
40
40
|
* +default+ is the value that will be returned when +param+ does not pass the check.
|
41
41
|
* +options+ are function specific options to check +param+ against:
|
42
|
-
*
|
43
|
-
*
|
44
|
-
*
|
42
|
+
* +:min+, +:max+ in +check_integer+ and +check_float+ are the minimum and maximum allowed values of param. (If not provided then no range is checked at all.)
|
43
|
+
* +:allowed+ in +check_string+ and +check_symbol+ represent the allowed values of +param+. It can be either a regular expression, a string (resp. a symbol for +check_symbol+), or an array of strings (resp. an array of symbols for +check_symbol+).
|
44
|
+
* +:true+ and +:false+ represent the allowed string values for the true and false booleans. By default is :true => ["1", "true"] and :false => ["0", "false"]
|
45
45
|
|
46
46
|
All functions return the casted value (check_integer returns an integer, check_symbol returns a symbol, and so on).
|
47
47
|
|
48
|
-
|
48
|
+
=== Examples
|
49
49
|
|
50
50
|
Below are some simple examples how to use those functions.
|
51
51
|
|
@@ -61,6 +61,33 @@ Below are some simple examples how to use those functions.
|
|
61
61
|
# Have custom boolean string representation values.
|
62
62
|
accepted = check_boolean(params[:accepted], false, :true => ["yep", "yes"], :false => ["nope", "no"])
|
63
63
|
|
64
|
+
== Alternative usage
|
65
|
+
|
66
|
+
Since version 0.3 you can also extend your Hash or HashWithIndifferentAccess with ParamChecker::HashExt. This will allow you to directly call the ParamChecker methods on the +params+ hash:
|
67
|
+
|
68
|
+
params.check(type, params_key, default, options)
|
69
|
+
|
70
|
+
+type+ can be:
|
71
|
+
|
72
|
+
* +:i+ or +:integer+ calls check_integer internally
|
73
|
+
* +:f+ or +:float+ calls check_float internally
|
74
|
+
* +:s+ or +:string+ calls check_string internally
|
75
|
+
* +:sym+ or +:symbol+ calls check_symbol internally
|
76
|
+
* +:b+ or +:boolean+ calls check boolean internally
|
77
|
+
|
78
|
+
+params_key+ can be either an array of keys or just one key to access the hash.
|
79
|
+
|
80
|
+
=== Examples
|
81
|
+
|
82
|
+
# Checks params[:page] and returns the integer representation if valid.
|
83
|
+
params.check(:i, :page, 5, :min => 1)
|
84
|
+
|
85
|
+
# Check params[:company][:name] and returns "Comparilla" if invalid.
|
86
|
+
params.check(:s, [:company, :name], "Comparilla")
|
87
|
+
|
88
|
+
# Does exactly the same (alternative type symbol for string type)
|
89
|
+
params.check(:string, [:company, :name], "Comparilla")
|
90
|
+
|
64
91
|
== Testing
|
65
92
|
|
66
93
|
ParamChecker uses RSpec for testing and has a rake task for executing the provided specs
|
data/lib/param_checker.rb
CHANGED
@@ -0,0 +1,36 @@
|
|
1
|
+
module ParamChecker
|
2
|
+
module HashExt
|
3
|
+
|
4
|
+
def check(type, key, default, options = {})
|
5
|
+
value = nil
|
6
|
+
if key.class == Array
|
7
|
+
value = self
|
8
|
+
key.each do |k|
|
9
|
+
value = value[k]
|
10
|
+
break if value.nil?
|
11
|
+
end
|
12
|
+
else # String || Symbol
|
13
|
+
value = self[key]
|
14
|
+
end
|
15
|
+
|
16
|
+
if value.nil?
|
17
|
+
default
|
18
|
+
else
|
19
|
+
case type
|
20
|
+
when :integer, :i
|
21
|
+
ParamChecker.check_integer(value, default, options)
|
22
|
+
when :float, :f
|
23
|
+
ParamChecker.check_float(value, default, options)
|
24
|
+
when :string, :s
|
25
|
+
ParamChecker.check_string(value, default, options)
|
26
|
+
when :symbol, :sym
|
27
|
+
ParamChecker.check_symbol(value, default, options)
|
28
|
+
when :boolean, :b
|
29
|
+
ParamChecker.check_boolean(value, default, options)
|
30
|
+
else
|
31
|
+
raise
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -121,6 +121,59 @@ describe "ParamChecker" do
|
|
121
121
|
end
|
122
122
|
end
|
123
123
|
|
124
|
+
describe "hash extension" do
|
125
|
+
|
126
|
+
|
127
|
+
before do
|
128
|
+
class Hash
|
129
|
+
include ParamChecker::HashExt
|
130
|
+
end
|
131
|
+
|
132
|
+
@params = Hash[
|
133
|
+
:lorem => "ipsum",
|
134
|
+
:foo => { :bar => "dato" }
|
135
|
+
]
|
136
|
+
# @params.extend(ParamChecker::HashExt)
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should call param checker module functions" do
|
140
|
+
ParamChecker.should_receive(:check_integer)
|
141
|
+
@params.check(:i, :lorem, nil)
|
142
|
+
|
143
|
+
ParamChecker.should_receive(:check_integer)
|
144
|
+
@params.check(:integer, :lorem, nil)
|
145
|
+
|
146
|
+
ParamChecker.should_receive(:check_float)
|
147
|
+
@params.check(:f, :lorem, nil)
|
148
|
+
|
149
|
+
ParamChecker.should_receive(:check_float)
|
150
|
+
@params.check(:float, :lorem, nil)
|
151
|
+
|
152
|
+
ParamChecker.should_receive(:check_string)
|
153
|
+
@params.check(:s, :lorem, nil)
|
154
|
+
|
155
|
+
ParamChecker.should_receive(:check_string)
|
156
|
+
@params.check(:string, :lorem, nil)
|
157
|
+
|
158
|
+
ParamChecker.should_receive(:check_symbol)
|
159
|
+
@params.check(:sym, :lorem, nil)
|
160
|
+
|
161
|
+
ParamChecker.should_receive(:check_symbol)
|
162
|
+
@params.check(:symbol, :lorem, nil)
|
163
|
+
|
164
|
+
ParamChecker.should_receive(:check_boolean)
|
165
|
+
@params.check(:b, :lorem, nil)
|
166
|
+
|
167
|
+
ParamChecker.should_receive(:check_boolean)
|
168
|
+
@params.check(:boolean, :lorem, nil)
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should use multiple keys" do
|
172
|
+
ParamChecker.should_receive(:check_string).with("dato", nil, {})
|
173
|
+
@params.check(:s, [:foo, :bar], nil)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
124
177
|
it "can be called as module functions" do
|
125
178
|
ParamChecker.check_integer("5", 99).should == 5
|
126
179
|
ParamChecker.check_float("5.1", 99.2).should == 5.1
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: param_checker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 3
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kai Schlamp
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-03-
|
18
|
+
date: 2011-03-28 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -50,6 +50,7 @@ files:
|
|
50
50
|
- README.rdoc
|
51
51
|
- Rakefile
|
52
52
|
- lib/param_checker.rb
|
53
|
+
- lib/param_checker/hash_ext.rb
|
53
54
|
- lib/param_checker/version.rb
|
54
55
|
- param_checker.gemspec
|
55
56
|
- spec/lib/param_checker_spec.rb
|