myrrha 3.0.0.rc3 → 3.0.0.rc4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/myrrha/ext/domain.rb +2 -12
- data/lib/myrrha/version.rb +1 -1
- data/myrrha.noespec +1 -1
- data/spec/ext/domain/test_coerce.rb +19 -12
- metadata +1 -1
data/lib/myrrha/ext/domain.rb
CHANGED
@@ -9,21 +9,11 @@ module Domain
|
|
9
9
|
|
10
10
|
def coerce(arg)
|
11
11
|
coercions.coerce(arg, self)
|
12
|
-
rescue Myrrha::Error
|
12
|
+
rescue Myrrha::Error => ex
|
13
13
|
domain_error!(arg)
|
14
14
|
end
|
15
|
+
alias_method :[], :coerce
|
15
16
|
|
16
|
-
def [](first = NOT_PROVIDED, *args)
|
17
|
-
if first == NOT_PROVIDED
|
18
|
-
coerce([])
|
19
|
-
elsif args.empty?
|
20
|
-
coerce(first)
|
21
|
-
else
|
22
|
-
coerce(args.unshift(first))
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
NOT_PROVIDED = Object.new
|
27
17
|
end # module CoercionMethods
|
28
18
|
include CoercionMethods
|
29
19
|
end # module Domain
|
data/lib/myrrha/version.rb
CHANGED
data/myrrha.noespec
CHANGED
@@ -1,39 +1,46 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
describe Domain, "coerce" do
|
3
3
|
|
4
|
-
|
4
|
+
class Point
|
5
|
+
extend Domain::Scalar.new(:x, :y)
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
domain.coercions do |c|
|
10
|
-
c.coercion(String){|v,_| expected }
|
11
|
-
c.coercion(Array) {|v,_| expected }
|
7
|
+
coercions do |c|
|
8
|
+
c.coercion(String){|v,_| Point.new(*v.split(',').map(&:to_i)) }
|
9
|
+
c.coercion(Array) {|v,_| Point.new(*v) }
|
12
10
|
end
|
11
|
+
|
12
|
+
EMPTY = Point.new(0, 0)
|
13
13
|
end
|
14
14
|
|
15
|
+
let(:point_1_1){ Point.new(1, 1) }
|
16
|
+
let(:point_2_2){ Point.new(2, 2) }
|
17
|
+
|
15
18
|
it 'should delegate to coercions' do
|
16
|
-
|
19
|
+
Point.coerce("1, 1").should eq(point_1_1)
|
17
20
|
end
|
18
21
|
|
19
22
|
it 'raises a TypeError if something goes wrong' do
|
20
23
|
lambda{
|
21
|
-
|
24
|
+
Point.coerce(12)
|
22
25
|
}.should raise_error(TypeError, /Can't convert `12`/)
|
23
26
|
end
|
24
27
|
|
25
28
|
describe "the [] alias" do
|
26
29
|
|
27
30
|
it 'delegates to coerce when one argument' do
|
28
|
-
|
31
|
+
Point["1,1"].should eq(point_1_1)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'supports Array' do
|
35
|
+
Point[[2, 2]].should eq(point_2_2)
|
29
36
|
end
|
30
37
|
|
31
38
|
it 'supports Array literals' do
|
32
|
-
|
39
|
+
Point[2, 2].should eq(point_2_2)
|
33
40
|
end
|
34
41
|
|
35
42
|
it 'supports empty Array literals' do
|
36
|
-
|
43
|
+
Point[].should be(Point::EMPTY)
|
37
44
|
end
|
38
45
|
end
|
39
46
|
|