hash_params 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +37 -0
- data/LICENSE +19 -0
- data/README.md +174 -0
- data/Rakefile +11 -0
- data/coverage/assets/0.8.0/application.css +799 -0
- data/coverage/assets/0.8.0/application.js +1559 -0
- 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/index.html +964 -0
- data/hash_params.gemspec +27 -0
- data/lib/hash_params.rb +157 -0
- data/spec/hash_params_spec.rb +86 -0
- data/spec/spec_helper.rb +24 -0
- metadata +152 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9139c3fc879993adc169ef1d17d5c5041e9485ee
|
4
|
+
data.tar.gz: 7d1b1803fa43616a4c413b4b0712d1a2e6d435ba
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e4cc43d988d6884e450e80829a67feb2eeb19cc48a9b3bb71dff91dde41e2db68db625ed68b05a9ec3234a496bad7c6d7164426061df2c1d8da4d26e2bb6ee7b
|
7
|
+
data.tar.gz: 103591043e96fae420e6c5457fdf91e519633bf27b3ab9025a2ff4372adb09c445ec8060ad87f70ac023c78a5462a419c3d83dc67a752abac2ae065d6cd785b5
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
hash_params (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
coderay (1.1.0)
|
10
|
+
docile (1.1.5)
|
11
|
+
method_source (0.8.2)
|
12
|
+
minitest (5.4.1)
|
13
|
+
minitest-spec (0.0.2.1)
|
14
|
+
minitest (>= 3.0)
|
15
|
+
multi_json (1.10.1)
|
16
|
+
pry (0.10.1)
|
17
|
+
coderay (~> 1.1.0)
|
18
|
+
method_source (~> 0.8.1)
|
19
|
+
slop (~> 3.4)
|
20
|
+
rake (10.3.2)
|
21
|
+
simplecov (0.9.0)
|
22
|
+
docile (~> 1.1.0)
|
23
|
+
multi_json
|
24
|
+
simplecov-html (~> 0.8.0)
|
25
|
+
simplecov-html (0.8.0)
|
26
|
+
slop (3.6.0)
|
27
|
+
|
28
|
+
PLATFORMS
|
29
|
+
ruby
|
30
|
+
|
31
|
+
DEPENDENCIES
|
32
|
+
hash_params!
|
33
|
+
minitest
|
34
|
+
minitest-spec
|
35
|
+
pry
|
36
|
+
rake
|
37
|
+
simplecov
|
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2012 Tim Uckun
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
# hash-params
|
2
|
+
_Lightweight Parameter Validation & Type Coercion_
|
3
|
+
|
4
|
+
This is a variation of the sinatra-param gem https://github.com/mattt/sinatra-param
|
5
|
+
with the sinatra specific things taken out and slight modifications to make it more useful for generic applications.
|
6
|
+
|
7
|
+
**`hash-params` allows you to declare, validate, and transform endpoint parameters as you would in frameworks like [ActiveModel](http://rubydoc.info/gems/activemodel/3.2.3/frames) or [DataMapper](http://datamapper.org/). but in a lighterweight fashion**
|
8
|
+
|
9
|
+
|
10
|
+
## Example
|
11
|
+
|
12
|
+
``` ruby
|
13
|
+
describe HashParams do
|
14
|
+
|
15
|
+
let (:r) {
|
16
|
+
HashParams.new(
|
17
|
+
{
|
18
|
+
ignored: "this will be ignored because it's not mentioned",
|
19
|
+
to_be_renamed: :to_be_renamed,
|
20
|
+
integer_coercion: "1",
|
21
|
+
bad_number: '1aaa2',
|
22
|
+
array_with_delim: '1|2|3',
|
23
|
+
hash_as_string: "{a => 1,b => 2,c => d}",
|
24
|
+
proc_validation: "is_this_valid?",
|
25
|
+
some_number: 122,
|
26
|
+
some_string: 'this is a test string' ,
|
27
|
+
is_true: 'true',
|
28
|
+
is_false: 'f',
|
29
|
+
}
|
30
|
+
) do
|
31
|
+
param :doesnt_exist, required: true
|
32
|
+
param :to_be_renamed, as: :renamed
|
33
|
+
param :no_value, default: 1
|
34
|
+
#proc default relying on previously set value
|
35
|
+
param :proc_default, default: lambda { |o| o[:no_value] * 5 }
|
36
|
+
param :integer_coercion, coerce: Integer
|
37
|
+
#chained coersions of various types
|
38
|
+
param :bad_number, coerce: [lambda { |o| o.gsub('a', '') }, :to_i, Float]
|
39
|
+
#arrays and hashes
|
40
|
+
param :array_with_delim, coerce: Array, delimiter: '|'
|
41
|
+
param :hash_as_string, coerce: Hash, delimiter: ',', separator: '=>'
|
42
|
+
param :proc_validation, validate: lambda { |v| v == 'Failed_proc_validation' }
|
43
|
+
#validations
|
44
|
+
param :some_number, min: 120, max: 500, in: (100..200), is: 122
|
45
|
+
param :some_string, min_length: 21, max_length: 30, format: /^t.*g$/
|
46
|
+
#combinations
|
47
|
+
param :missing_with_validation, coerce: Integer, :default => 60 * 60, :validate => lambda { |v| v >= 60 * 60 }
|
48
|
+
param :is_true, coerce: :boolean
|
49
|
+
param :is_false, coerce: :boolean
|
50
|
+
end
|
51
|
+
}
|
52
|
+
|
53
|
+
|
54
|
+
it 'does amazing things' do
|
55
|
+
(r.valid?).must_equal false
|
56
|
+
r[:ignored].must_be_nil
|
57
|
+
r[:no_value].must_equal 1
|
58
|
+
r[:proc_default].must_equal 5
|
59
|
+
r[:renamed].must_equal :to_be_renamed
|
60
|
+
r[:integer_coercion].must_equal 1
|
61
|
+
r[:bad_number].must_equal 12.0
|
62
|
+
#no deep coersion
|
63
|
+
r[:array_with_delim].must_equal ["1", "2", "3"]
|
64
|
+
r[:hash_as_string].must_equal ({ "a" => "1", "b" => "2", "c" => "d" })
|
65
|
+
r[:missing_with_validation].must_equal 60 * 60
|
66
|
+
r[:is_true].must_equal true
|
67
|
+
r[:is_false].must_equal false
|
68
|
+
|
69
|
+
#failed items don't show up
|
70
|
+
r.errors.size.must_equal 2
|
71
|
+
r[:doesnt_exist].must_be_nil
|
72
|
+
r[:proc_validation].must_be_nil
|
73
|
+
r.errors[0].must_equal 'Parameter doesnt_exist is required and missing'
|
74
|
+
r.errors[1].must_equal 'is_this_valid? failed validation using proc'
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'injects into current class' do
|
79
|
+
r = HashParams.new({will_be_injected: 12345}, self) do
|
80
|
+
param :will_be_injected
|
81
|
+
end
|
82
|
+
r[:will_be_injected].must_equal 12345
|
83
|
+
@will_be_injected.must_equal 12345
|
84
|
+
will_be_injected.must_equal 12345
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
```
|
91
|
+
|
92
|
+
|
93
|
+
### Defaults
|
94
|
+
|
95
|
+
Passing a `default` option will provide a default value for a parameter if none is passed. A `default` can defined as either a default or as a `Proc`:
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
param :attribution, String, default: "©"
|
99
|
+
param :year, Integer, default: lambda { Time.now.year }
|
100
|
+
```
|
101
|
+
|
102
|
+
|
103
|
+
### Coercions
|
104
|
+
|
105
|
+
By declaring parameter types, incoming parameters will automatically be coerced into an object of that type.
|
106
|
+
|
107
|
+
- `String`
|
108
|
+
- `Integer`
|
109
|
+
- `Float`
|
110
|
+
- `Array` _("1,2,3,4,5")_
|
111
|
+
- `Hash` _(key1:value1,key2:value2)_
|
112
|
+
- `Date`, `Time`, & `DateTime`
|
113
|
+
|
114
|
+
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.
|
115
|
+
|
116
|
+
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`)
|
117
|
+
- `:boolean` _("1/0", "true/false", "t/f", "yes/no", "y/n")_
|
118
|
+
|
119
|
+
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
|
120
|
+
|
121
|
+
|
122
|
+
### Validations
|
123
|
+
|
124
|
+
Encapsulate business logic in a consistent way with validations. If a parameter does not satisfy a particular condition the parameter is not added to the result and an entry is made into the errors collection.
|
125
|
+
|
126
|
+
- `required`
|
127
|
+
- `blank`
|
128
|
+
- `is`
|
129
|
+
- `in`, `within`, `range`
|
130
|
+
- `min` / `max`
|
131
|
+
- `format`
|
132
|
+
|
133
|
+
**Only valid entries are returned, all others are ignored**
|
134
|
+
|
135
|
+
|
136
|
+
### Exceptions and Validation Failures
|
137
|
+
|
138
|
+
All recorded validation errors, coercion errors, and exceptions are in the errors collection of the returned object. You can also call the valid? method to check to see if everything went OK.
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
p = HashParams.new({a: :b}) do
|
142
|
+
param :a
|
143
|
+
end
|
144
|
+
|
145
|
+
p.errors.inspect unless p.valid?
|
146
|
+
```
|
147
|
+
|
148
|
+
### Injection into classes
|
149
|
+
|
150
|
+
You can if you choose inject all passing variables (but not valid? or errors collection) into a given class. The injection is done via `attr_accessor` The values are injected into the singleton so no need to worry about polluting other objects.
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
it 'injects into current class' do
|
154
|
+
r = HashParams.new({will_be_injected: 12345}, self) do
|
155
|
+
param :will_be_injected
|
156
|
+
end
|
157
|
+
r[:will_be_injected].must_equal 12345
|
158
|
+
@will_be_injected.must_equal 12345
|
159
|
+
will_be_injected.must_equal 12345
|
160
|
+
end
|
161
|
+
```
|
162
|
+
|
163
|
+
|
164
|
+
## Contact
|
165
|
+
|
166
|
+
Tim Uckun
|
167
|
+
|
168
|
+
- http://github.com/timuckun
|
169
|
+
- http://twitter.com/timuckun
|
170
|
+
- tim@uckun.com
|
171
|
+
|
172
|
+
## License
|
173
|
+
|
174
|
+
hash-parameters is available under the MIT license. See the LICENSE file for more info.
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require "bundler"
|
2
|
+
Bundler.setup
|
3
|
+
|
4
|
+
specfile='hash-params.gemspec'
|
5
|
+
gemspec = eval(File.read(specfile))
|
6
|
+
|
7
|
+
task :build => "#{gemspec.full_name}.gem"
|
8
|
+
|
9
|
+
file "#{gemspec.full_name}.gem" => gemspec.files + [specfile] do
|
10
|
+
system "gem build #{specfile}"
|
11
|
+
end
|