hash_params 2.0.1 → 2.0.2
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +12 -9
- data/README.md +103 -166
- data/lib/hash_params/hash_validator.rb +12 -4
- data/lib/hash_params.rb +1 -1
- data/spec/hash_params_spec.rb +1 -0
- metadata +3 -58
- data/coverage/assets/0.8.0/application.css +0 -799
- data/coverage/assets/0.8.0/application.js +0 -1559
- data/coverage/assets/0.8.0/colorbox/border.png +0 -0
- data/coverage/assets/0.8.0/colorbox/controls.png +0 -0
- data/coverage/assets/0.8.0/colorbox/loading.gif +0 -0
- data/coverage/assets/0.8.0/colorbox/loading_background.png +0 -0
- data/coverage/assets/0.8.0/favicon_green.png +0 -0
- data/coverage/assets/0.8.0/favicon_red.png +0 -0
- data/coverage/assets/0.8.0/favicon_yellow.png +0 -0
- data/coverage/assets/0.8.0/loading.gif +0 -0
- data/coverage/assets/0.8.0/magnify.png +0 -0
- data/coverage/assets/0.8.0/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
- data/coverage/assets/0.8.0/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
- data/coverage/assets/0.8.0/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
- data/coverage/assets/0.8.0/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
- data/coverage/assets/0.8.0/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
- data/coverage/assets/0.8.0/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
- data/coverage/assets/0.8.0/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
- data/coverage/assets/0.8.0/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
- data/coverage/assets/0.8.0/smoothness/images/ui-icons_222222_256x240.png +0 -0
- data/coverage/assets/0.8.0/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
- data/coverage/assets/0.8.0/smoothness/images/ui-icons_454545_256x240.png +0 -0
- data/coverage/assets/0.8.0/smoothness/images/ui-icons_888888_256x240.png +0 -0
- data/coverage/assets/0.8.0/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
- data/coverage/assets/0.9.0/application.css +0 -799
- data/coverage/assets/0.9.0/application.js +0 -1707
- data/coverage/assets/0.9.0/colorbox/border.png +0 -0
- data/coverage/assets/0.9.0/colorbox/controls.png +0 -0
- data/coverage/assets/0.9.0/colorbox/loading.gif +0 -0
- data/coverage/assets/0.9.0/colorbox/loading_background.png +0 -0
- data/coverage/assets/0.9.0/favicon_green.png +0 -0
- data/coverage/assets/0.9.0/favicon_red.png +0 -0
- data/coverage/assets/0.9.0/favicon_yellow.png +0 -0
- data/coverage/assets/0.9.0/loading.gif +0 -0
- data/coverage/assets/0.9.0/magnify.png +0 -0
- data/coverage/assets/0.9.0/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
- data/coverage/assets/0.9.0/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
- data/coverage/assets/0.9.0/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
- data/coverage/assets/0.9.0/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
- data/coverage/assets/0.9.0/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
- data/coverage/assets/0.9.0/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
- data/coverage/assets/0.9.0/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
- data/coverage/assets/0.9.0/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
- data/coverage/assets/0.9.0/smoothness/images/ui-icons_222222_256x240.png +0 -0
- data/coverage/assets/0.9.0/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
- data/coverage/assets/0.9.0/smoothness/images/ui-icons_454545_256x240.png +0 -0
- data/coverage/assets/0.9.0/smoothness/images/ui-icons_888888_256x240.png +0 -0
- data/coverage/assets/0.9.0/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
- data/coverage/index.html +0 -2192
- data/tmp/hash_params.rb +0 -181
- data/tmp/hash_params_spec.rb +0 -102
- data/tmp/module_spec.rb +0 -27
- data/tmp/var_spec.rb +0 -9
- data/tmp/yaml_params.rb +0 -109
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7fda373b2a97192b7d68af0c8a2f581b60e1206b
|
|
4
|
+
data.tar.gz: 851b0042442ab3a3558f7b1a7d409889eae54574
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1ca9a5e181d26357087e3ec00a4453fde04216caa890a01eff2588ff04661bfdb6deca1ca1d8b74bc02b6888fa2c2461da0621937548ae243b6d8c8233bec557
|
|
7
|
+
data.tar.gz: c54fd364b0353c0d577d7bb7a5fd90763b9b1bbcfa258bc79709ec7c9fcc0dd59807cfa8745f620cdbacd59f0f87e97711a6de906eef27ca2f30759d155dc664
|
data/Gemfile.lock
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
hash_params (2.0.
|
|
4
|
+
hash_params (2.0.1)
|
|
5
5
|
|
|
6
6
|
GEM
|
|
7
7
|
remote: https://rubygems.org/
|
|
8
8
|
specs:
|
|
9
9
|
coderay (1.1.0)
|
|
10
10
|
docile (1.1.5)
|
|
11
|
+
json (1.8.3)
|
|
11
12
|
method_source (0.8.2)
|
|
12
|
-
minitest (5.
|
|
13
|
+
minitest (5.8.3)
|
|
13
14
|
minitest-spec (0.0.2.1)
|
|
14
15
|
minitest (>= 3.0)
|
|
15
|
-
|
|
16
|
-
pry (0.10.1)
|
|
16
|
+
pry (0.10.3)
|
|
17
17
|
coderay (~> 1.1.0)
|
|
18
18
|
method_source (~> 0.8.1)
|
|
19
19
|
slop (~> 3.4)
|
|
20
|
-
rake (10.
|
|
21
|
-
simplecov (0.
|
|
20
|
+
rake (10.5.0)
|
|
21
|
+
simplecov (0.11.1)
|
|
22
22
|
docile (~> 1.1.0)
|
|
23
|
-
|
|
24
|
-
simplecov-html (~> 0.
|
|
25
|
-
simplecov-html (0.
|
|
23
|
+
json (~> 1.8)
|
|
24
|
+
simplecov-html (~> 0.10.0)
|
|
25
|
+
simplecov-html (0.10.0)
|
|
26
26
|
slop (3.6.0)
|
|
27
27
|
|
|
28
28
|
PLATFORMS
|
|
@@ -35,3 +35,6 @@ DEPENDENCIES
|
|
|
35
35
|
pry
|
|
36
36
|
rake
|
|
37
37
|
simplecov
|
|
38
|
+
|
|
39
|
+
BUNDLED WITH
|
|
40
|
+
1.11.2
|
data/README.md
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
# hash-params
|
|
2
|
+
#
|
|
2
3
|
_Lightweight Parameter Validation & Type Coercion_
|
|
3
4
|
|
|
4
5
|
This is a variation of the sinatra-param gem https://github.com/mattt/sinatra-param
|
|
@@ -8,217 +9,153 @@ with the sinatra specific things taken out and slight modifications to make it m
|
|
|
8
9
|
|
|
9
10
|
See the spec down the page for usage scenarios
|
|
10
11
|
|
|
11
|
-
## Validations
|
|
12
|
-
|
|
13
|
-
- Validations are passed as a hash.
|
|
14
|
-
- You can validate either individual values or hashes.
|
|
15
|
-
- Hashes can be validated recursively
|
|
16
|
-
- You can call the coersion methods separately if you wish
|
|
17
|
-
- If you don't pass in any validations you'll get the same hash back unless you set the strict option (see below)
|
|
18
|
-
|
|
19
|
-
## Coercions
|
|
20
|
-
|
|
21
|
-
By declaring parameter types, incoming parameters will automatically be coerced into an object of that type.
|
|
22
|
-
|
|
23
|
-
- `String`
|
|
24
|
-
- `Integer`
|
|
25
|
-
- `Float`
|
|
26
|
-
- `Array` _("1,2,3,4,5")_
|
|
27
|
-
- `Hash` _(key1:value1,key2:value2)_
|
|
28
|
-
- `Date`, `Time`, & `DateTime`
|
|
29
|
-
- `:boolean` _("1/0", "true/false", "t/f", "yes/no", "y/n")_
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
Note that the `Hash` and `Array` coercions are not deep (only one level). The internal elements are not validated or coerced, everything is returned as a string.
|
|
33
|
-
|
|
34
|
-
Since Ruby doesn't have a Boolean type use the symbol :boolean. This will attempt to cast the commonly used values to either true or false (`TrueClass` or `FalseClass`)
|
|
35
|
-
|
|
36
|
-
You can also use anything that will respond to `to_proc` such as `:to_i`, `:downcase`, etc. It's up to you to make sure the value will obey the method
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
## Validation Options (Only applies to validating hashes and yaml files)
|
|
40
|
-
|
|
41
|
-
You can pass in the following options along with your validations
|
|
42
|
-
|
|
43
|
-
- `strict`: Defaults to true. Only the elements mentioned in the validation will be returned. If set to false all elements will be returned by the validated values will replace the originals
|
|
44
|
-
- `make_methods`: If passed the returned hash will have methods defined for each key in the new hash. This will allow you access the values using standard dot notation.
|
|
45
|
-
- `raise_errors`: Defaults to true. All errors will be collected during processing and raised just once. If you set this to false no error will be raised, you can check the ```hash.valid?``` method or the ```hash.errors``` collection
|
|
46
|
-
|
|
47
12
|
|
|
48
13
|
## Yaml files
|
|
49
14
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
15
|
+
This gem also provides a YamlParams object which scours the disk for likely config files, intelligently parses and merges them
|
|
16
|
+
and passes them into the HashParams for validation
|
|
17
|
+
|
|
18
|
+
The common way to use it is
|
|
19
|
+
```ruby
|
|
20
|
+
YamlParams.autoconfig
|
|
21
|
+
```
|
|
22
|
+
|
|
57
23
|
You can specify the following in the options hash they will all have sensible defaults except the ```app_name```
|
|
58
24
|
|
|
59
25
|
- app_name
|
|
60
26
|
- env
|
|
61
27
|
- roots
|
|
62
|
-
-
|
|
63
|
-
|
|
64
|
-
|
|
28
|
+
- files
|
|
29
|
+
|
|
30
|
+
roots and files specified will be added to the default list but with higher priority.
|
|
31
|
+
|
|
65
32
|
## Example
|
|
66
33
|
|
|
67
34
|
``` ruby
|
|
68
|
-
|
|
69
|
-
let (:sample_hash) {
|
|
70
|
-
|
|
71
|
-
h= {
|
|
72
|
-
ignored: "this will be ignored because it's not mentioned",
|
|
73
|
-
to_be_renamed: :to_be_renamed,
|
|
74
|
-
integer_coercion: "1",
|
|
75
|
-
bad_number: '1aaa2',
|
|
76
|
-
array_with_delim: '1|2|3',
|
|
77
|
-
hash_as_string: "{a => 1,b => 2,c => d}",
|
|
78
|
-
proc_validation: "is_this_valid?",
|
|
79
|
-
some_number: 122,
|
|
80
|
-
some_string: 'this is a test string',
|
|
81
|
-
is_true: 'true',
|
|
82
|
-
is_false: 'f',
|
|
83
|
-
recursive: {}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
let (:validations) {
|
|
88
|
-
v={
|
|
89
|
-
doesnt_exist: {required: true},
|
|
90
|
-
to_be_renamed: {as: :renamed},
|
|
91
|
-
no_value: {default: 1},
|
|
92
|
-
#proc default relying on previously set value
|
|
93
|
-
proc_default: {default: lambda { |o| 1 * 5 }},
|
|
94
|
-
integer_coercion: {coerce: Integer},
|
|
95
|
-
#chained coersions of various types
|
|
96
|
-
bad_number: {coerce: [lambda { |o| o.gsub('a', '') }, :to_i, Float]},
|
|
97
|
-
#arrays and hashes
|
|
98
|
-
array_with_delim: {coerce: Array, delimiter: '|'},
|
|
99
|
-
hash_as_string: {coerce: Hash, delimiter: ',', separator: '=>'},
|
|
100
|
-
proc_validation: {validate: lambda { |v| v == 'Failed_proc_validation' }},
|
|
101
|
-
#validations
|
|
102
|
-
some_number: {min: 120, max: 500, in: (100..200), is: 122},
|
|
103
|
-
some_string: {min_length: 21, max_length: 30, format: /^t.*g$/},
|
|
104
|
-
#combinations
|
|
105
|
-
missing_with_validation: {coerce: Integer, :default => 60 * 60, :validate => lambda { |v| v >= 60 * 60 }},
|
|
106
|
-
is_true: {coerce: :boolean},
|
|
107
|
-
is_false: {coerce: :boolean},
|
|
108
|
-
#recursive}
|
|
109
|
-
recursive: {
|
|
110
|
-
wasnt_here_before: {default: true}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
let (:r) {
|
|
115
|
-
HashParams.validate_hash(sample_hash, validations, {raise_errors: false})
|
|
116
|
-
}
|
|
117
|
-
|
|
35
|
+
require_relative 'spec_helper'
|
|
118
36
|
|
|
119
|
-
it 'does amazing things' do
|
|
120
|
-
|
|
121
|
-
(r.valid?).must_equal false
|
|
122
|
-
|
|
123
|
-
r[:ignored].must_be_nil
|
|
124
|
-
r[:no_value].must_equal 1
|
|
125
|
-
r[:proc_default].must_equal 5
|
|
126
|
-
r[:to_be_renamed].must_equal :to_be_renamed
|
|
127
|
-
r[:integer_coercion].must_equal 1
|
|
128
|
-
r[:bad_number].must_equal 12.0
|
|
129
|
-
|
|
130
|
-
r[:array_with_delim].must_equal ["1", "2", "3"]
|
|
131
|
-
r[:hash_as_string].must_equal ({"a" => "1", "b" => "2", "c" => "d"})
|
|
132
|
-
r[:missing_with_validation].must_equal 60 * 60
|
|
133
|
-
r[:is_true].must_equal true
|
|
134
|
-
r[:is_false].must_equal false
|
|
135
|
-
|
|
136
|
-
#recursive checking
|
|
137
|
-
r[:recursive][:wasnt_here_before].must_equal true
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
#failed items don't show up
|
|
141
|
-
r.errors.size.must_equal 2
|
|
142
|
-
r[:doesnt_exist].must_be_nil
|
|
143
|
-
r[:proc_validation].must_be_nil
|
|
144
|
-
r.errors[:doesnt_exist].must_equal 'Required Parameter missing and has no default specified'
|
|
145
|
-
r.errors[:proc_validation].must_equal 'is_this_valid? failed validation using proc'
|
|
146
|
-
|
|
147
|
-
end
|
|
148
|
-
|
|
149
|
-
it 'injects methods into returned hash' do
|
|
150
|
-
|
|
151
|
-
#set strict to false so all keys will pass trough
|
|
152
|
-
hash={will_be_injected: 12345}
|
|
153
|
-
opts={make_methods: true, strict: false}
|
|
154
|
-
|
|
155
|
-
r=HashParams.validate_hash(hash, {}, opts)
|
|
156
|
-
r[:will_be_injected].must_equal 12345
|
|
157
|
-
r.will_be_injected.must_equal 12345
|
|
158
|
-
end
|
|
159
|
-
|
|
160
|
-
end
|
|
161
37
|
|
|
162
38
|
describe HashParams do
|
|
163
|
-
let(:v) { HashParams}
|
|
39
|
+
let(:v) { HashParams }
|
|
164
40
|
|
|
165
41
|
it 'raises error if required and missing' do
|
|
166
42
|
proc {
|
|
167
|
-
v.validate(nil, required: true)
|
|
168
|
-
}.must_raise HashParams::ValidationError
|
|
43
|
+
v.validate(nil, nil, required: true)
|
|
44
|
+
}.must_raise HashParams::Validator::ValidationError
|
|
169
45
|
end
|
|
46
|
+
|
|
170
47
|
it 'runs multiple coersions' do
|
|
171
|
-
v.validate('1aaa2', coerce: [lambda { |o| o.gsub('a', '') }, :to_i
|
|
48
|
+
v.validate('1aaa2', Float, coerce: [lambda { |o| o.gsub('a', '') }, :to_i]).must_equal(12.0)
|
|
172
49
|
end
|
|
173
50
|
|
|
174
51
|
it 'defaults missing values' do
|
|
175
|
-
v.validate(nil, default: 1).must_equal
|
|
176
|
-
v.validate(nil, default: lambda {
|
|
52
|
+
v.validate(nil, Integer, default: 1).must_equal(1)
|
|
53
|
+
v.validate(nil, Integer, default: lambda { 2 * 5 }).must_equal(10)
|
|
177
54
|
end
|
|
178
55
|
|
|
179
|
-
it '
|
|
180
|
-
v.validate(nil,
|
|
56
|
+
it 'validates with lambdas' do
|
|
57
|
+
v.validate(nil, Integer, :validate => lambda { |v| v = 60 * 60 }).must_equal(60 * 60)
|
|
181
58
|
end
|
|
182
59
|
|
|
183
60
|
it 'validates with procs' do
|
|
184
|
-
|
|
185
|
-
v
|
|
186
|
-
|
|
187
|
-
end
|
|
188
|
-
}.must_raise HashParams::ValidationError
|
|
61
|
+
v.validate('is_this_valid?', String) do |v|
|
|
62
|
+
v = 'Validated with Proc'
|
|
63
|
+
end.must_equal('Validated with Proc')
|
|
189
64
|
end
|
|
190
65
|
|
|
191
66
|
it 'verifies numbers with common params' do
|
|
192
|
-
v.validate(122, min: 120, max: 500, in: (100..200), is: 122).must_equal
|
|
67
|
+
v.validate(122, Integer, min: 120, max: 500, in: (100..200), is: 122).must_equal(122)
|
|
193
68
|
end
|
|
69
|
+
|
|
194
70
|
it 'verifies strings with common params' do
|
|
195
|
-
v.validate('this is a test string', min_length: 21, max_length: 30, format: /^t.*g$/).must_equal 'this is a test string'
|
|
71
|
+
v.validate('this is a test string', String, min_length: 21, max_length: 30, format: /^t.*g$/).must_equal 'this is a test string'
|
|
196
72
|
end
|
|
197
73
|
|
|
198
74
|
it 'coerces true' do
|
|
199
|
-
v.
|
|
200
|
-
v.
|
|
201
|
-
v.
|
|
202
|
-
v.
|
|
203
|
-
v.
|
|
75
|
+
v.validate('t', :boolean).must_equal true
|
|
76
|
+
v.validate('true', :boolean).must_equal true
|
|
77
|
+
v.validate('yes', :boolean).must_equal true
|
|
78
|
+
v.validate('1', :boolean).must_equal true
|
|
79
|
+
v.validate(1, :boolean).must_equal true
|
|
204
80
|
end
|
|
81
|
+
|
|
205
82
|
it 'coerces false' do
|
|
206
|
-
v.
|
|
207
|
-
v.
|
|
208
|
-
v.
|
|
209
|
-
v.
|
|
210
|
-
v.
|
|
83
|
+
v.validate('f', :boolean).must_equal false
|
|
84
|
+
v.validate('false', :boolean).must_equal false
|
|
85
|
+
v.validate('no', :boolean).must_equal false
|
|
86
|
+
v.validate('0', :boolean).must_equal false
|
|
87
|
+
v.validate(0, :boolean).must_equal false
|
|
211
88
|
end
|
|
89
|
+
|
|
90
|
+
|
|
212
91
|
it 'coerces array' do
|
|
213
|
-
|
|
92
|
+
v.validate('1|2|3', Array, delimiter: '|').must_equal ["1", "2", "3"]
|
|
214
93
|
end
|
|
94
|
+
|
|
215
95
|
it 'coerces hash' do
|
|
216
|
-
v.
|
|
96
|
+
v.validate('{a => 1,b => 2,c => d}', Hash, delimiter: ',', separator: '=>').must_equal({"a" => "1", "b" => "2", "c" => "d"})
|
|
217
97
|
end
|
|
218
98
|
|
|
99
|
+
it 'validates a hash using a block' do
|
|
100
|
+
h = {
|
|
101
|
+
ignored: "this will be ignored because it's not mentioned",
|
|
102
|
+
to_be_renamed: :renamed_value,
|
|
103
|
+
'integer_coercion': "1",
|
|
104
|
+
proc_validation: "is_this_valid?",
|
|
105
|
+
recursive: {}
|
|
106
|
+
}
|
|
107
|
+
r = v.validate(h, Hash, symbolize_keys: true) do
|
|
108
|
+
key :doesnt_exist, nil, required: true
|
|
109
|
+
key :to_be_renamed, Symbol, as: :renamed
|
|
110
|
+
#Default Lambdas take no parameters
|
|
111
|
+
key :proc_default, Integer, default: lambda { 1 * 5 }
|
|
112
|
+
key :proc_validation, String, validate: lambda { |v| v = 'Validated in proc' }
|
|
113
|
+
#recursive
|
|
114
|
+
key :recursive, Hash do
|
|
115
|
+
key :wasnt_here_before, :boolean, default: true
|
|
116
|
+
end
|
|
117
|
+
end
|
|
219
118
|
|
|
220
|
-
|
|
119
|
+
(r.valid?).must_equal false
|
|
120
|
+
r[:ignored].must_be_nil
|
|
121
|
+
# r[:proc_default].must_equal 5
|
|
122
|
+
r[:renamed].must_equal :renamed_value
|
|
123
|
+
#recursive checking
|
|
124
|
+
r[:recursive][:wasnt_here_before].must_equal true
|
|
221
125
|
|
|
126
|
+
#failed items don't show up
|
|
127
|
+
r[:doesnt_exist].must_be_nil
|
|
128
|
+
r[:proc_validation].must_equal 'Validated in proc'
|
|
129
|
+
|
|
130
|
+
r.validation_errors.size.must_equal 1
|
|
131
|
+
r.validation_errors[0].must_equal "Error processing key 'doesnt_exist': Required Parameter missing and has no default specified"
|
|
132
|
+
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
it 'validates variables in the local binding' do
|
|
136
|
+
|
|
137
|
+
x='10'
|
|
138
|
+
y='this is a test string'
|
|
139
|
+
z={}
|
|
140
|
+
will_be_int='100'
|
|
141
|
+
|
|
142
|
+
HashParams.with_binding do
|
|
143
|
+
var :x, Integer, min: 10, max: 100
|
|
144
|
+
var :y, String, min_length: 21, max_length: 30, format: /^t.*g$/
|
|
145
|
+
var :z, Hash, raise_errors: true, symbolize_keys: true, make_methods: true do
|
|
146
|
+
key :blah, String, default: 1
|
|
147
|
+
end
|
|
148
|
+
var :will_be_int, :to_i
|
|
149
|
+
end
|
|
150
|
+
x.must_equal 10
|
|
151
|
+
y.must_equal 'this is a test string'
|
|
152
|
+
z[:blah].must_equal '1'
|
|
153
|
+
z.blah.must_equal '1'
|
|
154
|
+
will_be_int.must_equal 100
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
end
|
|
222
159
|
|
|
223
160
|
|
|
224
161
|
```
|
|
@@ -33,19 +33,26 @@ module HashParams
|
|
|
33
33
|
|
|
34
34
|
@incoming = h
|
|
35
35
|
@outgoing = HPHash.new
|
|
36
|
-
@options
|
|
36
|
+
@options = options
|
|
37
37
|
|
|
38
38
|
if block_given?
|
|
39
39
|
instance_eval(&Proc.new)
|
|
40
40
|
else
|
|
41
41
|
#no proc was given this means just pass the hash back as is
|
|
42
|
-
|
|
42
|
+
if @options.empty?
|
|
43
|
+
@outgoing = @incoming
|
|
44
|
+
else
|
|
45
|
+
h.each do |k, v|
|
|
46
|
+
key k, v.class, @options
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
43
50
|
end
|
|
44
51
|
@outgoing
|
|
45
52
|
end
|
|
46
53
|
|
|
47
54
|
def key(hash_key, type, opts={})
|
|
48
|
-
value
|
|
55
|
+
value = @incoming[hash_key] || @incoming[hash_key.to_s]
|
|
49
56
|
# if a block is given to the param then it's a recursive call
|
|
50
57
|
# recursive calls can only be done with a hash
|
|
51
58
|
new_value = if value.is_a?(Hash)
|
|
@@ -58,7 +65,7 @@ module HashParams
|
|
|
58
65
|
else
|
|
59
66
|
HashParams.validate value, type, opts
|
|
60
67
|
end
|
|
61
|
-
hash_key
|
|
68
|
+
hash_key = opts[:as] if opts[:as]
|
|
62
69
|
@outgoing.set_key_value(hash_key, new_value, @options[:symbolize_keys], @options[:make_methods])
|
|
63
70
|
new_value
|
|
64
71
|
rescue => e
|
|
@@ -66,6 +73,7 @@ module HashParams
|
|
|
66
73
|
raise e if @options[:raise_errors]
|
|
67
74
|
nil
|
|
68
75
|
end
|
|
76
|
+
|
|
69
77
|
alias_method :param, :key
|
|
70
78
|
end
|
|
71
79
|
end
|
data/lib/hash_params.rb
CHANGED
|
@@ -8,7 +8,7 @@ require_relative 'hash_params/binding_validator'
|
|
|
8
8
|
|
|
9
9
|
module HashParams
|
|
10
10
|
ENVIRONMENT = ENV['HASH_PARAMS_ENV'] || (defined?(Rails) && Rails.env) || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
|
|
11
|
-
VERSION = '2.0.
|
|
11
|
+
VERSION = '2.0.2'
|
|
12
12
|
extend HashParams::Validator
|
|
13
13
|
end
|
|
14
14
|
|
data/spec/hash_params_spec.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: hash_params
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.0.
|
|
4
|
+
version: 2.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tim Uckun
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2016-01-20 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rake
|
|
@@ -93,66 +93,12 @@ files:
|
|
|
93
93
|
- "./LICENSE"
|
|
94
94
|
- "./README.md"
|
|
95
95
|
- "./Rakefile"
|
|
96
|
-
- "./coverage/assets/0.8.0/application.css"
|
|
97
|
-
- "./coverage/assets/0.8.0/application.js"
|
|
98
|
-
- "./coverage/assets/0.8.0/colorbox/border.png"
|
|
99
|
-
- "./coverage/assets/0.8.0/colorbox/controls.png"
|
|
100
|
-
- "./coverage/assets/0.8.0/colorbox/loading.gif"
|
|
101
|
-
- "./coverage/assets/0.8.0/colorbox/loading_background.png"
|
|
102
|
-
- "./coverage/assets/0.8.0/favicon_green.png"
|
|
103
|
-
- "./coverage/assets/0.8.0/favicon_red.png"
|
|
104
|
-
- "./coverage/assets/0.8.0/favicon_yellow.png"
|
|
105
|
-
- "./coverage/assets/0.8.0/loading.gif"
|
|
106
|
-
- "./coverage/assets/0.8.0/magnify.png"
|
|
107
|
-
- "./coverage/assets/0.8.0/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png"
|
|
108
|
-
- "./coverage/assets/0.8.0/smoothness/images/ui-bg_flat_75_ffffff_40x100.png"
|
|
109
|
-
- "./coverage/assets/0.8.0/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png"
|
|
110
|
-
- "./coverage/assets/0.8.0/smoothness/images/ui-bg_glass_65_ffffff_1x400.png"
|
|
111
|
-
- "./coverage/assets/0.8.0/smoothness/images/ui-bg_glass_75_dadada_1x400.png"
|
|
112
|
-
- "./coverage/assets/0.8.0/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png"
|
|
113
|
-
- "./coverage/assets/0.8.0/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png"
|
|
114
|
-
- "./coverage/assets/0.8.0/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png"
|
|
115
|
-
- "./coverage/assets/0.8.0/smoothness/images/ui-icons_222222_256x240.png"
|
|
116
|
-
- "./coverage/assets/0.8.0/smoothness/images/ui-icons_2e83ff_256x240.png"
|
|
117
|
-
- "./coverage/assets/0.8.0/smoothness/images/ui-icons_454545_256x240.png"
|
|
118
|
-
- "./coverage/assets/0.8.0/smoothness/images/ui-icons_888888_256x240.png"
|
|
119
|
-
- "./coverage/assets/0.8.0/smoothness/images/ui-icons_cd0a0a_256x240.png"
|
|
120
|
-
- "./coverage/assets/0.9.0/application.css"
|
|
121
|
-
- "./coverage/assets/0.9.0/application.js"
|
|
122
|
-
- "./coverage/assets/0.9.0/colorbox/border.png"
|
|
123
|
-
- "./coverage/assets/0.9.0/colorbox/controls.png"
|
|
124
|
-
- "./coverage/assets/0.9.0/colorbox/loading.gif"
|
|
125
|
-
- "./coverage/assets/0.9.0/colorbox/loading_background.png"
|
|
126
|
-
- "./coverage/assets/0.9.0/favicon_green.png"
|
|
127
|
-
- "./coverage/assets/0.9.0/favicon_red.png"
|
|
128
|
-
- "./coverage/assets/0.9.0/favicon_yellow.png"
|
|
129
|
-
- "./coverage/assets/0.9.0/loading.gif"
|
|
130
|
-
- "./coverage/assets/0.9.0/magnify.png"
|
|
131
|
-
- "./coverage/assets/0.9.0/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png"
|
|
132
|
-
- "./coverage/assets/0.9.0/smoothness/images/ui-bg_flat_75_ffffff_40x100.png"
|
|
133
|
-
- "./coverage/assets/0.9.0/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png"
|
|
134
|
-
- "./coverage/assets/0.9.0/smoothness/images/ui-bg_glass_65_ffffff_1x400.png"
|
|
135
|
-
- "./coverage/assets/0.9.0/smoothness/images/ui-bg_glass_75_dadada_1x400.png"
|
|
136
|
-
- "./coverage/assets/0.9.0/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png"
|
|
137
|
-
- "./coverage/assets/0.9.0/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png"
|
|
138
|
-
- "./coverage/assets/0.9.0/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png"
|
|
139
|
-
- "./coverage/assets/0.9.0/smoothness/images/ui-icons_222222_256x240.png"
|
|
140
|
-
- "./coverage/assets/0.9.0/smoothness/images/ui-icons_2e83ff_256x240.png"
|
|
141
|
-
- "./coverage/assets/0.9.0/smoothness/images/ui-icons_454545_256x240.png"
|
|
142
|
-
- "./coverage/assets/0.9.0/smoothness/images/ui-icons_888888_256x240.png"
|
|
143
|
-
- "./coverage/assets/0.9.0/smoothness/images/ui-icons_cd0a0a_256x240.png"
|
|
144
|
-
- "./coverage/index.html"
|
|
145
96
|
- "./hash_params.gemspec"
|
|
146
97
|
- "./lib/hash_params.rb"
|
|
147
98
|
- "./lib/hash_params/binding_validator.rb"
|
|
148
99
|
- "./lib/hash_params/hash_validator.rb"
|
|
149
100
|
- "./lib/hash_params/validator.rb"
|
|
150
101
|
- "./lib/hash_params/yaml_params.rb"
|
|
151
|
-
- "./tmp/hash_params.rb"
|
|
152
|
-
- "./tmp/hash_params_spec.rb"
|
|
153
|
-
- "./tmp/module_spec.rb"
|
|
154
|
-
- "./tmp/var_spec.rb"
|
|
155
|
-
- "./tmp/yaml_params.rb"
|
|
156
102
|
- spec/hash_params_spec.rb
|
|
157
103
|
- spec/spec_helper.rb
|
|
158
104
|
homepage: https://github.com/timuckun/hash_params
|
|
@@ -175,11 +121,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
175
121
|
version: '0'
|
|
176
122
|
requirements: []
|
|
177
123
|
rubyforge_project:
|
|
178
|
-
rubygems_version: 2.4.5
|
|
124
|
+
rubygems_version: 2.4.5.1
|
|
179
125
|
signing_key:
|
|
180
126
|
specification_version: 4
|
|
181
127
|
summary: Parameter Validation & Type Coercion for parameters passed in as a Hash..
|
|
182
128
|
test_files:
|
|
183
129
|
- spec/hash_params_spec.rb
|
|
184
130
|
- spec/spec_helper.rb
|
|
185
|
-
has_rdoc:
|