parametron 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- MjQ1MDg2ODZiNDM4ZDc5OWQ4NGViMmFiNzFiNjg2NjJkOWIwM2E1Yg==
5
- data.tar.gz: !binary |-
6
- NjZlYzJiMGU1YWRmNTU4NTJkMDFlODFlMjcwNDQ4M2ViZGFmZDVmZQ==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- NjY1YWQxM2U3YWY1NGM0N2U3ODk5YmQ4YmI0YTg3MmZkMTYyZjU4ZWJiM2Yx
10
- OTFmZjU1Mzg5OWRlNTU1YjQwZjU4N2JhMTJlNmFmZmUwMDUyZmM4MWVlY2Y1
11
- OTIzNTc5Y2JhNWNhMGQxODg4NDkyOTA2YjMyZjZkNzliNzYzMTA=
12
- data.tar.gz: !binary |-
13
- MWFhNzJkOGVjODcxOGVlMGYxZDE0YjNhNzYyODg3OTUxZDg0N2RiYWQ1ZjBl
14
- N2Q1YjFhNjg4NzMxOWM0N2JlMWZkZDg1ODM1ZWYzZjVkOTQ4NjU4ZmI4OTk5
15
- ZDlkZTE1MDAwMjA4MWQzMmZiNjI4NDU3NjgxYzljNGVjZmI5MjQ=
2
+ SHA1:
3
+ metadata.gz: 76fe6f2be9dd10050ee5f0749f341d7fd839f5bc
4
+ data.tar.gz: a5594be4a8b3226ba320c8ade4a84e672a92dc97
5
+ SHA512:
6
+ metadata.gz: ed8ad2f475604a05ee620885ca083463825fd26a0449f049e70146a14458764109bef90bf0f766fa9664e39b38c6c766755a35718f9194ba92ab7ad31e9d1024
7
+ data.tar.gz: 5f86ba4facefae42b5b95c5ee2e9039c794985c3df951ad7ecb0789a43015a6e23a0a7da7ec1eaf063bd429962b1e98f66b9123c8d739bcc2b14b709ab812a0d
data/README.md CHANGED
@@ -29,6 +29,7 @@ Or install it yourself as:
29
29
  optional :title, validator: ->(str){ str != "Moscow" }
30
30
  optional :number, validator: /\d+/, default: 42
31
31
  optional :gears, default: ->(obj){ choose_by(obj) }
32
+ optional :weel_d, as: :weel_diameter
32
33
  end
33
34
 
34
35
  def fetch params
data/lib/parametron.rb CHANGED
@@ -9,6 +9,10 @@ module Parametron
9
9
 
10
10
  module ClassMethods
11
11
  attr_reader :params_validator
12
+ # Declare known parameter keys
13
+ # opts [Hash]
14
+ # :strict => false ; Raise exception on unknown key
15
+ # :reject => true ; Reject unknown keys
12
16
  def params_for(method_name, opts={}, &block)
13
17
  instance_eval do
14
18
  @_method_name = method_name.to_sym
@@ -23,13 +27,22 @@ module Parametron
23
27
  original = instance_method(name.to_sym)
24
28
  remove_method(name.to_sym)
25
29
  define_method(name) do |params|
26
- new_params = _validate!(_set_defaults!(params))
30
+ new_params = _rename_params!(_validate!(_set_defaults!(params)))
27
31
  original.bind(self).call(new_params)
28
32
  end
29
33
  end
30
34
  end
31
35
 
32
36
  private
37
+ def _rename_params!(params)
38
+ new_par = params.dup
39
+ _validators_list.each do |v|
40
+ next unless v.as
41
+ new_par[v.as] = new_par.delete(v.name.to_sym) || new_par.delete(v.name.to_str)
42
+ end
43
+ new_par
44
+ end
45
+
33
46
  def _set_defaults!(params)
34
47
  new_par = params.dup
35
48
  _validators_list.each do |v|
