Skip to content

Commit

Permalink
Move threaded lifecycle checks and methods into their own module
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Oct 15, 2011
1 parent 97dc925 commit ff34f0d
Show file tree
Hide file tree
Showing 23 changed files with 181 additions and 175 deletions.
4 changes: 2 additions & 2 deletions lib/mongoid.rb
Expand Up @@ -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"
Expand Down
21 changes: 0 additions & 21 deletions lib/mongoid/attributes.rb
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions lib/mongoid/components.rb
Expand Up @@ -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
Expand Down
23 changes: 2 additions & 21 deletions lib/mongoid/persistence.rb
Expand Up @@ -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
Expand All @@ -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?
Expand Down Expand Up @@ -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
31 changes: 2 additions & 29 deletions lib/mongoid/relations/binding.rb
Expand Up @@ -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.
#
Expand Down
8 changes: 4 additions & 4 deletions lib/mongoid/relations/bindings/embedded/in.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/mongoid/relations/bindings/embedded/many.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/mongoid/relations/bindings/embedded/one.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/mongoid/relations/bindings/referenced/in.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/mongoid/relations/bindings/referenced/many.rb
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions lib/mongoid/relations/bindings/referenced/many_to_many.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/mongoid/relations/bindings/referenced/one.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
17 changes: 0 additions & 17 deletions lib/mongoid/relations/builders.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/relations/embedded/in.rb
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/relations/embedded/many.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/relations/embedded/one.rb
Expand Up @@ -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
Expand Down

0 comments on commit ff34f0d

Please sign in to comment.