Skip to content
Browse files

Move threaded lifecycle checks and methods into their own module

  • Loading branch information...
1 parent 97dc925 commit ff34f0d445db29cb1ba033ac74c81268a3cee705 @durran durran committed
View
4 lib/mongoid.rb
@@ -28,10 +28,10 @@
require "active_support/time_with_zone"
require "active_model"
require "mongo"
-require "mongoid/errors"
require "mongoid/extensions"
-require "mongoid/relations"
+require "mongoid/errors"
require "mongoid/threaded"
+require "mongoid/relations"
require "mongoid/atomic"
require "mongoid/attributes"
require "mongoid/callbacks"
View
21 lib/mongoid/attributes.rb
@@ -175,27 +175,6 @@ def apply_defaults
end
end
- # Begin the assignment of attributes. While in this block embedded
- # documents will not autosave themselves in order to allow the document to
- # be in a valid state.
- #
- # @example Execute the assignment.
- # _assigning do
- # person.attributes = { :addresses => [ address ] }
- # end
- #
- # @return [ Object ] The yielded value.
- #
- # @since 2.2.0
- def _assigning
- begin
- Threaded.begin_assign
- yield
- ensure
- Threaded.exit_assign
- end
- end
-
# Used for allowing accessor methods for dynamic attributes.
#
# @param [ String, Symbol ] name The name of the method.
View
1 lib/mongoid/components.rb
@@ -39,6 +39,7 @@ module Components #:nodoc
include Mongoid::Serialization
include Mongoid::Sharding
include Mongoid::State
+ include Mongoid::Threaded::Lifecycle
include Mongoid::Timestamps::Timeless
include Mongoid::Validations
include Mongoid::Callbacks
View
23 lib/mongoid/persistence.rb
@@ -168,7 +168,7 @@ module ClassMethods #:nodoc:
#
# @return [ Document ] The newly created document.
def create(attributes = {}, options = {}, &block)
- creating do
+ _creating do
new(attributes, options, &block).tap { |doc| doc.save }
end
end
@@ -187,7 +187,7 @@ def create(attributes = {}, options = {}, &block)
#
# @return [ Document ] The newly created document.
def create!(attributes = {}, options = {}, &block)
- creating do
+ _creating do
new(attributes, options, &block).tap do |doc|
fail_validate!(doc) if doc.insert.errors.any?
fail_callback!(doc, :create!) if doc.new?
@@ -259,25 +259,6 @@ def fail_validate!(document)
def fail_callback!(document, method)
raise Errors::Callback.new(document.class, method)
end
-
- private
-
- # Execute a block in creating mode.
- #
- # @example Execute in creating mode.
- # creating do
- # relation.push(doc)
- # end
- #
- # @return [ Object ] The return value of the block.
- #
- # @since 2.1.0
- def creating
- Threaded.begin_create
- yield
- ensure
- Threaded.exit_create
- end
end
end
end
View
31 lib/mongoid/relations/binding.rb
@@ -4,36 +4,9 @@ module Relations #:nodoc:
# Superclass for all objects that bind relations together.
class Binding
- attr_reader :base, :target, :metadata
-
- # Execute a block in binding mode.
- #
- # @example Execute in binding mode.
- # binding do
- # relation.push(doc)
- # end
- #
- # @return [ Object ] The return value of the block.
- #
- # @since 2.1.0
- def binding
- Threaded.begin_bind
- yield
- ensure
- Threaded.exit_bind
- end
+ include Threaded::Lifecycle
- # Is the current thread in binding mode?
- #
- # @example Is the thread in binding mode?
- # binding.binding?
- #
- # @return [ true, false ] If the thread is binding.
- #
- # @since 2.1.0
- def binding?
- Threaded.binding?
- end
+ attr_reader :base, :target, :metadata
# Create the new binding.
#
View
8 lib/mongoid/relations/bindings/embedded/in.rb
@@ -26,8 +26,8 @@ class In < Binding
def bind
base.metadata = metadata.inverse_metadata(target)
base.parentize(target)
- unless binding?
- binding do
+ unless _binding?
+ _binding do
if base.embedded_many?
target.do_or_do_not(metadata.inverse(target)).push(base)
else
@@ -51,8 +51,8 @@ def bind
#
# @since 2.0.0.rc.1
def unbind
- unless binding?
- binding do
+ unless _binding?
+ _binding do
if base.embedded_many?
target.do_or_do_not(metadata.inverse(target)).delete(base)
else
View
8 lib/mongoid/relations/bindings/embedded/many.rb
@@ -42,8 +42,8 @@ def bind
# @since 2.0.0.rc.1
def bind_one(doc)
doc.parentize(base)
- unless binding?
- binding do
+ unless _binding?
+ _binding do
unless metadata.versioned?
doc.do_or_do_not(metadata.inverse_setter(target), base)
end
@@ -80,8 +80,8 @@ def unbind
#
# @since 2.0.0.rc.1
def unbind_one(doc)
- unless binding?
- binding do
+ unless _binding?
+ _binding do
doc.do_or_do_not(metadata.inverse_setter(target), nil)
end
end
View
8 lib/mongoid/relations/bindings/embedded/one.rb
@@ -25,8 +25,8 @@ class One < Binding
# @since 2.0.0.rc.1
def bind
target.parentize(base)
- unless binding?
- binding do
+ unless _binding?
+ _binding do
target.do_or_do_not(metadata.inverse_setter(target), base)
end
end
@@ -47,8 +47,8 @@ def bind
#
# @since 2.0.0.rc.1
def unbind
- unless binding?
- binding do
+ unless _binding?
+ _binding do
target.do_or_do_not(metadata.inverse_setter(target), nil)
end
end
View
8 lib/mongoid/relations/bindings/referenced/in.rb
@@ -19,8 +19,8 @@ class In < Binding
#
# @since 2.0.0.rc.1
def bind
- unless binding?
- binding do
+ unless _binding?
+ _binding do
inverse = metadata.inverse(target)
base.you_must(metadata.foreign_key_setter, target.id)
if metadata.inverse_type
@@ -51,8 +51,8 @@ def bind
#
# @since 2.0.0.rc.1
def unbind
- unless binding?
- binding do
+ unless _binding?
+ _binding do
inverse = metadata.inverse(target)
base.you_must(metadata.foreign_key_setter, nil)
if metadata.inverse_type
View
8 lib/mongoid/relations/bindings/referenced/many.rb
@@ -17,8 +17,8 @@ class Many < Binding
#
# @since 2.0.0.rc.1
def bind_one(doc)
- unless binding?
- binding do
+ unless _binding?
+ _binding do
doc.you_must(metadata.foreign_key_setter, base.id)
if metadata.type
doc.you_must(metadata.type_setter, base.class.model_name)
@@ -37,8 +37,8 @@ def bind_one(doc)
#
# @since 2.0.0.rc.1
def unbind_one(doc)
- unless binding?
- binding do
+ unless _binding?
+ _binding do
doc.you_must(metadata.foreign_key_setter, nil)
if metadata.type
doc.you_must(metadata.type_setter, nil)
View
8 lib/mongoid/relations/bindings/referenced/many_to_many.rb
@@ -17,8 +17,8 @@ class ManyToMany < Binding
#
# @since 2.0.0.rc.1
def bind_one(doc)
- unless binding?
- binding do
+ unless _binding?
+ _binding do
inverse_keys = doc.you_must(metadata.inverse_foreign_key)
inverse_keys.push(base.id) if inverse_keys
base.synced[metadata.foreign_key] = true
@@ -34,8 +34,8 @@ def bind_one(doc)
#
# @since 2.0.0.rc.1
def unbind_one(doc)
- unless binding?
- binding do
+ unless _binding?
+ _binding do
base.send(metadata.foreign_key).delete_one(doc.id)
inverse_keys = doc.you_must(metadata.inverse_foreign_key)
inverse_keys.delete_one(base.id) if inverse_keys
View
8 lib/mongoid/relations/bindings/referenced/one.rb
@@ -19,8 +19,8 @@ class One < Binding
#
# @since 2.0.0.rc.1
def bind
- unless binding?
- binding do
+ unless _binding?
+ _binding do
target.you_must(metadata.foreign_key_setter, base.id)
target.send(metadata.inverse_setter, base)
if metadata.type
@@ -40,8 +40,8 @@ def bind
#
# @since 2.0.0.rc.1
def unbind
- unless binding?
- binding do
+ unless _binding?
+ _binding do
target.you_must(metadata.foreign_key_setter, nil)
target.send(metadata.inverse_setter, nil)
if metadata.type
View
17 lib/mongoid/relations/builders.rb
@@ -32,23 +32,6 @@ module Relations #:nodoc:
module Builders
extend ActiveSupport::Concern
- # Execute a block in building mode.
- #
- # @example Execute in building mode.
- # _building do
- # relation.push(doc)
- # end
- #
- # @return [ Object ] The return value of the block.
- #
- # @since 2.1.0
- def _building
- Threaded.begin_build
- yield
- ensure
- Threaded.exit_build
- end
-
module ClassMethods #:nodoc:
# Defines a builder method for an embeds_one relation. This is
View
2 lib/mongoid/relations/embedded/in.rb
@@ -89,7 +89,7 @@ def characterize_one(document)
#
# @since 2.1.0
def persistable?
- target.persisted? && !binding? && !_building?
+ target.persisted? && !_binding? && !_building?
end
class << self
View
4 lib/mongoid/relations/embedded/many.rb
@@ -145,7 +145,7 @@ def create!(attributes = {}, options = {}, type = nil, &block)
# @since 2.0.0.rc.1
def delete(document)
target.delete_one(document).tap do |doc|
- if doc && !binding?
+ if doc && !_binding?
if _assigning?
base.add_atomic_pull(doc)
else
@@ -390,7 +390,7 @@ def method_missing(name, *args, &block)
#
# @since 2.1.0
def persistable?
- base.persisted? && !binding?
+ base.persisted? && !_binding?
end
# Reindex all the target elements. This is useful when performing
View
2 lib/mongoid/relations/embedded/one.rb
@@ -73,7 +73,7 @@ def binding
#
# @since 2.1.0
def persistable?
- base.persisted? && !binding? && !_building? && !_assigning?
+ base.persisted? && !_binding? && !_building? && !_assigning?
end
class << self
View
49 lib/mongoid/relations/proxy.rb
@@ -5,6 +5,7 @@ module Relations #:nodoc:
# This class is the superclass for all relation proxy objects, and contains
# common behaviour for all of them.
class Proxy
+ include Threaded::Lifecycle
# We undefine most methods to get them sent through to the target.
instance_methods.each do |method|
@@ -50,54 +51,6 @@ def substitutable
protected
- # Is the current thread in assigning mode?
- #
- # @example Is the current thread in assigning mode?
- # proxy._assigning?
- #
- # @return [ true, false ] If the thread is assigning.
- #
- # @since 2.1.0
- def _assigning?
- Threaded.assigning?
- end
-
- # Is the current thread in binding mode?
- #
- # @example Is the current thread in binding mode?
- # proxy.binding?
- #
- # @return [ true, false ] If the thread is binding.
- #
- # @since 2.1.0
- def binding?
- Threaded.binding?
- end
-
- # Is the current thread in building mode?
- #
- # @example Is the current thread in building mode?
- # proxy._building?
- #
- # @return [ true, false ] If the thread is building.
- #
- # @since 2.1.0
- def _building?
- Threaded.building?
- end
-
- # Is the current thread in creating mode?
- #
- # @example Is the current thread in creating mode?
- # proxy.creating?
- #
- # @return [ true, false ] If the thread is creating.
- #
- # @since 2.1.0
- def creating?
- Threaded.creating?
- end
-
# Get the collection from the root of the hierarchy.
#
# @example Get the collection.
View
2 lib/mongoid/relations/referenced/in.rb
@@ -71,7 +71,7 @@ def binding
#
# @since 2.1.0
def persistable?
- target.persisted? && !binding? && !_building?
+ target.persisted? && !_binding? && !_building?
end
class << self
View
2 lib/mongoid/relations/referenced/many.rb
@@ -408,7 +408,7 @@ def method_missing(name, *args, &block)
#
# @since 2.1.0
def persistable?
- creating? || base.persisted? && !binding? && !_building?
+ _creating? || base.persisted? && !_binding? && !_building?
end
# Deletes all related documents from the database given the supplied
View
4 lib/mongoid/relations/referenced/many_to_many.rb
@@ -30,7 +30,7 @@ def <<(*args)
args.flatten.each do |doc|
next unless doc
append(doc)
- if persistable? || creating?
+ if persistable? || _creating?
ids.push(doc.id)
doc.save
else
@@ -38,7 +38,7 @@ def <<(*args)
base.synced[metadata.foreign_key] = false
end
end
- if persistable? || creating?
+ if persistable? || _creating?
base.push_all(metadata.foreign_key, ids)
base.synced[metadata.foreign_key] = false
end
View
2 lib/mongoid/relations/referenced/one.rb
@@ -82,7 +82,7 @@ def binding
#
# @since 2.1.0
def persistable?
- base.persisted? && !binding? && !_building?
+ base.persisted? && !_binding? && !_building?
end
class << self
View
2 lib/mongoid/threaded.rb
@@ -1,4 +1,6 @@
# encoding: utf-8
+require "mongoid/threaded/lifecycle"
+
module Mongoid #:nodoc:
# This module contains logic for easy access to objects that have a lifecycle
View
134 lib/mongoid/threaded/lifecycle.rb
@@ -0,0 +1,134 @@
+# encoding: utf-8
+module Mongoid #:nodoc:
+ module Threaded #:nodoc:
+
+ # This module contains convenience methods for document lifecycle that
+ # resides on thread locals.
+ module Lifecycle
+ extend ActiveSupport::Concern
+
+ private
+
+ # Begin the assignment of attributes. While in this block embedded
+ # documents will not autosave themselves in order to allow the document to
+ # be in a valid state.
+ #
+ # @example Execute the assignment.
+ # _assigning do
+ # person.attributes = { :addresses => [ address ] }
+ # end
+ #
+ # @return [ Object ] The yielded value.
+ #
+ # @since 2.2.0
+ def _assigning
+ Threaded.begin_assign
+ yield
+ ensure
+ Threaded.exit_assign
+ end
+
+ # Is the current thread in assigning mode?
+ #
+ # @example Is the current thread in assigning mode?
+ # proxy._assigning?
+ #
+ # @return [ true, false ] If the thread is assigning.
+ #
+ # @since 2.1.0
+ def _assigning?
+ Threaded.assigning?
+ end
+
+ # Execute a block in binding mode.
+ #
+ # @example Execute in binding mode.
+ # binding do
+ # relation.push(doc)
+ # end
+ #
+ # @return [ Object ] The return value of the block.
+ #
+ # @since 2.1.0
+ def _binding
+ Threaded.begin_bind
+ yield
+ ensure
+ Threaded.exit_bind
+ end
+
+ # Is the current thread in binding mode?
+ #
+ # @example Is the current thread in binding mode?
+ # proxy.binding?
+ #
+ # @return [ true, false ] If the thread is binding.
+ #
+ # @since 2.1.0
+ def _binding?
+ Threaded.binding?
+ end
+
+ # Execute a block in building mode.
+ #
+ # @example Execute in building mode.
+ # _building do
+ # relation.push(doc)
+ # end
+ #
+ # @return [ Object ] The return value of the block.
+ #
+ # @since 2.1.0
+ def _building
+ Threaded.begin_build
+ yield
+ ensure
+ Threaded.exit_build
+ end
+
+ # Is the current thread in building mode?
+ #
+ # @example Is the current thread in building mode?
+ # proxy._building?
+ #
+ # @return [ true, false ] If the thread is building.
+ #
+ # @since 2.1.0
+ def _building?
+ Threaded.building?
+ end
+
+ # Is the current thread in creating mode?
+ #
+ # @example Is the current thread in creating mode?
+ # proxy.creating?
+ #
+ # @return [ true, false ] If the thread is creating.
+ #
+ # @since 2.1.0
+ def _creating?
+ Threaded.creating?
+ end
+
+ module ClassMethods #:nodoc:
+
+ # Execute a block in creating mode.
+ #
+ # @example Execute in creating mode.
+ # creating do
+ # relation.push(doc)
+ # end
+ #
+ # @return [ Object ] The return value of the block.
+ #
+ # @since 2.1.0
+ def _creating
+ Threaded.begin_create
+ yield
+ ensure
+ Threaded.exit_create
+ end
+ end
+ end
+ end
+end

0 comments on commit ff34f0d

Please sign in to comment.
Something went wrong with that request. Please try again.