@@ -3,7 +3,8 @@ class Parametron::ParamsValidator
3
3
  attr_accessor :required_vals, :optional_vals
4
4
 
5
5
  def initialize(opts)
6
- @raise_on_excess = opts.fetch(:strict, false)
6
+ @reject_unexpected = opts.fetch(:reject, true)
7
+ @raise_on_excess = opts.fetch(:strict, false)
7
8
  self.required_vals = []
8
9
  self.optional_vals = []
9
10
  end
@@ -11,15 +12,17 @@ class Parametron::ParamsValidator
11
12
  def optional(name, opts={})
12
13
  default = opts.delete(:default)
13
14
  validator = opts.delete(:validator)
15
+ as = opts.delete(:as)
14
16
  raise Parametron::ErrorMethodParams.new("Not available param: #{opts.inspect}") unless opts.empty?
15
- self.optional_vals << OptionalParameter.new(name.to_s, default, validator)
17
+ self.optional_vals << OptionalParameter.new(name.to_s, default, validator, as)
16
18
  end
17
19
 
18
20
  def required(name, opts={})
19
21
  default = opts.delete(:default)
20
22
  validator = opts.delete(:validator)
23
+ as = opts.delete(:as)
21
24
  raise Parametron::ErrorMethodParams.new("Not available param: #{opts.inspect}") unless opts.empty?
22
- self.required_vals << RequiredParameter.new(name.to_s, default, validator)
25
+ self.required_vals << RequiredParameter.new(name.to_s, default, validator, as)
23
26
  end
24
27
 
25
28
  def validate!(obj, params)
@@ -42,7 +45,10 @@ class Parametron::ParamsValidator
42
45
 
43
46
  params.each do |k, v|
44
47
  key = k.to_s
45
- next unless valid_keys.include?(key)
48
+ unless valid_keys.include?(key)
49
+ params.delete(key) if @reject_unexpected
50
+ next
51
+ end
46
52
  validators.find{|val| val.name == key}.tap do |curr_val|
47
53
  unless curr_val.valid?(v)
48
54
  obj.validation_error_cause << [key, v]
@@ -66,11 +72,18 @@ class Parametron::ParamsValidator
66
72
  self.required_vals.map{|x| x.name.to_s}
67
73
  end
68
74
 
69
- class GenericParameter < Struct.new(:name, :default, :validator)
75
+ class GenericParameter < Struct.new(:name, :default, :validator, :as)
76
+ def initialize(name, default, validator, as)
77
+ super
78
+ unless as.nil? || String===as || Symbol===as
79
+ raise ArgumentError.new("Parameter :as should be either String or Symbol!")
80
+ end
81
+ end
82
+
70
83
  def valid?(value)
71
84
  case self.validator
72
85
  when Regexp then value && !!self.validator.match(value.to_s)
73
- when Proc then value && !!self.validator(value).call
86
+ when Proc then value && !!self.validator(value).call
74
87
  else
75
88
  true
76
89
  end
@@ -1,3 +1,3 @@
1
1
  module Parametron
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -2,207 +2,254 @@ require 'spec_helper'
2
2
 
3
3
  describe Parametron do
4
4
 
5
- context 'Params' do
6
- context '.params' do
7
- it 'accepts params block' do
8
- expect do
9
- class Victim
10
- include Parametron
11
- params_for(:_) do; end
12
- end
13
- end.not_to raise_error
14
- end
5
+ context '.params' do
6
+ it 'accepts params block' do
7
+ expect do
8
+ class Victim
9
+ include Parametron
10
+ params_for(:_) do; end
11
+ end
12
+ end.not_to raise_error
15
13
  end
14
+ end
16
15
 
