typed 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -56,6 +56,9 @@ We need some typed variables to avoid silly and stealth mistakes.
56
56
  >> vars[:services] = {22 => {:tcp => "ssh"}}
57
57
  TypeError: services({Integer=>[{Symbol=>String}]}) got {Fixnum=>{Symbol=>String}}: {22=>{:tcp=>"ssh"}}
58
58
 
59
+
60
+ == Changes
61
+
59
62
  # changes object tells whether a key is changed or not
60
63
  >> vars = Typed::Hash.new
61
64
  >> vars.changes.keys
@@ -68,6 +71,17 @@ We need some typed variables to avoid silly and stealth mistakes.
68
71
  => []
69
72
 
70
73
 
74
+ == Events
75
+
76
+ # events allows user to use callback about :read,:write
77
+ >> vars = Typed::Hash.new
78
+ >> vars[:a] = 1
79
+ >> vars.events.on(:read){|k,v| puts "reading #{k}"}
80
+ >> vars[:a]
81
+ reading a
82
+ => 1
83
+
84
+
71
85
  == REQUIREMENTS:
72
86
 
73
87
  * activesupport gem
@@ -0,0 +1,21 @@
1
+ module Typed
2
+ class Events
3
+ def initialize
4
+ @callbacks = {}
5
+ end
6
+
7
+ def on(type, &block)
8
+ event(type) << block
9
+ end
10
+
11
+ def fire(type, key, val)
12
+ event(type).each{|block| block.call(key,val)}
13
+ end
14
+
15
+ private
16
+ def event(type)
17
+ @callbacks[type] ||= []
18
+ end
19
+ end
20
+ end
21
+
data/lib/typed/hash.rb CHANGED
@@ -8,6 +8,7 @@ module Typed
8
8
 
9
9
  delegate :keys, :to=>"@hash"
10
10
  attr_reader :changes
11
+ attr_reader :events
11
12
 
12
13
  def initialize(options = {})
13
14
  @hash = {}
@@ -15,6 +16,7 @@ module Typed
15
16
  @schema = Schema.new
16
17
  @default = Default.new(self)
17
18
  @changes = Changes.new
19
+ @events = Events.new
18
20
  end
19
21
 
20
22
  ######################################################################
@@ -44,7 +46,9 @@ module Typed
44
46
 
45
47
  def [](key)
46
48
  if exist?(key)
47
- return load(key)
49
+ val = load(key)
50
+ @events.fire(:read, key, val)
51
+ return val
48
52
  else
49
53
  from = caller.is_a?(Array) ? caller.first : self.class
50
54
  raise NotDefined, "'#{key}' is not initialized\n#{from}"
@@ -53,6 +57,7 @@ module Typed
53
57
 
54
58
  def update(key, val)
55
59
  @hash[key] = val
60
+ @events.fire(:write, key, val)
56
61
  @changes.touch(key)
57
62
  end
58
63
 
data/lib/typed/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Typed
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
data/lib/typed.rb CHANGED
@@ -9,5 +9,6 @@ module Typed
9
9
  autoload :Schema , "typed/schema"
10
10
  autoload :Default, "typed/default"
11
11
  autoload :Changes, "typed/changes"
12
+ autoload :Events , "typed/events"
12
13
  end
13
14
 
@@ -0,0 +1,34 @@
1
+ require "spec_helper"
2
+
3
+ describe Typed::Events do
4
+ it "should observe :read event" do
5
+ data = Typed::Hash.new
6
+ data[:a] = 1
7
+ data[:b] = 2
8
+
9
+ read = []
10
+ data.events.on(:read) {|k,v|
11
+ read << [k,v]
12
+ }
13
+
14
+ data[:a]
15
+
16
+ read.should == [[:a,1]]
17
+ end
18
+
19
+ it "should observe :write event" do
20
+ data = Typed::Hash.new
21
+ data[:a] = 1
22
+ data[:b] = 2
23
+
24
+ written = []
25
+ data.events.on(:write) {|k,v|
26
+ written << [k,v]
27
+ }
28
+
29
+ data[:a] = 3
30
+ data[:c] = 5
31
+ written.should == [[:a,3], [:c,5]]
32
+ end
33
+ end
34
+
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: 29
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 3
10
- version: 0.1.3
9
+ - 4
10
+ version: 0.1.4
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-04-26 00:00:00 +09:00
18
+ date: 2012-04-27 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -79,10 +79,12 @@ files:
79
79
  - lib/typed.rb
80
80
  - lib/typed/changes.rb
81
81
  - lib/typed/default.rb
82
+ - lib/typed/events.rb
82
83
  - lib/typed/hash.rb
83
84
  - lib/typed/schema.rb
84
85
  - lib/typed/version.rb
85
86
  - spec/changes_spec.rb
87
+ - spec/events_spec.rb
86
88
  - spec/hash_spec.rb
87
89
  - spec/schema_spec.rb
88
90
  - spec/spec_helper.rb