typed 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +29 -3
- data/lib/typed.rb +2 -1
- data/lib/typed/changes.rb +20 -0
- data/lib/typed/hash.rb +3 -0
- data/lib/typed/version.rb +1 -1
- data/spec/changes_spec.rb +38 -0
- data/spec/hash_spec.rb +7 -0
- metadata +6 -4
data/README.rdoc
CHANGED
@@ -1,12 +1,27 @@
|
|
1
1
|
= typed
|
2
2
|
|
3
|
-
|
3
|
+
A Ruby library for Typed variables
|
4
4
|
|
5
5
|
|
6
6
|
== DESCRIPTION:
|
7
7
|
|
8
|
-
|
9
|
-
|
8
|
+
No more "NoMethodError: undefined method"!
|
9
|
+
We need some typed variables to avoid silly and stealth mistakes.
|
10
|
+
|
11
|
+
# Ruby
|
12
|
+
irb> name = "foo"
|
13
|
+
irb> name = 10
|
14
|
+
|
15
|
+
# Scala
|
16
|
+
scala> var name = "foo"
|
17
|
+
scala> name = 10
|
18
|
+
<console>:8: error: type mismatch;
|
19
|
+
|
20
|
+
# Ruby + typed.gem
|
21
|
+
irb> vars = Typed::Hash.new
|
22
|
+
irb> vars[:name] = "foo"
|
23
|
+
irb> vars[:name] = 10
|
24
|
+
TypeError: name(String) got Fixnum: 10
|
10
25
|
|
11
26
|
|
12
27
|
== SYNOPSIS:
|
@@ -41,6 +56,17 @@
|
|
41
56
|
>> vars[:services] = {22 => {:tcp => "ssh"}}
|
42
57
|
TypeError: services({Integer=>[{Symbol=>String}]}) got {Fixnum=>{Symbol=>String}}: {22=>{:tcp=>"ssh"}}
|
43
58
|
|
59
|
+
# changes object tells whether a key is changed or not
|
60
|
+
>> vars = Typed::Hash.new
|
61
|
+
>> vars.changes.keys
|
62
|
+
=> []
|
63
|
+
>> vars[:a] = 1
|
64
|
+
>> vars.changes.keys
|
65
|
+
=> [:a]
|
66
|
+
>> vars.changes.reset
|
67
|
+
>> vars.changes.keys
|
68
|
+
=> []
|
69
|
+
|
44
70
|
|
45
71
|
== REQUIREMENTS:
|
46
72
|
|
data/lib/typed.rb
CHANGED
data/lib/typed/hash.rb
CHANGED
@@ -7,12 +7,14 @@ module Typed
|
|
7
7
|
}
|
8
8
|
|
9
9
|
delegate :keys, :to=>"@hash"
|
10
|
+
attr_reader :changes
|
10
11
|
|
11
12
|
def initialize(options = {})
|
12
13
|
@hash = {}
|
13
14
|
@options = DEFAULT_OPTIONS.merge(options.must(::Hash))
|
14
15
|
@schema = Schema.new
|
15
16
|
@default = Default.new(self)
|
17
|
+
@changes = Changes.new
|
16
18
|
end
|
17
19
|
|
18
20
|
######################################################################
|
@@ -51,6 +53,7 @@ module Typed
|
|
51
53
|
|
52
54
|
def update(key, val)
|
53
55
|
@hash[key] = val
|
56
|
+
@changes.touch(key)
|
54
57
|
end
|
55
58
|
|
56
59
|
def []=(key, val)
|
data/lib/typed/version.rb
CHANGED
@@ -0,0 +1,38 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Typed::Changes do
|
4
|
+
describe "#keys" do
|
5
|
+
it "should return []" do
|
6
|
+
subject.keys.should == []
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should return [:a] after touch(:a)" do
|
10
|
+
subject.touch(:a)
|
11
|
+
subject.keys.should == [:a]
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should return [:a,:b] after touch(:a) and touch(:b)" do
|
15
|
+
subject.touch(:a)
|
16
|
+
subject.touch(:b)
|
17
|
+
subject.keys.should == [:a, :b]
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should return [:b,:a] after touch(:b) and touch(:a)" do
|
21
|
+
subject.touch(:b)
|
22
|
+
subject.touch(:a)
|
23
|
+
subject.keys.should == [:b, :a]
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should return [:a] after touch(:a) twice" do
|
27
|
+
subject.touch(:a)
|
28
|
+
subject.touch(:a)
|
29
|
+
subject.keys.should == [:a]
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return [] after touch(:a) and reset" do
|
33
|
+
subject.touch(:a)
|
34
|
+
subject.reset
|
35
|
+
subject.keys.should == []
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/spec/hash_spec.rb
CHANGED
@@ -269,5 +269,12 @@ describe Typed::Hash do
|
|
269
269
|
data[:foo].should == 2
|
270
270
|
end
|
271
271
|
end
|
272
|
+
|
273
|
+
######################################################################
|
274
|
+
### Changes
|
275
|
+
|
276
|
+
describe "#changes" do
|
277
|
+
its(:changes) {should be_kind_of(Typed::Changes)}
|
278
|
+
end
|
272
279
|
end
|
273
280
|
|
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: 29
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 3
|
10
|
+
version: 0.1.3
|
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-
|
18
|
+
date: 2012-04-26 00:00:00 +09:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -77,10 +77,12 @@ files:
|
|
77
77
|
- README.rdoc
|
78
78
|
- Rakefile
|
79
79
|
- lib/typed.rb
|
80
|
+
- lib/typed/changes.rb
|
80
81
|
- lib/typed/default.rb
|
81
82
|
- lib/typed/hash.rb
|
82
83
|
- lib/typed/schema.rb
|
83
84
|
- lib/typed/version.rb
|
85
|
+
- spec/changes_spec.rb
|
84
86
|
- spec/hash_spec.rb
|
85
87
|
- spec/schema_spec.rb
|
86
88
|
- spec/spec_helper.rb
|