17
- context '.optional' do
18
- it 'accepts optional params' do
19
- expect do
20
- class Victim
21
- include Parametron
22
- params_for(:_) do
23
- optional :city, default: ''
24
- end
16
+ context '.optional' do
17
+ it 'accepts optional params' do
18
+ expect do
19
+ class Victim
20
+ include Parametron
21
+ params_for(:_) do
22
+ optional :city, default: 'Krasnoyarsk', as: :name
25
23
  end
26
- end.not_to raise_error
27
- end
24
+ end
25
+ end.not_to raise_error
26
+ end
28
27
 
29
- it 'accepts defaults and validator params' do
30
- expect do
31
- class Victim
32
- include Parametron
33
- params_for(:_) do
34
- optional :city, default: '', validator: /\d+/
35
- end
28
+ it 'accepts defaults and validator params' do
29
+ expect do
30
+ class Victim
31
+ include Parametron
32
+ params_for(:_) do
33
+ optional :name, default: '', validator: /\d+/
36
34
  end
37
- end.not_to raise_error
38
- end
35
+ end
36
+ end.not_to raise_error
37
+ end
39
38
 
40
- it 'store params validator' do
39
+ it 'accepts rename :as params' do
40
+ expect do
41
41
  class Victim
42
42
  include Parametron
43
43
  params_for(:_) do
44
- optional :city, default: '', validator: /\d+/
44
+ optional :city, default: '', validator: /\d+/, as: :capital
45
45
  end
46
46
  end
47
- Victim.params_validator.should be_kind_of(Parametron::ParamsValidator)
48
- Victim.params_validator.required_vals.should be_empty
49
- Victim.params_validator.optional_vals.should have(1).items
50
- end
47
+ end.not_to raise_error
48
+ end
51
49
 
52
- end # context .optional
50
+ it 'store params validator' do
51
+ class Victim
52
+ include Parametron
53
+ params_for(:_) do
54
+ optional :city, default: '', validator: /\d+/
55
+ end
56
+ end
57
+ Victim.params_validator.should be_kind_of(Parametron::ParamsValidator)
58
+ Victim.params_validator.required_vals.should be_empty
59
+ Victim.params_validator.optional_vals.should have(1).items
60
+ end
53
61
 
62
+ end # context .optional
54
63
 
55
- context '.required' do
56
- it 'accepts required params' do
57
- expect do
58
- class Victim
59
- include Parametron
60
- params_for(:_) do
61
- required :city, default: ''
62
- end
63
- end
64
- end.not_to raise_error
65
- end
66
64
 
67
- it 'accepts defaults and validator params' do
68
- expect do
69
- class Victim
70
- include Parametron
71
- params_for(:_) do
72
- required :city, default: '', validator: /\d+/
73
- end
65
+ context '.required' do
66
+ it 'accepts required params' do
67
+ expect do
68
+ class Victim
69
+ include Parametron
70
+ params_for(:_) do
71
+ required :city, default: ''
74
72
  end
75
- end.not_to raise_error
76
- end
73
+ end
74
+ end.not_to raise_error
75
+ end
77
76
 
78
- it 'store params validator' do
77
+ it 'accepts defaults and validator params' do
78
+ expect do
79
79
  class Victim
80
80
  include Parametron
81
81
  params_for(:_) do
82
82
  required :city, default: '', validator: /\d+/
83
83
  end
84
84
  end
85
- Victim.params_validator.should be_kind_of(Parametron::ParamsValidator)
86
- Victim.params_validator.optional_vals.should be_empty
87
- Victim.params_validator.required_vals.should have(1).items
85
+ end.not_to raise_error
86
+ end
87
+
88
+ it 'store params validator' do
89
+ class Victim
90
+ include Parametron
91
+ params_for(:_) do
92
+ required :city, default: '', validator: /\d+/
93
+ end
88
94
  end
95
+ Victim.params_validator.should be_kind_of(Parametron::ParamsValidator)
96
+ Victim.params_validator.optional_vals.should be_empty
97
+ Victim.params_validator.required_vals.should have(1).items
98
+ end
89
99
 
