typed 0.2.9 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/typed/scala/accessors.rb +9 -0
- data/lib/typed/scala/builder.rb +46 -9
- data/lib/typed/version.rb +1 -1
- data/spec/scala/attrs_spec.rb +2 -2
- data/spec/scala/build_spec.rb +99 -64
- data/spec/scala/core_spec.rb +3 -2
- data/spec/scala/equal_spec.rb +50 -0
- metadata +3 -2
@@ -29,6 +29,15 @@ module Typed
|
|
29
29
|
raise Typed::NotDefined, "#{key} is not a member of #{self.class}"
|
30
30
|
end
|
31
31
|
end
|
32
|
+
|
33
|
+
def ==(other)
|
34
|
+
return false unless other.is_a?(self.class)
|
35
|
+
return __attrs__ == other.__attrs__
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_s
|
39
|
+
__attrs__.inspect
|
40
|
+
end
|
32
41
|
end
|
33
42
|
end
|
34
43
|
end
|
data/lib/typed/scala/builder.rb
CHANGED
@@ -1,16 +1,33 @@
|
|
1
1
|
module Typed
|
2
2
|
module Scala
|
3
3
|
|
4
|
-
######################################################################
|
5
|
-
### Builder
|
6
4
|
module Builder
|
5
|
+
######################################################################
|
6
|
+
### Build instances
|
7
|
+
|
8
|
+
# check hash except arg size. (valid: return hash, invalid: raised)
|
7
9
|
def check(hash)
|
8
10
|
build(hash)
|
9
11
|
return hash
|
10
12
|
end
|
11
13
|
|
12
|
-
#
|
14
|
+
# check hash strictly. (valid: return hash, invalid: raised)
|
15
|
+
def check!(hash)
|
16
|
+
build!(hash)
|
17
|
+
return hash
|
18
|
+
end
|
19
|
+
|
20
|
+
# Build instance from hash (check only types)
|
13
21
|
def build(hash)
|
22
|
+
obj = new
|
23
|
+
hash.each_pair do |k,v|
|
24
|
+
obj[k] = v if variables[k]
|
25
|
+
end
|
26
|
+
return obj
|
27
|
+
end
|
28
|
+
|
29
|
+
# Build instance from hash strictly. (check both arg size and types)
|
30
|
+
def build!(hash)
|
14
31
|
hash.must(::Hash) { raise ArgumentError, "#{self} expects Hash, but got #{hash.class}" }
|
15
32
|
|
16
33
|
if hash.size != variables.size
|
@@ -23,6 +40,7 @@ module Typed
|
|
23
40
|
raise Typed::SizeMismatch, msg
|
24
41
|
end
|
25
42
|
|
43
|
+
# 'build' just ignore unknown fields, but 'build!' raise errors
|
26
44
|
obj = new
|
27
45
|
hash.each_pair do |k,v|
|
28
46
|
obj[k] = v
|
@@ -30,12 +48,8 @@ module Typed
|
|
30
48
|
return obj
|
31
49
|
end
|
32
50
|
|
33
|
-
# Build instance from array
|
51
|
+
# Build instance from array. (check only types)
|
34
52
|
def apply(*args)
|
35
|
-
if args.size > variables.size
|
36
|
-
raise Typed::SizeMismatch, "#{self}.apply expects #{variables.size} args, but got #{args.size}"
|
37
|
-
end
|
38
|
-
|
39
53
|
obj = new
|
40
54
|
variables.each_key do |name|
|
41
55
|
val = args.shift or next
|
@@ -44,7 +58,7 @@ module Typed
|
|
44
58
|
return obj
|
45
59
|
end
|
46
60
|
|
47
|
-
# Build instance from array strictly.
|
61
|
+
# Build instance from array strictly. (check both arg size and types)
|
48
62
|
def apply!(*args)
|
49
63
|
if args.size != variables.size
|
50
64
|
raise Typed::SizeMismatch, "#{self} expects #{variables.size} args, but got #{args.size}"
|
@@ -56,6 +70,29 @@ module Typed
|
|
56
70
|
end
|
57
71
|
return obj
|
58
72
|
end
|
73
|
+
|
74
|
+
######################################################################
|
75
|
+
### Extract attrs
|
76
|
+
|
77
|
+
def attrs(obj)
|
78
|
+
obj.must(self).__attrs__
|
79
|
+
end
|
80
|
+
|
81
|
+
def unbuild(obj)
|
82
|
+
raise NotImplementedError
|
83
|
+
end
|
84
|
+
|
85
|
+
def unbuild!(obj)
|
86
|
+
raise NotImplementedError
|
87
|
+
end
|
88
|
+
|
89
|
+
def unapply(obj)
|
90
|
+
raise NotImplementedError
|
91
|
+
end
|
92
|
+
|
93
|
+
def unapply!(obj)
|
94
|
+
raise NotImplementedError
|
95
|
+
end
|
59
96
|
end
|
60
97
|
|
61
98
|
end
|
data/lib/typed/version.rb
CHANGED
data/spec/scala/attrs_spec.rb
CHANGED
@@ -26,9 +26,9 @@ describe Typed::Scala do
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
describe ".build" do
|
29
|
+
describe ".build!" do
|
30
30
|
context '(:key=>"x", :attrs=>{:a=>1})' do
|
31
|
-
subject { A.build(:key=>"x", :attrs=>{:a=>1}) }
|
31
|
+
subject { A.build!(:key=>"x", :attrs=>{:a=>1}) }
|
32
32
|
its(:key) { should == "x" }
|
33
33
|
its(:attrs) { should == {:a=>1} }
|
34
34
|
end
|
data/spec/scala/build_spec.rb
CHANGED
@@ -25,97 +25,132 @@ describe Typed::Scala do
|
|
25
25
|
its(:vars) { should be_kind_of ActiveSupport::OrderedHash }
|
26
26
|
its(:vars) { should == { "name" => String, "age" => Fixnum } }
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
28
|
+
def check ; User.check(input) ; end
|
29
|
+
def check! ; User.check!(input) ; end
|
30
|
+
def build ; User.build(input) ; end
|
31
|
+
def build! ; User.build!(input) ; end
|
32
|
+
def apply ; User.apply(*input) ; end
|
33
|
+
def apply! ; User.apply!(*input) ; end
|
34
|
+
|
35
|
+
######################################################################
|
36
|
+
### apply(array)
|
37
|
+
|
38
|
+
describe "from array" do
|
39
|
+
let(:input) { ["001", "aya", 12] }
|
40
|
+
subject { apply }
|
41
|
+
|
42
|
+
its(:key ) { should == "001" }
|
43
|
+
its(:name) { should == "aya" }
|
44
|
+
its(:age ) { should == 12 }
|
45
|
+
|
46
|
+
context "(complete)" do
|
47
|
+
specify do
|
48
|
+
expect( apply ).to be_a_kind_of(User)
|
49
|
+
expect( apply! ).to be_a_kind_of(User)
|
50
|
+
end
|
34
51
|
end
|
35
52
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
53
|
+
context "(less)" do
|
54
|
+
before { input.pop }
|
55
|
+
specify do
|
56
|
+
expect( apply ).to be_a_kind_of(User)
|
57
|
+
expect{ apply! }.to raise_error(Typed::SizeMismatch)
|
58
|
+
end
|
41
59
|
end
|
42
60
|
|
43
|
-
|
44
|
-
|
61
|
+
context "(more)" do
|
62
|
+
before { input << "!!!" }
|
63
|
+
specify do
|
64
|
+
expect( apply ).to be_a_kind_of(User)
|
65
|
+
expect{ apply! }.to raise_error(Typed::SizeMismatch)
|
66
|
+
end
|
45
67
|
end
|
46
68
|
end
|
47
69
|
|
70
|
+
# use case
|
48
71
|
describe ".apply!" do
|
49
|
-
|
50
|
-
lambda { User.apply!("001", "aya") }.should raise_error(/expects 3 args/)
|
51
|
-
end
|
52
|
-
|
53
|
-
specify "exact args" do
|
54
|
-
user = User.apply!("001", "aya", 12)
|
55
|
-
user.key .should == "001"
|
56
|
-
user.name.should == "aya"
|
57
|
-
user.age .should == 12
|
58
|
-
end
|
72
|
+
subject { User.apply!("turi", "aya", 12) }
|
59
73
|
|
60
|
-
|
61
|
-
|
62
|
-
|
74
|
+
its(:key ) { should == "turi" }
|
75
|
+
its(:name) { should == "aya" }
|
76
|
+
its(:age ) { should == 12 }
|
63
77
|
end
|
64
78
|
|
65
|
-
|
66
|
-
|
67
|
-
|
79
|
+
######################################################################
|
80
|
+
### build(hash)
|
81
|
+
|
82
|
+
context "(complete hash)" do
|
83
|
+
let(:input) { {:key=>"turi", :name=>"aya", :age=>12 } }
|
68
84
|
|
69
|
-
specify
|
70
|
-
expect(
|
85
|
+
specify do
|
86
|
+
expect( check ).to eq(input)
|
87
|
+
expect( check! ).to eq(input)
|
88
|
+
expect( build ).to be_a_kind_of(User)
|
89
|
+
expect( build! ).to be_a_kind_of(User)
|
71
90
|
end
|
91
|
+
end
|
72
92
|
|
73
|
-
|
74
|
-
|
75
|
-
|
93
|
+
context "(less hash)" do
|
94
|
+
let(:input) { {:name => "aya"} }
|
95
|
+
|
96
|
+
specify do
|
97
|
+
expect( check ).to eq(input)
|
98
|
+
expect{ check! }.to raise_error(Typed::SizeMismatch)
|
99
|
+
expect( build ).to be_a_kind_of(User)
|
100
|
+
expect{ build! }.to raise_error(Typed::SizeMismatch)
|
76
101
|
end
|
77
102
|
end
|
78
103
|
|
104
|
+
context "(more hash)" do
|
105
|
+
let(:input) { {:key=>"turi", :name=>"aya", :age=>12, :state=>true } }
|
79
106
|
|
80
|
-
|
81
|
-
|
82
|
-
expect
|
107
|
+
specify do
|
108
|
+
expect( check ).to eq(input)
|
109
|
+
expect{ check! }.to raise_error(Typed::SizeMismatch)
|
110
|
+
expect( build ).to be_a_kind_of(User)
|
111
|
+
expect{ build! }.to raise_error(Typed::SizeMismatch)
|
83
112
|
end
|
113
|
+
end
|
84
114
|
|
85
|
-
|
86
|
-
|
115
|
+
context "(no args)" do
|
116
|
+
specify do
|
117
|
+
expect{ User.check }.to raise_error(ArgumentError)
|
118
|
+
expect{ User.check! }.to raise_error(ArgumentError)
|
119
|
+
expect{ User.build }.to raise_error(ArgumentError)
|
120
|
+
expect{ User.build! }.to raise_error(ArgumentError)
|
87
121
|
end
|
122
|
+
end
|
88
123
|
|
89
|
-
|
90
|
-
|
91
|
-
|
124
|
+
# use case
|
125
|
+
describe ".build!" do
|
126
|
+
before { @hash = {:key=>"turi", :name=>"aya", :age=>12 } }
|
127
|
+
subject { User.build!(@hash) }
|
92
128
|
|
93
|
-
|
94
|
-
|
95
|
-
|
129
|
+
its(:key ) { should == "turi" }
|
130
|
+
its(:name) { should == "aya" }
|
131
|
+
its(:age ) { should == 12 }
|
96
132
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
133
|
+
specify "when less args, raise Typed::SizeMismatch" do
|
134
|
+
@hash.delete(:age)
|
135
|
+
expect { subject }.to raise_error(Typed::SizeMismatch)
|
136
|
+
expect { subject }.to raise_error(/age/)
|
137
|
+
end
|
102
138
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
139
|
+
specify "when more args, raise Typed::SizeMismatch" do
|
140
|
+
@hash[:extra] = 10
|
141
|
+
expect { subject }.to raise_error(Typed::SizeMismatch)
|
142
|
+
expect { subject }.to raise_error(/extra/)
|
143
|
+
end
|
108
144
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
145
|
+
specify "when unknown field, raise Typed::NotDefined" do
|
146
|
+
@hash.delete(:age)
|
147
|
+
@hash[:xxx] = 100
|
148
|
+
expect { subject }.to raise_error(Typed::NotDefined)
|
149
|
+
end
|
114
150
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
end
|
151
|
+
specify "when type mismatch, raise TypeError" do
|
152
|
+
@hash[:age] = "!"
|
153
|
+
expect { subject }.to raise_error(TypeError)
|
119
154
|
end
|
120
155
|
end
|
121
156
|
end
|
data/spec/scala/core_spec.rb
CHANGED
@@ -65,9 +65,10 @@ describe Typed::Scala do
|
|
65
65
|
it { should respond_to(:map) }
|
66
66
|
end
|
67
67
|
|
68
|
-
describe "
|
69
|
-
subject { User.apply!("001", "aya", 12)
|
68
|
+
describe "attributes" do
|
69
|
+
subject { User.attrs(User.apply!("001", "aya", 12)) }
|
70
70
|
|
71
|
+
its(:class ) { should == Hash }
|
71
72
|
its(:size ) { should == 3 }
|
72
73
|
its(:keys ) { should == %w( key name age ) }
|
73
74
|
its(:values) { should == ["001", "aya", 12] }
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Typed::Scala do
|
4
|
+
include_context "scala_source"
|
5
|
+
|
6
|
+
######################################################################
|
7
|
+
### Basic usage
|
8
|
+
|
9
|
+
describe "Kvs" do
|
10
|
+
before { scala_source("Kvs", <<-EOF)
|
11
|
+
class Kvs
|
12
|
+
include Typed::Scala
|
13
|
+
|
14
|
+
val key = String
|
15
|
+
var val = String
|
16
|
+
end
|
17
|
+
EOF
|
18
|
+
}
|
19
|
+
|
20
|
+
describe "#==" do
|
21
|
+
context "(different class)" do
|
22
|
+
specify do
|
23
|
+
Kvs.new.should_not == Kvs
|
24
|
+
Kvs.new.should_not == {}
|
25
|
+
Kvs.new.should_not == []
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context "(blank)" do
|
30
|
+
specify do
|
31
|
+
Kvs.new.should == Kvs.new
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "(full data)" do
|
36
|
+
specify do
|
37
|
+
Kvs.apply("x", "1").should == Kvs.apply("x", "1")
|
38
|
+
Kvs.apply("x", "1").should_not == Kvs.apply("x", "2")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "less data" do
|
43
|
+
specify do
|
44
|
+
Kvs.apply("x", "1").should_not == Kvs.apply("x")
|
45
|
+
Kvs.apply("x", "1").should_not == Kvs.new
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: typed
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
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: 2013-
|
12
|
+
date: 2013-11-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -93,6 +93,7 @@ files:
|
|
93
93
|
- spec/scala/attrs_spec.rb
|
94
94
|
- spec/scala/build_spec.rb
|
95
95
|
- spec/scala/core_spec.rb
|
96
|
+
- spec/scala/equal_spec.rb
|
96
97
|
- spec/scala/inherit_spec.rb
|
97
98
|
- spec/scala/modifier_spec.rb
|
98
99
|
- spec/scala/todo_spec.rb
|