typed 0.1.5 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +2 -1
- data/lib/typed/changes.rb +1 -1
- data/lib/typed/default.rb +4 -4
- data/lib/typed/hash.rb +7 -7
- data/lib/typed/schema.rb +10 -10
- data/lib/typed/version.rb +1 -1
- data/spec/changes_spec.rb +4 -4
- data/spec/events_spec.rb +2 -2
- data/spec/hash_spec.rb +13 -6
- metadata +5 -5
data/README.rdoc
CHANGED
@@ -65,7 +65,7 @@ We need some typed variables to avoid silly and stealth mistakes.
|
|
65
65
|
=> []
|
66
66
|
>> vars[:a] = 1
|
67
67
|
>> vars.changes.keys
|
68
|
-
=> [
|
68
|
+
=> ["a"]
|
69
69
|
>> vars.changes.reset
|
70
70
|
>> vars.changes.keys
|
71
71
|
=> []
|
@@ -92,6 +92,7 @@ We need some typed variables to avoid silly and stealth mistakes.
|
|
92
92
|
|
93
93
|
* Typed::Hash can't assign Class/Module cause they are treated as type definitions
|
94
94
|
* must gem adds Object#must method
|
95
|
+
* All keys are forced to String [since: verson 0.2.0]
|
95
96
|
|
96
97
|
|
97
98
|
== INSTALL:
|
data/lib/typed/changes.rb
CHANGED
data/lib/typed/default.rb
CHANGED
@@ -5,14 +5,14 @@ module Typed
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def []=(key, val)
|
8
|
-
return if @kvs.exist?(key)
|
9
|
-
@kvs[key] = val
|
8
|
+
return if @kvs.exist?(key.to_s)
|
9
|
+
@kvs[key.to_s] = val
|
10
10
|
end
|
11
11
|
|
12
12
|
def regsiter_lazy(key, block)
|
13
|
-
return if @kvs.exist?(key)
|
13
|
+
return if @kvs.exist?(key.to_s)
|
14
14
|
raise ArgumentError, "Lazy default value needs block: #{key}" unless block
|
15
|
-
@kvs[key] = Schema::LazyValue.new(block)
|
15
|
+
@kvs[key.to_s] = Schema::LazyValue.new(block)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
data/lib/typed/hash.rb
CHANGED
@@ -47,7 +47,7 @@ module Typed
|
|
47
47
|
def [](key)
|
48
48
|
if exist?(key)
|
49
49
|
val = load(key)
|
50
|
-
@events.fire(:read, key, val)
|
50
|
+
@events.fire(:read, key.to_s, val)
|
51
51
|
return val
|
52
52
|
else
|
53
53
|
from = caller.is_a?(Array) ? caller.first : self.class
|
@@ -56,8 +56,8 @@ module Typed
|
|
56
56
|
end
|
57
57
|
|
58
58
|
def update(key, val)
|
59
|
-
@hash[key] = val
|
60
|
-
@events.fire(:write, key, val)
|
59
|
+
@hash[key.to_s] = val
|
60
|
+
@events.fire(:write, key.to_s, val)
|
61
61
|
@changes.touch(key)
|
62
62
|
end
|
63
63
|
|
@@ -87,7 +87,7 @@ module Typed
|
|
87
87
|
### Testing
|
88
88
|
|
89
89
|
def exist?(key)
|
90
|
-
@hash.has_key?(key)
|
90
|
+
@hash.has_key?(key.to_s)
|
91
91
|
end
|
92
92
|
|
93
93
|
def set?(key)
|
@@ -96,7 +96,7 @@ module Typed
|
|
96
96
|
|
97
97
|
def check(key, type = nil)
|
98
98
|
type ||= @schema[key]
|
99
|
-
@schema.validate!(key, @hash[key], type)
|
99
|
+
@schema.validate!(key, @hash[key.to_s], type)
|
100
100
|
end
|
101
101
|
|
102
102
|
######################################################################
|
@@ -117,13 +117,13 @@ module Typed
|
|
117
117
|
### Utils
|
118
118
|
|
119
119
|
def inspect
|
120
|
-
keys = @hash.keys.
|
120
|
+
keys = @hash.keys.sort.join(',')
|
121
121
|
"{#{keys}}"
|
122
122
|
end
|
123
123
|
|
124
124
|
private
|
125
125
|
def load(key)
|
126
|
-
value = @hash[key]
|
126
|
+
value = @hash[key.to_s]
|
127
127
|
|
128
128
|
# LazyValue should be evaluated at runtime
|
129
129
|
return (self[key] = value.value.call) if value.is_a?(Schema::LazyValue)
|
data/lib/typed/schema.rb
CHANGED
@@ -38,34 +38,34 @@ module Typed
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def definition(key)
|
41
|
-
@types[key]
|
41
|
+
@types[key.to_s]
|
42
42
|
end
|
43
43
|
|
44
44
|
def [](key)
|
45
|
-
@types[key].value
|
45
|
+
@types[key.to_s].value
|
46
46
|
end
|
47
47
|
|
48
48
|
def declare!(key, declare)
|
49
49
|
case declare.must.be.kind_of(Explicit, Implicit)
|
50
50
|
when Explicit
|
51
|
-
case @types[key]
|
51
|
+
case @types[key.to_s]
|
52
52
|
when Explicit
|
53
|
-
raise TypeError, "%s has already been declared as `%s'" % [key, @types[key].value.inspect]
|
53
|
+
raise TypeError, "%s has already been declared as `%s'" % [key, @types[key.to_s].value.inspect]
|
54
54
|
when Implicit
|
55
55
|
# update schema if sub-class, otherwise raises
|
56
|
-
declare.value.must.struct?(@types[key].value) or
|
57
|
-
raise TypeError, "%s has already been typed as `%s'" % [key, @types[key].value.inspect]
|
56
|
+
declare.value.must.struct?(@types[key.to_s].value) or
|
57
|
+
raise TypeError, "%s has already been typed as `%s'" % [key, @types[key.to_s].value.inspect]
|
58
58
|
end
|
59
59
|
explicit(key, declare)
|
60
60
|
|
61
61
|
when Implicit
|
62
|
-
case @types[key]
|
62
|
+
case @types[key.to_s]
|
63
63
|
when Explicit
|
64
64
|
# nop
|
65
65
|
when Implicit
|
66
66
|
# update schema if sub-struct
|
67
67
|
struct = self.class.struct(declare.value)
|
68
|
-
if struct.must.struct?(@types[key].value)
|
68
|
+
if struct.must.struct?(@types[key.to_s].value)
|
69
69
|
implicit(key, struct)
|
70
70
|
end
|
71
71
|
else
|
@@ -91,12 +91,12 @@ module Typed
|
|
91
91
|
private
|
92
92
|
def implicit(key, val)
|
93
93
|
val = Implicit.new(val) unless val.is_a?(Implicit)
|
94
|
-
@types[key] = val
|
94
|
+
@types[key.to_s] = val
|
95
95
|
end
|
96
96
|
|
97
97
|
def explicit(key, val)
|
98
98
|
val = Explicit.new(val) unless val.is_a?(Explicit)
|
99
|
-
@types[key] = val
|
99
|
+
@types[key.to_s] = val
|
100
100
|
end
|
101
101
|
end
|
102
102
|
end
|
data/lib/typed/version.rb
CHANGED
data/spec/changes_spec.rb
CHANGED
@@ -8,25 +8,25 @@ describe Typed::Changes do
|
|
8
8
|
|
9
9
|
it "should return [:a] after touch(:a)" do
|
10
10
|
subject.touch(:a)
|
11
|
-
subject.keys.should == [
|
11
|
+
subject.keys.should == ["a"]
|
12
12
|
end
|
13
13
|
|
14
14
|
it "should return [:a,:b] after touch(:a) and touch(:b)" do
|
15
15
|
subject.touch(:a)
|
16
16
|
subject.touch(:b)
|
17
|
-
subject.keys.should == [
|
17
|
+
subject.keys.should == ["a", "b"]
|
18
18
|
end
|
19
19
|
|
20
20
|
it "should return [:b,:a] after touch(:b) and touch(:a)" do
|
21
21
|
subject.touch(:b)
|
22
22
|
subject.touch(:a)
|
23
|
-
subject.keys.should == [
|
23
|
+
subject.keys.should == ["b", "a"]
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should return [:a] after touch(:a) twice" do
|
27
27
|
subject.touch(:a)
|
28
28
|
subject.touch(:a)
|
29
|
-
subject.keys.should == [
|
29
|
+
subject.keys.should == ["a"]
|
30
30
|
end
|
31
31
|
|
32
32
|
it "should return [] after touch(:a) and reset" do
|
data/spec/events_spec.rb
CHANGED
@@ -13,7 +13,7 @@ describe Typed::Events do
|
|
13
13
|
|
14
14
|
data[:a]
|
15
15
|
|
16
|
-
read.should == [[
|
16
|
+
read.should == [["a",1]]
|
17
17
|
end
|
18
18
|
|
19
19
|
it "should observe :write event" do
|
@@ -28,7 +28,7 @@ describe Typed::Events do
|
|
28
28
|
|
29
29
|
data[:a] = 3
|
30
30
|
data[:c] = 5
|
31
|
-
written.should == [[
|
31
|
+
written.should == [["a",3], ["c",5]]
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should not fire :read event on :write" do
|
data/spec/hash_spec.rb
CHANGED
@@ -22,17 +22,24 @@ describe Typed::Hash do
|
|
22
22
|
data.values.sort.should == [1, 2]
|
23
23
|
end
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
25
|
+
it "should stringify keys" do
|
26
|
+
data["a"] = 1
|
27
|
+
data[:a].should == 1
|
28
|
+
data[:a] = 2
|
29
|
+
data["a"].should == 2
|
30
|
+
end
|
30
31
|
|
32
|
+
describe "#[]" do
|
31
33
|
it "should raise NotDefined if value not exists" do
|
32
34
|
lambda {
|
33
|
-
data[
|
35
|
+
data["foo"]
|
34
36
|
}.should raise_error(Typed::NotDefined)
|
35
37
|
end
|
38
|
+
|
39
|
+
it "should return value if exists" do
|
40
|
+
data["foo"] = 1
|
41
|
+
data["foo"].should == 1
|
42
|
+
end
|
36
43
|
end
|
37
44
|
|
38
45
|
describe "#[]=" do
|
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:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
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-05-
|
18
|
+
date: 2012-05-02 00:00:00 +09:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|