90
- end # context .required
100
+ end # context .required
91
101
 
92
102
 
93
- context '.validate' do
94
- class VictimStrict
95
- include Parametron
96
- params_for(:fetch, strict: true) do
97
- required :city, validator: /\w+/
98
- required :year, validator: /\d+/, default: 2012
99
- optional :title, validator: /\w+/
100
- optional :other, default: 'staff'
101
- end
102
- def fetch params
103
- end
103
+ context '.validate' do
104
+ class VictimStrict
105
+ include Parametron
106
+ params_for(:fetch, strict: true) do
107
+ required :city, validator: /\w+/
108
+ required :year, validator: /\d+/, default: 2012
109
+ optional :title, validator: /\w+/
110
+ optional :other, default: 'staff'
111
+ end
112
+ def fetch params
104
113
  end
105
- class VictimRelaxed
114
+ end
115
+ class VictimRelaxed
116
+ include Parametron
117
+ params_for(:fetch) do
118
+ required :city, validator: /\w+/
119
+ required :year, validator: /\d+/, default: 2012
120
+ optional :title, validator: /\w+/
121
+ optional :other, default: 'staff'
122
+ end
123
+ def fetch params
124
+ end
125
+ end
126
+
127
+ it 'reject unexpected params' do
128
+ class Victim
106
129
  include Parametron
107
130
  params_for(:fetch) do
108
- required :city, validator: /\w+/
109
- required :year, validator: /\d+/, default: 2012
110
- optional :title, validator: /\w+/
111
- optional :other, default: 'staff'
112
- end
113
- def fetch params
131
+ optional :city, default: 'Krasnoyarsk'
114
132
  end
133
+ def fetch(params); params ; end
115
134
  end
135
+ v = Victim.new
136
+ expect do
137
+ v.fetch({'_'=>'Not needed'})
138
+ end.not_to raise_error
139
+ res = v.fetch({'_'=>'Not needed'})
140
+ res.should_not have_key('_')
141
+ end
142
+
143
+ subject{ VictimStrict.new }
116
144
 
145
+ it 'accepts valid params' do
146
+ expect do
147
+ subject.fetch({city: 'Moskow', year: '1917', title: 'Nothing', other: 'Not need'})
148
+ end.not_to raise_error
149
+ end
150
+
151
+ it 'raise error when required param absent' do
152
+ expect do
153
+ subject.fetch({year: 1917, title: 'Nothing', other: 'Not need'})
154
+ end.to raise_error(Parametron::RequiredParamError)
155
+ subject.validation_error_cause.should eql [["city"]]
156
+ end
157
+
158
+ it 'raise error on unknown param' do
159
+ expect do
160
+ subject.fetch({city: 'Moskow', year: '1917', title: 'Nothing', other: 'Not need', '_' => 22})
161
+ end.to raise_error(Parametron::ExcessParameter)
162
+ end
163
+
164
+ context "when strict" do
117
165
  subject{ VictimStrict.new }
118
166
 
119
- it 'accepts valid params' do
167
+ it 'raise on unknown params' do
120
168
  expect do
121
- subject.fetch({city: 'Moskow', year: '1917', title: 'Nothing', other: 'Not need'})
122
- end.not_to raise_error
169
+ subject.fetch({city: 'Moskow', year: 1917, title: 'Nothing', other: 'Not need', invalid: 'Yes'})
170
+ end.to raise_error(Parametron::ExcessParameter)
171
+ subject.validation_error_cause.should eql [["invalid", "Yes"]]
123
172
  end
173
+ end # context strict
174
+
175
+ context "when not strict" do
176
+ subject{ VictimRelaxed.new }
124
177
 
125
- it 'raise error when required param absent' do
178
+ it 'not raise on unknown params' do
126
179
  expect do
