typed 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/typed/schema.rb CHANGED
@@ -52,9 +52,11 @@ module Typed
52
52
  when Explicit
53
53
  raise TypeError, "%s has already been declared as `%s'" % [key, @types[key.to_s].value.inspect]
54
54
  when Implicit
55
- # update schema if same or sub-class, otherwise raises
56
- declare.value == @types[key.to_s].value or # need this hack due to bug of Must#struct?
57
- declare.value.must.struct?(@types[key.to_s].value) or
55
+ type = @types[key.to_s].value
56
+ # update schema if same or sub-class or ancestor, otherwise raises
57
+ declare.value == type or # need this hack due to bug of Must#struct?
58
+ declare.value.must.struct?(type) or # sub-class
59
+ type.must.struct?(declare.value) or # ancestor
58
60
  raise TypeError, "%s has already been typed as `%s'" % [key, @types[key.to_s].value.inspect]
59
61
  explicit(key, declare)
60
62
  else
data/lib/typed/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Typed
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
data/spec/hash_spec.rb CHANGED
@@ -136,6 +136,36 @@ describe Typed::Hash do
136
136
  }.should_not raise_error
137
137
  end
138
138
 
139
+ it "should accept atomic ancestor class (like Numeric) for its schema" do
140
+ data[:foo] = 1
141
+ data.schema(:foo).should == Fixnum # implicitly defined
142
+ lambda {
143
+ data[:foo] = Numeric
144
+ }.should_not raise_error
145
+ data.schema(:foo).should == Numeric
146
+ end
147
+
148
+ =begin
149
+ it "should accept atomic ancestor class (like Object) for its schema" do
150
+ data[:foo] = [:a]
151
+ data.schema(:foo).should == [Symbol] # implicitly defined
152
+ lambda {
153
+ data[:foo] = Object
154
+ }.should_not raise_error
155
+ data.schema(:foo).should == Object
156
+ data[:foo] = 1
157
+ data[:foo].should == 1
158
+ end
159
+ =end
160
+
161
+ it "should reject atomic non-ancestor class (like String) for its schema" do
162
+ data[:foo] = 1
163
+ data.schema(:foo).should == Fixnum # implicitly defined
164
+ lambda {
165
+ data[:foo] = String
166
+ }.should raise_error(TypeError)
167
+ end
168
+
139
169
  it "should accept complex classes (like [Fixnum]) for its schema" do
140
170
  data[:foo] = [1]
141
171
  lambda {
@@ -150,6 +180,24 @@ describe Typed::Hash do
150
180
  }.should_not raise_error
151
181
  end
152
182
 
183
+ =begin
184
+ it "should accept complex ancestor class (like {String=>Integer}) for its schema" do
185
+ data[:foo] = {"a" => 1}
186
+ data.schema(:foo).should == {String => Fixnum} # implicitly defined
187
+ lambda {
188
+ data[:foo] = {String => Integer}
189
+ }.should_not raise_error
190
+ data.schema(:foo).should == {String => Integer}
191
+ end
192
+ =end
193
+
194
+ it "should reject complex non-ancestor class (like String) for its schema" do
195
+ data[:foo] = {"a" => 1}
196
+ lambda {
197
+ data[:foo] = {String => Symbol}
198
+ }.should raise_error(TypeError)
199
+ end
200
+
153
201
  it "can override implicitly declared schema by sub-struct schema" do
154
202
  data[:foo] = {}
155
203
  data[:foo].should == {}
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typed
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 4
10
- version: 0.2.4
9
+ - 5
10
+ version: 0.2.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - maiha
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-07-02 00:00:00 Z
18
+ date: 2012-08-17 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: activesupport