parametron 0.3.4 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/README.md +19 -0
- data/lib/parametron.rb +9 -1
- data/lib/parametron/params_validator.rb +7 -1
- data/lib/parametron/version.rb +1 -1
- data/parametron.gemspec +1 -0
- data/spec/casting_spec.rb +0 -2
- data/spec/on_exception_spec.rb +38 -0
- data/spec/spec_helper.rb +1 -0
- metadata +17 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 658e94725bd642f4c1612120ed3a42ade927dc8f
|
4
|
+
data.tar.gz: f085908b94b530e83a04af25725175384c3d2f59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 291b2da26f3c6dc34611dd90d5410a38b4cc6f6f801b7fabdf5c4ad8b687a2671a57f4113079338a66bdcffd7c4434bc319b059973f63791e5f8f6e20a3acaf0
|
7
|
+
data.tar.gz: 49a7b0de9b23fb73a2ac9181689d94c255371f7fdadbc93a39e091e99f323a26f9511161ebe9398ab661c6df8fa1f0021a182399aa3882c5820147c2298c0d93
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -130,6 +130,25 @@ it will get |current_value, current_casted_parameters, original_params|
|
|
130
130
|
This could be handy when you want to have access to other parameters from input hash already
|
131
131
|
casted.
|
132
132
|
|
133
|
+
## Exceptions
|
134
|
+
|
135
|
+
Normally `parametron` raise exceptions when mandatory required parameter missed,
|
136
|
+
when `cast` expect something other than it gets and so on.
|
137
|
+
You could change this behavior if you need adding exception handler:
|
138
|
+
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
params_for(:fetch) do
|
142
|
+
required :city, validator: /\w+/
|
143
|
+
required :year, validator: /\d{4}/
|
144
|
+
on_exception ->(ex) {
|
145
|
+
# return something other then exception `ex`
|
146
|
+
return false
|
147
|
+
}
|
148
|
+
end
|
149
|
+
```
|
150
|
+
|
151
|
+
|
133
152
|
|
134
153
|
## Contributing
|
135
154
|
|
data/lib/parametron.rb
CHANGED
@@ -29,7 +29,15 @@ module Parametron
|
|
29
29
|
remove_method(name.to_sym)
|
30
30
|
|
31
31
|
define_method(name) do |params={}|
|
32
|
-
|
32
|
+
begin
|
33
|
+
new_params = _rename_params!(_cast!(_validate!(_set_defaults!(params))))
|
34
|
+
rescue => e
|
35
|
+
if self.class.params_validator.on_exception_handler
|
36
|
+
return self.class.params_validator.on_exception_handler.call(e)
|
37
|
+
else
|
38
|
+
raise e
|
39
|
+
end
|
40
|
+
end
|
33
41
|
original.bind(self).call(new_params)
|
34
42
|
end
|
35
43
|
end
|
@@ -1,12 +1,18 @@
|
|
1
1
|
class Parametron::ParamsValidator
|
2
2
|
|
3
|
-
attr_accessor :required_vals, :optional_vals
|
3
|
+
attr_accessor :required_vals, :optional_vals, :on_exception_handler
|
4
4
|
|
5
5
|
def initialize(opts)
|
6
6
|
@reject_unexpected = opts.fetch(:reject, true)
|
7
7
|
@raise_on_excess = opts.fetch(:strict, false)
|
8
8
|
self.required_vals = []
|
9
9
|
self.optional_vals = []
|
10
|
+
self.on_exception_handler = nil
|
11
|
+
end
|
12
|
+
|
13
|
+
def on_exception(lmbd)
|
14
|
+
raise ArgumentError.new('on_exception expects lambda or proc') unless lmbd.respond_to? :call
|
15
|
+
self.on_exception_handler = lmbd
|
10
16
|
end
|
11
17
|
|
12
18
|
def optional(name, opts={})
|
data/lib/parametron/version.rb
CHANGED
data/parametron.gemspec
CHANGED
data/spec/casting_spec.rb
CHANGED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Parametron, "On exception" do
|
4
|
+
|
5
|
+
it 'accepts on_exception handler' do
|
6
|
+
expect do
|
7
|
+
class VictimWithOnExceptionHandlerTest
|
8
|
+
include Parametron
|
9
|
+
params_for(:fetch, strict: true) do
|
10
|
+
required :title, validator: /\w+/
|
11
|
+
on_exception -> e {}
|
12
|
+
end
|
13
|
+
def fetch params; params; end
|
14
|
+
end
|
15
|
+
end.not_to raise_error
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'calls on_exeption handler on exception raised in params' do
|
19
|
+
class VictimWithOnExceptionHandler1
|
20
|
+
include Parametron
|
21
|
+
params_for(:fetch, strict: true) do
|
22
|
+
required :title, validator: /\w+/
|
23
|
+
on_exception -> e { [false, e] }
|
24
|
+
end
|
25
|
+
def fetch params; params; end
|
26
|
+
end
|
27
|
+
|
28
|
+
v = VictimWithOnExceptionHandler1.new
|
29
|
+
expect do
|
30
|
+
v.fetch()
|
31
|
+
end.not_to raise_error
|
32
|
+
|
33
|
+
result = v.fetch()
|
34
|
+
expect(result[0]).to eq false
|
35
|
+
expect(result[1]).to be_an_instance_of(Parametron::RequiredParamError)
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parametron
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yury Batenko
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rake
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -74,6 +88,7 @@ files:
|
|
74
88
|
- lib/parametron/version.rb
|
75
89
|
- parametron.gemspec
|
76
90
|
- spec/casting_spec.rb
|
91
|
+
- spec/on_exception_spec.rb
|
77
92
|
- spec/parametron_spec.rb
|
78
93
|
- spec/spec_helper.rb
|
79
94
|
homepage: http://github.com/svenyurgensson/parametron
|
@@ -102,5 +117,6 @@ specification_version: 4
|
|
102
117
|
summary: DSL for method arguments validation and casting
|
103
118
|
test_files:
|
104
119
|
- spec/casting_spec.rb
|
120
|
+
- spec/on_exception_spec.rb
|
105
121
|
- spec/parametron_spec.rb
|
106
122
|
- spec/spec_helper.rb
|