parametric 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -1
- data/README.md +4 -2
- data/lib/parametric/params.rb +10 -2
- data/lib/parametric/policies.rb +3 -3
- data/lib/parametric/utils.rb +2 -2
- data/lib/parametric/version.rb +1 -1
- data/spec/parametric_spec.rb +12 -2
- metadata +14 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9fece5e356da755e6b7d0571b1e86b20a6291def
|
4
|
+
data.tar.gz: af78d3380fb03a0b95036d5a66345d26c6ec85aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 365b72ed5a785678d6788023972124ddfc796118426f4281da28ea1529ebb4bd38db417fbc85d0c8fe6b28bfa9482ba22d9dc8a073448450a4bdc33f92de879c
|
7
|
+
data.tar.gz: d3bc507e91bc614db0f665bff1ef33ae5af4ae77a6ab92e98befb3e90b97ddca09908fa65dfeac583e44dac5a9915421de60b02a8432c89dc48da9b959ba5bd1
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
# Parametric
|
2
|
+
[![Build Status](https://travis-ci.org/ismasan/parametric.png)](https://travis-ci.org/ismasan/parametric)
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/parametric.png)](http://badge.fury.io/rb/parametric)
|
2
4
|
|
3
5
|
DSL for declaring allowed parameters with options, regexp patern and default values.
|
4
6
|
|
@@ -124,7 +126,7 @@ search.params[:status] # => ['closed']
|
|
124
126
|
|
125
127
|
## `available_params`
|
126
128
|
|
127
|
-
`#available_params` returns the subset of keys that were populated (including defaults). Useful
|
129
|
+
`#available_params` returns the subset of keys that were populated (including defaults). Useful for building query strings.
|
128
130
|
|
129
131
|
```ruby
|
130
132
|
order_search = OrdersSearch.new(page: 2, foo: 'bar')
|
@@ -204,7 +206,7 @@ Or install it yourself as:
|
|
204
206
|
|
205
207
|
## Contributing
|
206
208
|
|
207
|
-
1. Fork it ( http://github.com
|
209
|
+
1. Fork it ( http://github.com/ismasan/parametric/fork )
|
208
210
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
209
211
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
210
212
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/lib/parametric/params.rb
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
module Parametric
|
2
2
|
|
3
|
+
class ParamsHash < Hash
|
4
|
+
def flat(separator = ',')
|
5
|
+
self.each_with_object({}) do |(k,v),memo|
|
6
|
+
memo[k] = Utils.value(v, separator)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
3
11
|
module Params
|
4
12
|
|
5
13
|
def self.included(base)
|
@@ -12,7 +20,7 @@ module Parametric
|
|
12
20
|
end
|
13
21
|
|
14
22
|
def available_params
|
15
|
-
@available_params ||= params.each_with_object(
|
23
|
+
@available_params ||= params.each_with_object(ParamsHash.new) do |(k,v),memo|
|
16
24
|
memo[k] = v if Utils.present?(v)
|
17
25
|
end
|
18
26
|
end
|
@@ -32,7 +40,7 @@ module Parametric
|
|
32
40
|
protected
|
33
41
|
|
34
42
|
def _reduce(raw_params)
|
35
|
-
self.class._allowed_params.each_with_object(
|
43
|
+
self.class._allowed_params.each_with_object(ParamsHash.new) do |(key,options),memo|
|
36
44
|
policy = Policies::Policy.new(raw_params[key], options)
|
37
45
|
policy = policy.wrap(Policies::MultiplePolicy) if options[:multiple]
|
38
46
|
policy = policy.wrap(Policies::OptionsPolicy) if options[:options]
|
data/lib/parametric/policies.rb
CHANGED
@@ -30,9 +30,9 @@ module Parametric
|
|
30
30
|
OPTION_SEPARATOR = /\s*,\s*/.freeze
|
31
31
|
|
32
32
|
def value
|
33
|
-
|
34
|
-
|
35
|
-
|
33
|
+
decorated.value.map do |v|
|
34
|
+
v.is_a?(String) ? v.split(options.fetch(:separator, OPTION_SEPARATOR)) : v
|
35
|
+
end.flatten
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
data/lib/parametric/utils.rb
CHANGED
data/lib/parametric/version.rb
CHANGED
data/spec/parametric_spec.rb
CHANGED
@@ -60,6 +60,10 @@ describe Parametric do
|
|
60
60
|
klass.new(status: 'one,three').params[:status].should == ['one', 'three']
|
61
61
|
end
|
62
62
|
|
63
|
+
it ':multiple values can be arrays' do
|
64
|
+
klass.new(status: ['one','three']).params[:status].should == ['one', 'three']
|
65
|
+
end
|
66
|
+
|
63
67
|
it 'turns :multiple separated values with custom separator into arrays' do
|
64
68
|
klass.new(piped_status: 'one|three').params[:piped_status].should == ['one', 'three']
|
65
69
|
end
|
@@ -94,15 +98,21 @@ describe Parametric do
|
|
94
98
|
end
|
95
99
|
|
96
100
|
describe '#available_params' do
|
97
|
-
let(:subject) { klass.new(foo: 'bar', name: 'lala', per_page: 20, status: 'four') }
|
101
|
+
let(:subject) { klass.new(foo: 'bar', name: 'lala', per_page: 20, status: 'four', emails: 'one@email.com,two@email.com') }
|
98
102
|
|
99
103
|
it 'only includes declared params with values or defaults' do
|
100
104
|
subject.available_params.keys.sort.should == [:emails, :name, :page, :per_page]
|
101
|
-
subject.available_params[:emails].should == [
|
105
|
+
subject.available_params[:emails].should == ['one@email.com', 'two@email.com']
|
102
106
|
subject.available_params[:name].should == 'lala'
|
103
107
|
subject.available_params[:per_page].should == 20
|
104
108
|
subject.available_params[:page].should == 1
|
105
109
|
end
|
110
|
+
|
111
|
+
describe '#flat' do
|
112
|
+
it 'joins values back' do
|
113
|
+
subject.available_params.flat[:emails].should == 'one@email.com,two@email.com'
|
114
|
+
end
|
115
|
+
end
|
106
116
|
end
|
107
117
|
|
108
118
|
describe '#schema' do
|
metadata
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parametric
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ismael Celis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.5'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.5'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
description: Useful for modelling search or form objects, white-listed query parameters
|
@@ -60,9 +60,9 @@ executables: []
|
|
60
60
|
extensions: []
|
61
61
|
extra_rdoc_files: []
|
62
62
|
files:
|
63
|
-
-
|
64
|
-
-
|
65
|
-
-
|
63
|
+
- .gitignore
|
64
|
+
- .rspec
|
65
|
+
- .travis.yml
|
66
66
|
- Gemfile
|
67
67
|
- LICENSE.txt
|
68
68
|
- README.md
|
@@ -86,17 +86,17 @@ require_paths:
|
|
86
86
|
- lib
|
87
87
|
required_ruby_version: !ruby/object:Gem::Requirement
|
88
88
|
requirements:
|
89
|
-
- -
|
89
|
+
- - '>='
|
90
90
|
- !ruby/object:Gem::Version
|
91
91
|
version: '0'
|
92
92
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- -
|
94
|
+
- - '>='
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
requirements: []
|
98
98
|
rubyforge_project:
|
99
|
-
rubygems_version: 2.
|
99
|
+
rubygems_version: 2.0.3
|
100
100
|
signing_key:
|
101
101
|
specification_version: 4
|
102
102
|
summary: DSL for declaring allowed parameters with options, regexp patern and default
|