Skip to content

Implementation of MyMongoid Lifecycle Callbacks #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ source 'https://rubygems.org'

gemspec

gem "actionpack", "~> 4.0"
#gem "actionpack", "~> 4.0"

group :test do
gem "rspec", "~> 3.0.0.beta1"
Expand Down
2 changes: 1 addition & 1 deletion lib/my_mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "my_mongoid/configuration"
require "my_mongoid/session"
require "my_mongoid/errors"
require "my_mongoid/my_callbacks"
require 'active_support/concern'

module MyMongoid
Expand All @@ -11,7 +12,6 @@ def self.models
end

def self.register_model(klass)
# why self todo
@models << klass unless models.include?(klass)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/my_mongoid/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ module Attributes
attr_accessor :attributes

def process_attributes(attr)
# adding new key in interation
attr.each do |key, val|
key_s = key.to_s
raise UnknownAttributeError unless self.class.fields[key_s]
# todo 如何调用类方法
send("#{key_s}=", val)
end
end
Expand Down
11 changes: 11 additions & 0 deletions lib/my_mongoid/callbacks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module MyMongoid
module Callbacks
extend ActiveSupport::Concern

included do
extend ActiveModel::Callbacks
define_model_callbacks :delete, :save, :create, :update
define_model_callbacks :find, :initialize, :only => :after
end
end
end
23 changes: 13 additions & 10 deletions lib/my_mongoid/document.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
require "my_mongoid/fields"
require "my_mongoid/session"
require "my_mongoid/attributes"
require "my_mongoid/callbacks"
require 'active_support/concern'
require 'active_model'

module MyMongoid

module Document
extend ActiveSupport::Concern
include Fields
include Attributes
include Callbacks

included do
MyMongoid.register_model(self)
Expand Down Expand Up @@ -44,15 +47,17 @@ def to_document
end

def save
return true unless self.changed?
if @is_new
self.class.collection.insert(self.to_document)
@is_new = false
else
self.class.collection.find({"_id" => self._id}).update(self.atomic_updates)
run_callbacks(:save) do
return true unless self.changed?
if @is_new
self.class.collection.insert(self.to_document)
@is_new = false
else
self.class.collection.find({"_id" => self._id}).update(self.atomic_updates)
end
@changed_attributes = {}
true
end
@changed_attributes = {}
true
end

def deleted?
Expand All @@ -77,8 +82,6 @@ def initialize(attributes)
raise ArgumentError, 'It is not a hash' unless attributes.is_a?(Hash)
@is_new = true
@attributes ||= {}
# todo @attributes = attributes 会导致错误
# adding new key in interation

unless attributes.key?('id') or attributes.key?('_id')
self._id = BSON::ObjectId.new
Expand Down
87 changes: 87 additions & 0 deletions lib/my_mongoid/my_callbacks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
require 'active_support/core_ext'

module MyMongoid
module MyCallbacks

def self.included(base)
base.extend ClassMethods
end

# TODO: I know it could be done by ActiveSupport::Concern. But later?
module ClassMethods
def define_callbacks(name)
class_attribute "_#{name}_callbacks"
send("_#{name}_callbacks=", CallbackChain.new)
end

def set_callback(name, kind, filter)
send("_#{name}_callbacks").append(Callback.new(filter, kind))
end

end

def run_callbacks(name)
self.class.send("_#{name}_callbacks").invoke(self) do
yield
end
end

# fancy Array, store all callbacks for one spec
class CallbackChain
attr_accessor :chain, :before_callbacks, :around_callbacks, :after_callbacks

def initialize
@chain = []
@before_callbacks = @around_callbacks = @after_callbacks = []
end

def empty?
@chain.empty?
end

def append(callback)
@chain.push callback
end

def invoke(target, &block)
_invoke(0, target, &block)
end

protected

def _invoke(i, target, &block)
if i >= @chain.length
block.call
else
# TODO: dirty?
@chain[i].invoke(target) if @chain[i].kind == :before
if @chain[i].kind == :around
@chain[i].invoke(target) do
_invoke(i+1, target, &block)
end
else
_invoke(i+1, target, &block)
end
@chain[i].invoke(target) if @chain[i].kind == :after
end
end
end

class Callback
attr_accessor :kind, :filter

# @param [Symbol] filter The callback method name.
# @param [Symbol] kind The kind of callback
def initialize(filter, kind)
@filter = filter
@kind = kind
end

def invoke(target, &block)
# TODO: String, Proc & Object Supporting
target.send(filter, &block)
end
end

end
end
2 changes: 2 additions & 0 deletions my_mongoid.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_dependency "moped", ["~> 2.0.beta6"]
spec.add_dependency("activesupport", ["~> 4.0.3"])
spec.add_dependency("activemodel", ["~> 4.0.3"])
end
122 changes: 122 additions & 0 deletions spec/my_mongoid/callbacks-02_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
require "spec_helper"

describe MyMongoid::MyCallbacks do
let(:base) {
Class.new do
include MyMongoid::MyCallbacks

define_callbacks :save

def before_1
end

def before_2
end

def save
run_callbacks(:save) {
_save
}
end

def _save
end

def to_s
"#<Foo #{object_id}>"
end
end
}



describe "run before callbacks recursively" do
let(:klass) {
Class.new(base) {
set_callback :save, :before, :before_1
set_callback :save, :before, :before_2
}
}

let(:target) {
klass.new
}

after {
target.save
}

it "should recursively call _invoke" do
expect(target._save_callbacks).to receive(:_invoke).and_call_original.exactly(3).times
end

it "should call the before methods in order" do
expect(target).to receive(:before_1).ordered
expect(target).to receive(:before_2).ordered
expect(target).to receive(:_save).ordered
end
end

describe "run after callbacks recursively" do
let(:klass) {
Class.new(base) {
set_callback :save, :after, :after_1
set_callback :save, :after, :after_2
}
}

let(:target) {
klass.new
}

after {
target.save
}

it "should call the after methods in order" do
expect(target).to receive(:_save).ordered
expect(target).to receive(:after_2).ordered
expect(target).to receive(:after_1).ordered
end

end

describe "run around callbacks recursively" do
let(:klass) {
Class.new(base) {
set_callback :save, :around, :around_1
set_callback :save, :around, :around_2

def around_1
around_1_top
yield
around_1_bottom
end

def around_2
around_2_top
yield
around_2_bottom
end
}
}

let(:target) {
klass.new
}

after {
target.save
}

it "should call the around methods in order" do
expect(target).to receive(:around_1).and_call_original.ordered
expect(target).to receive(:around_1_top).ordered
expect(target).to receive(:around_2).and_call_original.ordered
expect(target).to receive(:around_2_top).ordered
expect(target).to receive(:_save).ordered
expect(target).to receive(:around_2_bottom).ordered
expect(target).to receive(:around_1_bottom).ordered
end
end
end
Loading