Permalink
Browse files

Move threaded lifecycle checks and methods into their own module

  • Loading branch information...
durran committed Oct 15, 2011
1 parent 97dc925 commit ff34f0d445db29cb1ba033ac74c81268a3cee705
View
@@ -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
@@ -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.
@@ -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
@@ -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
@@ -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.
#
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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)
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
Oops, something went wrong.

0 comments on commit ff34f0d

Please sign in to comment.