127
- subject.fetch({year: 1917, title: 'Nothing', other: 'Not need'})
128
- end.to raise_error(Parametron::RequiredParamError)
129
- subject.validation_error_cause.should eql [["city"]]
180
+ subject.fetch({city: 'Moskow', year: 1917, title: 'Nothing', other: 'Not need', invalid: 'Yes'})
181
+ end.not_to raise_error
182
+ subject.validation_error_cause.should eql [["invalid", "Yes"]]
183
+ end
184
+ end # context not strict
185
+
186
+ end # context .validate
187
+
188
+ context 'setting defaults for parameters' do
189
+ class VictimHavingDefaults
190
+ include Parametron
191
+ params_for(:fetch, strict: true) do
192
+ required :city, validator: /\w+/
193
+ required :year, validator: /\d+/, default: '2012'
194
+ optional :title, validator: /\w+/
195
+ optional :other, default: 'staff'
130
196
  end
131
197
 
198
+ attr_reader :city, :year, :title, :other
132
199
 
133
- context "when strict" do
134
- subject{ VictimStrict.new }
135
-
136
- it 'raise on unknown params' do
137
- expect do
138
- subject.fetch({city: 'Moskow', year: 1917, title: 'Nothing', other: 'Not need', invalid: 'Yes'})
139
- end.to raise_error(Parametron::ExcessParameter)
140
- subject.validation_error_cause.should eql [["invalid", "Yes"]]
141
- end
142
- end # context strict
143
-
144
- context "when not strict" do
145
- subject{ VictimRelaxed.new }
146
-
147
- it 'not raise on unknown params' do
148
- expect do
149
- subject.fetch({city: 'Moskow', year: 1917, title: 'Nothing', other: 'Not need', invalid: 'Yes'})
150
- end.not_to raise_error
151
- subject.validation_error_cause.should eql [["invalid", "Yes"]]
152
- end
153
- end # context not strict
154
-
155
- end # context .validate
200
+ def fetch params
201
+ @city = params[:city]
202
+ @year = params[:year]
203
+ @title= params[:title]
204
+ @other= params[:other]
205
+ end
206
+ end
156
207
 
157
- context 'setting defaults for parameters' do
158
- class VictimWithDef
159
- include Parametron
160
- params_for(:fetch, strict: true) do
161
- required :city, validator: /\w+/
162
- required :year, validator: /\d+/, default: '2012'
163
- optional :title, validator: /\w+/
164
- optional :other, default: 'staff'
165
- end
166
- attr_reader :city, :year, :title, :other
208
+ let(:par){{city: 'Krasn', title: 'No way'}}
209
+ let(:par_full){{city: 'Krasn', year: '2000', title: 'No way', other: 'KillAll'}}
167
210
 
168
- def fetch params
169
- @city = params[:city]
170
- @year = params[:year]
171
- @title= params[:title]
172
- @other= params[:other]
173
- end
174
- end
211
+ it 'accepts params' do
212
+ expect do
213
+ obj = VictimHavingDefaults.new
214
+ obj.fetch(par)
215
+ end.not_to raise_error
216
+ end
175
217
 
176
- let(:par){{city: 'Krasn', title: 'No way'}}
177
- let(:par_full){{city: 'Krasn', year: '2000', title: 'No way', other: 'KillAll'}}
218
+ it 'set defaults' do
219
+ obj = VictimHavingDefaults.new
220
+ obj.fetch(par)
221
+ obj.city.should eql par[:city]
222
+ obj.title.should eql par[:title]
223
+ obj.year.should eql '2012'
224
+ obj.other.should eql 'staff'
225
+ end
178
226
 
179
- it 'accepts params' do
180
- expect do
181
- @obj = VictimWithDef.new
182
- @obj.fetch(par)
183
- end.not_to raise_error
184
- end
227
+ it 'given params have advantage over defaults' do
228
+ obj = VictimHavingDefaults.new
229
+ obj.fetch(par_full)
230
+ obj.city.should eql par_full[:city]
231
+ obj.title.should eql par_full[:title]
232
+ obj.year.should eql par_full[:year]
233
+ obj.other.should eql par_full[:other]
234
+ end
235
+ end
185
236
 
