Commit 5434c92fa0f25f041e76d350f9012000d5e82d3a

Minor callback mechanism refactoring; bugfix

Commit diff

strokedb-ruby/TODO

 
1515[ ] Improve PointQuery (I really don't like it)
1616[ ] Refactor Meta#document, fix bugs
1717[ ] Think about non-index named meta lookup
18[.] Refactor callback stuff in Document — call on_initialization only for specified meta (add param to execute_callbacks?)
18[.] Refactor callback stuff in Document
1919[ ] "Valid Document's JSON with multiple meta names specified" spec should check callback through message sending, not instance vars
2020[ ] Drop :marshal serialization method or figure out why it is not really working now (run test/playing2.rb with :marshal and see)
2121[ ] Add #[index] API to Document::Versions, with negative indexes as well.
toggle raw diff

strokedb-ruby/lib/document/document.rb

 
8181 if _module
8282 @document.extend(_module)
8383 _module.send!(:setup_callbacks,@document) rescue nil
84 @document.send!(:execute_callbacks, :on_initialization)
84 @document.send!(:execute_callbacks_for, _module, :on_initialization)
8585 end
8686 @document[:__meta__] = self
8787 end
278278 callback.call(self)
279279 end
280280 end
281
281
282 def execute_callbacks_for(origin,name)
283 (callbacks[name.to_s]||[]).each do |callback|
284 callback.call(self) if callback.origin == origin
285 end
286 end
287
282288 def do_initialize(store, slots={}, uuid=nil)
283289 @callbacks = {}
284290 @store = store
toggle raw diff

strokedb-ruby/lib/document/meta.rb

 
11module StrokeDB
22
3 class Callback
4 attr_reader :origin
5 def initialize(origin,&block)
6 @origin, @block = origin, block
7 end
8 def call(*args)
9 @block.call(*args)
10 end
11 end
12
313 module Meta
414
515 class << self
122122 def setup_callbacks(doc)
123123 CALLBACKS.each do |callback_name|
124124 if callback = instance_variable_get("@#{callback_name}_block")
125 callback = Callback.new(self,&callback)
125126 doc.callbacks[callback_name] ||= []
126127 doc.callbacks[callback_name] << callback
127128 end
toggle raw diff

strokedb-ruby/spec/document/meta_spec.rb

 
112112 end
113113 end
114114
115 it "should call callback block on meta instantiation" do
115 it "should receive this callback on meta instantiation" do
116116 Kernel.should_receive(:on_initialization_called).with(true)
117 s = SomeName.new
117 doc = SomeName.new
118118 end
119
119
120 it "should be a sole meta receiving this callback when adding metas dynamically" do
121 Object.send!(:remove_const,'SomeOtherName') if defined?(SomeOtherName)
122 SomeOtherName = Meta.new do
123 on_initialization do |obj|
124 Kernel.send!(:other_on_initialization_called,obj.new?)
125 end
126 end
127 Kernel.should_receive(:other_on_initialization_called).with(true).once
128 doc = SomeOtherName.new
129 Kernel.should_receive(:on_initialization_called).with(true).once
130 doc.metas << SomeName
131 end
132
120133end
121134
122135describe "Meta module with before_save callback" do
146146 end
147147 end
148148
149 it "should call callback block on Document#save! (before actually saving it)" do
149 it "should receive callback on Document#save! (before actually saving it)" do
150150 Kernel.should_receive(:before_save_called).with(true)
151151 s = SomeName.new
152152 s.save!
168168 end
169169 end
170170
171 it "should call callback block on Document#save! (after actually saving it)" do
171 it "should receive callback on Document#save! (after actually saving it)" do
172172 Kernel.should_receive(:after_save_called).with(false)
173173 s = SomeName.new
174174 s.save!
toggle raw diff