Commit 5434c92fa0f25f041e76d350f9012000d5e82d3a
- Date: Sun Feb 17 18:32:26 +0000 2008
- Committer: Yurii Rashkovskii (yrashk@gmail.com)
- Author: Yurii Rashkovskii (yrashk@gmail.com)
- Commit SHA1: 5434c92fa0f25f041e76d350f9012000d5e82d3a
- Tree SHA1: 206c45a747a73da6f7b7f1bcbee902265e4bfa52
Minor callback mechanism refactoring; bugfix
Commit diff
| |   |
| 15 | 15 | [ ] Improve PointQuery (I really don't like it) |
| 16 | 16 | [ ] Refactor Meta#document, fix bugs |
| 17 | 17 | [ ] 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 |
| 19 | 19 | [ ] "Valid Document's JSON with multiple meta names specified" spec should check callback through message sending, not instance vars |
| 20 | 20 | [ ] Drop :marshal serialization method or figure out why it is not really working now (run test/playing2.rb with :marshal and see) |
| 21 | 21 | [ ] Add #[index] API to Document::Versions, with negative indexes as well. |
| toggle raw diff |
--- a/strokedb-ruby/TODO
+++ b/strokedb-ruby/TODO
@@ -15,7 +15,7 @@ Completed items are removed.
[ ] Improve PointQuery (I really don't like it)
[ ] Refactor Meta#document, fix bugs
[ ] Think about non-index named meta lookup
-[.] Refactor callback stuff in Document — call on_initialization only for specified meta (add param to execute_callbacks?)
+[.] Refactor callback stuff in Document
[ ] "Valid Document's JSON with multiple meta names specified" spec should check callback through message sending, not instance vars
[ ] Drop :marshal serialization method or figure out why it is not really working now (run test/playing2.rb with :marshal and see)
[ ] Add #[index] API to Document::Versions, with negative indexes as well. |
| |   |
| 81 | 81 | if _module |
| 82 | 82 | @document.extend(_module) |
| 83 | 83 | _module.send!(:setup_callbacks,@document) rescue nil |
| 84 | | @document.send!(:execute_callbacks, :on_initialization) |
| 84 | @document.send!(:execute_callbacks_for, _module, :on_initialization) |
| 85 | 85 | end |
| 86 | 86 | @document[:__meta__] = self |
| 87 | 87 | end |
| … | … | |
| 278 | 278 | callback.call(self) |
| 279 | 279 | end |
| 280 | 280 | 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 | |
| 282 | 288 | def do_initialize(store, slots={}, uuid=nil) |
| 283 | 289 | @callbacks = {} |
| 284 | 290 | @store = store |
| toggle raw diff |
--- a/strokedb-ruby/lib/document/document.rb
+++ b/strokedb-ruby/lib/document/document.rb
@@ -81,7 +81,7 @@ module StrokeDB
if _module
@document.extend(_module)
_module.send!(:setup_callbacks,@document) rescue nil
- @document.send!(:execute_callbacks, :on_initialization)
+ @document.send!(:execute_callbacks_for, _module, :on_initialization)
end
@document[:__meta__] = self
end
@@ -278,7 +278,13 @@ module StrokeDB
callback.call(self)
end
end
-
+
+ def execute_callbacks_for(origin,name)
+ (callbacks[name.to_s]||[]).each do |callback|
+ callback.call(self) if callback.origin == origin
+ end
+ end
+
def do_initialize(store, slots={}, uuid=nil)
@callbacks = {}
@store = store |
| |   |
| 1 | 1 | module StrokeDB |
| 2 | 2 | |
| 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 | |
| 3 | 13 | module Meta |
| 4 | 14 | |
| 5 | 15 | class << self |
| … | … | |
| 122 | 122 | def setup_callbacks(doc) |
| 123 | 123 | CALLBACKS.each do |callback_name| |
| 124 | 124 | if callback = instance_variable_get("@#{callback_name}_block") |
| 125 | callback = Callback.new(self,&callback) |
| 125 | 126 | doc.callbacks[callback_name] ||= [] |
| 126 | 127 | doc.callbacks[callback_name] << callback |
| 127 | 128 | end |
| toggle raw diff |
--- a/strokedb-ruby/lib/document/meta.rb
+++ b/strokedb-ruby/lib/document/meta.rb
@@ -1,5 +1,15 @@
module StrokeDB
+ class Callback
+ attr_reader :origin
+ def initialize(origin,&block)
+ @origin, @block = origin, block
+ end
+ def call(*args)
+ @block.call(*args)
+ end
+ end
+
module Meta
class << self
@@ -112,6 +122,7 @@ module StrokeDB
def setup_callbacks(doc)
CALLBACKS.each do |callback_name|
if callback = instance_variable_get("@#{callback_name}_block")
+ callback = Callback.new(self,&callback)
doc.callbacks[callback_name] ||= []
doc.callbacks[callback_name] << callback
end |
| |   |
| 112 | 112 | end |
| 113 | 113 | end |
| 114 | 114 | |
| 115 | | it "should call callback block on meta instantiation" do |
| 115 | it "should receive this callback on meta instantiation" do |
| 116 | 116 | Kernel.should_receive(:on_initialization_called).with(true) |
| 117 | | s = SomeName.new |
| 117 | doc = SomeName.new |
| 118 | 118 | 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 | |
| 120 | 133 | end |
| 121 | 134 | |
| 122 | 135 | describe "Meta module with before_save callback" do |
| … | … | |
| 146 | 146 | end |
| 147 | 147 | end |
| 148 | 148 | |
| 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 |
| 150 | 150 | Kernel.should_receive(:before_save_called).with(true) |
| 151 | 151 | s = SomeName.new |
| 152 | 152 | s.save! |
| … | … | |
| 168 | 168 | end |
| 169 | 169 | end |
| 170 | 170 | |
| 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 |
| 172 | 172 | Kernel.should_receive(:after_save_called).with(false) |
| 173 | 173 | s = SomeName.new |
| 174 | 174 | s.save! |
| toggle raw diff |
--- a/strokedb-ruby/spec/document/meta_spec.rb
+++ b/strokedb-ruby/spec/document/meta_spec.rb
@@ -112,11 +112,24 @@ describe "Meta module with on_initialization callback" do
end
end
- it "should call callback block on meta instantiation" do
+ it "should receive this callback on meta instantiation" do
Kernel.should_receive(:on_initialization_called).with(true)
- s = SomeName.new
+ doc = SomeName.new
end
-
+
+ it "should be a sole meta receiving this callback when adding metas dynamically" do
+ Object.send!(:remove_const,'SomeOtherName') if defined?(SomeOtherName)
+ SomeOtherName = Meta.new do
+ on_initialization do |obj|
+ Kernel.send!(:other_on_initialization_called,obj.new?)
+ end
+ end
+ Kernel.should_receive(:other_on_initialization_called).with(true).once
+ doc = SomeOtherName.new
+ Kernel.should_receive(:on_initialization_called).with(true).once
+ doc.metas << SomeName
+ end
+
end
describe "Meta module with before_save callback" do
@@ -133,7 +146,7 @@ describe "Meta module with before_save callback" do
end
end
- it "should call callback block on Document#save! (before actually saving it)" do
+ it "should receive callback on Document#save! (before actually saving it)" do
Kernel.should_receive(:before_save_called).with(true)
s = SomeName.new
s.save!
@@ -155,7 +168,7 @@ describe "Meta module with after_save callback" do
end
end
- it "should call callback block on Document#save! (after actually saving it)" do
+ it "should receive callback on Document#save! (after actually saving it)" do
Kernel.should_receive(:after_save_called).with(false)
s = SomeName.new
s.save! |