186
- it 'set defaults' do
187
- @obj = VictimWithDef.new
188
- @obj.fetch(par)
189
- @obj.city.should eql par[:city]
190
- @obj.title.should eql par[:title]
191
- @obj.year.should eql '2012'
192
- @obj.other.should eql 'staff'
237
+ context 'renames the keys' do
238
+ class VictimWithRenames
239
+ include Parametron
240
+ params_for(:fetch, strict: true) do
241
+ required :year, validator: /\d+/, default: '2012', as: :last_year
193
242
  end
194
243
 
195
- it 'given params have advantage over defaults' do
196
- @obj = VictimWithDef.new
197
- @obj.fetch(par_full)
198
- @obj.city.should eql par_full[:city]
199
- @obj.title.should eql par_full[:title]
200
- @obj.year.should eql par_full[:year]
201
- @obj.other.should eql par_full[:other]
244
+ def fetch params
245
+ params
202
246
  end
203
247
  end
204
248
 
249
+ it 'renames parameter key' do
250
+ obj = VictimWithRenames.new
251
+ obj.fetch({year: 2013}).should eql({:last_year => 2013})
252
+ end
205
253
 
206
- end # context Params
207
-
254
+ end
208
255
  end
metadata CHANGED
@@ -1,57 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parametron
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yury Batenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-25 00:00:00.000000000 Z
11
+ date: 2013-05-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
+ name: bundler
14
15
  requirement: !ruby/object:Gem::Requirement
15
16
  requirements:
16
17
  - - ~>
17
18
  - !ruby/object:Gem::Version
18
19
  version: '1.3'
20
+ type: :development
21
+ prerelease: false
19
22
  version_requirements: !ruby/object:Gem::Requirement
20
23
  requirements:
21
24
  - - ~>
22
25
  - !ruby/object:Gem::Version
23
26
  version: '1.3'
24
- type: :development
25
- prerelease: false
26
- name: bundler
27
27
  - !ruby/object:Gem::Dependency
28
+ name: rake
28
29
  requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
- - - ! '>='
31
+ - - '>='
31
32
  - !ruby/object:Gem::Version
32
33
  version: '0'
34
+ type: :development
35
+ prerelease: false
33
36
  version_requirements: !ruby/object:Gem::Requirement
34
37
  requirements:
35
- - - ! '>='
38
+ - - '>='
36
39
  - !ruby/object:Gem::Version
37
40
  version: '0'
38
- type: :development
39
- prerelease: false
40
- name: rake
41
41
  - !ruby/object:Gem::Dependency
42
+ name: rspec
42
43
  requirement: !ruby/object:Gem::Requirement
43
44
  requirements:
44
- - - ! '>='
45
+ - - '>='
45
46
  - !ruby/object:Gem::Version
46
47
  version: '0'
48
+ type: :development
49
+ prerelease: false
47
50
  version_requirements: !ruby/object:Gem::Requirement
48
51
  requirements:
49
- - - ! '>='
52
+ - - '>='
50
53
  - !ruby/object:Gem::Version
51
54
  version: '0'
52
- type: :development
53
- prerelease: false
54
- name: rspec
55
55
  description: DSL for method arguments validation
56
56
  email:
57
57
  - jurbat@gmail.com
@@ -82,12 +82,12 @@ require_paths:
82
82
  - lib
83
83
  required_ruby_version: !ruby/object:Gem::Requirement
84
84
  requirements:
85
- - - ! '>='
85
+ - - '>='
86
86
  - !ruby/object:Gem::Version
87
87
  version: '1.9'
88
88
  required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  requirements:
90
- - - ! '>='
90
+ - - '>='
91
91
  - !ruby/object:Gem::Version
92
92
  version: '0'
93
93
  requirements: []