myrrha 3.0.0.rc6 → 3.0.0.rc7
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +2 -0
- data/lib/myrrha/coercions.rb +7 -1
- data/lib/myrrha/errors.rb +1 -1
- data/lib/myrrha/ext/domain.rb +7 -4
- data/lib/myrrha/version.rb +1 -1
- data/myrrha.noespec +1 -1
- data/spec/coercions/test_error_handler.rb +32 -0
- metadata +4 -2
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
* Implementation of domains has been moved to the domain gem. The syntax to create domains
|
4
4
|
has therefore changed. Myrrha::Domain does no longer exists.
|
5
|
+
* An error handler can now be set on Coercions rules. The default one raises a
|
6
|
+
Myrrha::Error, preserving backward compatibility.
|
5
7
|
|
6
8
|
# 2.0.0 / 2012-09-26
|
7
9
|
|
data/lib/myrrha/coercions.rb
CHANGED
@@ -6,6 +6,9 @@ module Myrrha
|
|
6
6
|
# @return [Domain] The main target domain, if any
|
7
7
|
attr_accessor :main_target_domain
|
8
8
|
|
9
|
+
# @return [Proc] the proc that handles coercion errors
|
10
|
+
attr_accessor :error_handler
|
11
|
+
|
9
12
|
# Creates an empty list of coercion rules
|
10
13
|
def initialize(&defn)
|
11
14
|
@definitions = []
|
@@ -14,6 +17,9 @@ module Myrrha
|
|
14
17
|
@fallbacks = []
|
15
18
|
@appender = :<<
|
16
19
|
@main_target_domain = nil
|
20
|
+
@error_handler = lambda{|value, target_domain, cause|
|
21
|
+
raise Error.new("Unable to coerce `#{value}` to #{target_domain}", cause)
|
22
|
+
}
|
17
23
|
extend_rules(:<<, defn) if defn
|
18
24
|
end
|
19
25
|
|
@@ -180,7 +186,7 @@ module Myrrha
|
|
180
186
|
error = ex unless error
|
181
187
|
end
|
182
188
|
end
|
183
|
-
|
189
|
+
error_handler.call(value, target_domain, error)
|
184
190
|
end
|
185
191
|
alias :apply :coerce
|
186
192
|
|
data/lib/myrrha/errors.rb
CHANGED
data/lib/myrrha/ext/domain.rb
CHANGED
@@ -2,16 +2,19 @@ module Domain
|
|
2
2
|
module CoercionMethods
|
3
3
|
|
4
4
|
def coercions(&bl)
|
5
|
-
@coercions ||= ::Myrrha::Coercions.new{|c|
|
5
|
+
@coercions ||= ::Myrrha::Coercions.new{|c|
|
6
|
+
c.main_target_domain = self
|
7
|
+
c.error_handler = lambda{|v,t,c|
|
8
|
+
raise c if TypeError===c
|
9
|
+
domain_error!(v)
|
10
|
+
}
|
11
|
+
}
|
6
12
|
@coercions.append(&bl) if bl
|
7
13
|
@coercions
|
8
14
|
end
|
9
15
|
|
10
16
|
def coerce(arg)
|
11
17
|
coercions.coerce(arg, self)
|
12
|
-
rescue Myrrha::Error => ex
|
13
|
-
raise ex.cause if ex.cause
|
14
|
-
domain_error!(arg)
|
15
18
|
end
|
16
19
|
alias_method :[], :coerce
|
17
20
|
|
data/lib/myrrha/version.rb
CHANGED
data/myrrha.noespec
CHANGED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
module Myrrha
|
3
|
+
describe Coercions, "error_handler" do
|
4
|
+
|
5
|
+
subject{ rules.apply(12, Float) }
|
6
|
+
|
7
|
+
context 'with the default error handler' do
|
8
|
+
let(:rules){
|
9
|
+
Coercions.new
|
10
|
+
}
|
11
|
+
|
12
|
+
it "raises a Myrrha::Error when coercion fails" do
|
13
|
+
lambda{
|
14
|
+
subject
|
15
|
+
}.should raise_error(Myrrha::Error, /Unable to coerce `12` to Float/)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'with a user-defined error handler' do
|
20
|
+
let(:rules){
|
21
|
+
Coercions.new{|c|
|
22
|
+
c.error_handler = lambda{|*args| args }
|
23
|
+
}
|
24
|
+
}
|
25
|
+
|
26
|
+
it "calls the handler when coercion fails" do
|
27
|
+
subject.should eq([12, Float, nil])
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: myrrha
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.0.
|
4
|
+
version: 3.0.0.rc7
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -150,6 +150,7 @@ files:
|
|
150
150
|
- spec/coercions/test_convert.rb
|
151
151
|
- spec/coercions/test_delegate.rb
|
152
152
|
- spec/coercions/test_dup.rb
|
153
|
+
- spec/coercions/test_error_handler.rb
|
153
154
|
- spec/coercions/test_subdomain.rb
|
154
155
|
- spec/ext/domain/test_coerce.rb
|
155
156
|
- spec/ext/domain/test_coercions.rb
|
@@ -207,6 +208,7 @@ test_files:
|
|
207
208
|
- spec/coercions/test_convert.rb
|
208
209
|
- spec/coercions/test_delegate.rb
|
209
210
|
- spec/coercions/test_dup.rb
|
211
|
+
- spec/coercions/test_error_handler.rb
|
210
212
|
- spec/coercions/test_subdomain.rb
|
211
213
|
- spec/ext/domain/test_coerce.rb
|
212
214
|
- spec/ext/domain/test_coercions.rb
|