typed 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/typed/hash.rb CHANGED
@@ -56,7 +56,18 @@ module Typed
56
56
  def []=(key, val)
57
57
  if check_schema?(key)
58
58
  if @schema.exist?(key)
59
+ case val
60
+ when LazyValue
61
+ # not schema
62
+ when [], {}
63
+ # not schema cause already declared
64
+ else
65
+ struct = Must::StructInfo.new(val).compact
66
+ # when schema format, try update schema
67
+ @schema[key] = val if struct == val
68
+ end
59
69
  @schema.check!(key, val)
70
+
60
71
  else
61
72
  case val
62
73
  when LazyValue
@@ -69,6 +80,7 @@ module Typed
69
80
  else
70
81
  struct = Must::StructInfo.new(val).compact
71
82
  @schema[key] = struct
83
+ # when schema format, just declare schema and return
72
84
  return if struct == val
73
85
  end
74
86
  end
@@ -91,9 +103,9 @@ module Typed
91
103
  return @schema.check!(key, self[key]) unless type
92
104
 
93
105
  self[key].must.struct(type) {
94
- summary = self[key].inspect
95
- summary = summary[0,200] + "..." if summary.size > 200
96
- raise TypeError, "'#{key}' expects '#{type.inspect}', but got #{summary}"
106
+ got = Must::StructInfo.new(self[key]).compact.inspect
107
+ value = self[key].inspect.truncate(200)
108
+ raise TypeError, "%s(%s) got %s: %s" % [key, type.inspect, got, value]
97
109
  }
98
110
  end
99
111
 
data/lib/typed/schema.rb CHANGED
@@ -16,11 +16,12 @@ module Typed
16
16
  end
17
17
 
18
18
  def []=(key, val)
19
- if exist?(key)
20
- raise "%s is already typed as `%s'" % [key, @types[key].inspect]
21
- else
22
- @types[key] = val
23
- end
19
+ # update schema if sub-class, otherwise raises
20
+ val.must.struct(@types[key]) {
21
+ raise TypeError, "%s is already typed as `%s'" % [key, @types[key].inspect]
22
+ } if exist?(key)
23
+
24
+ @types[key] = val
24
25
  end
25
26
 
26
27
  def check!(key, val)
@@ -32,7 +33,7 @@ module Typed
32
33
  else
33
34
  expected = @types[key].inspect
34
35
  got = Must::StructInfo.new(val).compact.inspect
35
- value = val.inspect.size > 200 ? val.inspect[0,200] + "..." : val.inspect
36
+ value = val.inspect.truncate(200)
36
37
  raise TypeError, "%s(%s) got %s: %s" % [key, expected, got, value]
37
38
  end
38
39
  end
data/lib/typed/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Typed
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/spec/hash_spec.rb CHANGED
@@ -43,23 +43,7 @@ describe Typed::Hash do
43
43
 
44
44
  it "should check schema if exists" do
45
45
  data[:foo] = String
46
-
47
- lambda {
48
- data[:foo] = "foo"
49
- }.should_not raise_error(TypeError)
50
-
51
- lambda {
52
- data[:foo] = 1
53
- }.should raise_error(TypeError)
54
- end
55
-
56
- it "should check schema if exists" do
57
- data[:foo] = String
58
-
59
- lambda {
60
- data[:foo] = "foo"
61
- }.should_not raise_error(TypeError)
62
-
46
+ data[:foo] = "foo"
63
47
  lambda {
64
48
  data[:foo] = 1
65
49
  }.should raise_error(TypeError)
@@ -77,6 +61,47 @@ describe Typed::Hash do
77
61
  data[:foo] = 2
78
62
  data[:foo].should == 2
79
63
  end
64
+
65
+ it "cannot override existing value when type mismatch" do
66
+ data[:foo] = 1 # Fixnum
67
+ lambda {
68
+ data[:foo] = 0.5 # Float
69
+ }.should raise_error(TypeError)
70
+ end
71
+
72
+ it "can override existing value if declared by common ancestor" do
73
+ data[:foo] = Numeric
74
+ data[:foo] = 1
75
+ data[:foo] = 0.5
76
+ data[:foo].should == 0.5
77
+ end
78
+
79
+ it "should raise TypeError when schema given twice" do
80
+ data[:foo] = String
81
+ lambda {
82
+ data[:foo] = Integer
83
+ }.should raise_error(TypeError)
84
+ end
85
+
86
+ it "can ovreride schema when given schema is sub-class of existing one" do
87
+ data[:num] = Numeric
88
+ data[:num] = 10
89
+ data.schema(:num).should == Numeric
90
+ data[:num] = Fixnum
91
+ data.schema(:num).should == Fixnum
92
+ data[:num] = 20
93
+ data.schema(:num).should == Fixnum
94
+ end
95
+ end
96
+
97
+ ######################################################################
98
+ ### Reflection
99
+
100
+ describe "#schema(key)" do
101
+ it "should return its schema(type class)" do
102
+ data[:num] = 1
103
+ data.schema(:num).should == Fixnum
104
+ end
80
105
  end
81
106
 
82
107
  ######################################################################
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: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - maiha