Skip to content
Browse files

Object#tap is cool, but not that much.

- My basic perf tests show returning the object instead of tap is still
  about 20% faster on MRI, even though tap is native now.
  • Loading branch information...
1 parent 4e213f5 commit 8ce131d2f756b7bd634bcbe9359da6e5d2ca8230 @durran durran committed Apr 11, 2012
Showing with 522 additions and 523 deletions.
  1. +11 −12 lib/mongoid/atomic.rb
  2. +9 −9 lib/mongoid/attributes.rb
  3. +7 −7 lib/mongoid/config.rb
  4. +11 −15 lib/mongoid/contextual/map_reduce.rb
  5. +10 −10 lib/mongoid/contextual/memory.rb
  6. +12 −12 lib/mongoid/contextual/mongo.rb
  7. +19 −19 lib/mongoid/criteria.rb
  8. +24 −25 lib/mongoid/criterion/scoping.rb
  9. +19 −19 lib/mongoid/dirty.rb
  10. +20 −21 lib/mongoid/document.rb
  11. +4 −4 lib/mongoid/extensions/array.rb
  12. +4 −4 lib/mongoid/extensions/hash.rb
  13. +4 −5 lib/mongoid/extensions/object_id.rb
  14. +20 −18 lib/mongoid/fields.rb
  15. +7 −7 lib/mongoid/fields/serializable.rb
  16. +15 −12 lib/mongoid/hierarchy.rb
  17. +3 −3 lib/mongoid/indexes.rb
  18. +3 −3 lib/mongoid/loggable.rb
  19. +0 −1 lib/mongoid/observer.rb
  20. +22 −20 lib/mongoid/persistence.rb
  21. +2 −1 lib/mongoid/persistence/atomic/add_to_set.rb
  22. +2 −1 lib/mongoid/persistence/atomic/bit.rb
  23. +2 −1 lib/mongoid/persistence/atomic/inc.rb
  24. +6 −4 lib/mongoid/persistence/atomic/operation.rb
  25. +2 −1 lib/mongoid/persistence/atomic/pop.rb
  26. +2 −1 lib/mongoid/persistence/atomic/pull.rb
  27. +2 −1 lib/mongoid/persistence/atomic/pull_all.rb
  28. +3 −4 lib/mongoid/persistence/atomic/rename.rb
  29. +2 −1 lib/mongoid/persistence/atomic/sets.rb
  30. +8 −9 lib/mongoid/persistence/insertion.rb
  31. +3 −3 lib/mongoid/persistence/modification.rb
  32. +3 −3 lib/mongoid/persistence/operations.rb
  33. +7 −7 lib/mongoid/relations/builders.rb
  34. +7 −7 lib/mongoid/relations/builders/embedded/many.rb
  35. +2 −1 lib/mongoid/relations/cascading.rb
  36. +3 −4 lib/mongoid/relations/conversions.rb
  37. +8 −9 lib/mongoid/relations/embedded/in.rb
  38. +64 −61 lib/mongoid/relations/embedded/many.rb
  39. +9 −10 lib/mongoid/relations/embedded/one.rb
  40. +39 −39 lib/mongoid/relations/macros.rb
  41. +6 −7 lib/mongoid/relations/polymorphic.rb
  42. +2 −3 lib/mongoid/relations/proxy.rb
  43. +5 −6 lib/mongoid/relations/referenced/in.rb
  44. +27 −28 lib/mongoid/relations/referenced/many.rb
  45. +15 −16 lib/mongoid/relations/referenced/many_to_many.rb
  46. +10 −10 lib/mongoid/relations/targets/enumerable.rb
  47. +9 −11 lib/mongoid/reloading.rb
  48. +3 −3 lib/mongoid/scoping.rb
  49. +10 −10 lib/mongoid/serialization.rb
  50. +13 −15 lib/mongoid/sessions.rb
  51. +5 −5 lib/mongoid/sessions/factory.rb
  52. +4 −4 lib/mongoid/sharding.rb
  53. +4 −2 lib/mongoid/timestamps/timeless.rb
  54. +4 −4 lib/mongoid/validations/uniqueness.rb
  55. +5 −5 lib/mongoid/versioning.rb
View
23 lib/mongoid/atomic.rb
@@ -99,12 +99,12 @@ def atomic_array_add_to_sets
#
# @since 2.1.0
def atomic_updates
- Modifiers.new.tap do |mods|
- generate_atomic_updates(mods, self)
- _children.each do |child|
- generate_atomic_updates(mods, child)
- end
+ mods = Modifiers.new
+ generate_atomic_updates(mods, self)
+ _children.each do |child|
+ generate_atomic_updates(mods, child)
end
+ mods
end
alias :_updates :atomic_updates
@@ -170,14 +170,13 @@ def atomic_attribute_name(name)
#
# @since 2.2.0
def atomic_pulls
- delayed_atomic_pulls.inject({}) do |pulls, (_, docs)|
- pulls.tap do |pull|
- docs.each do |doc|
- (pull[doc.atomic_path] ||= []).push(doc.as_document)
- doc.destroyed = true
- doc.flagged_for_destroy = false
- end
+ delayed_atomic_pulls.reduce({}) do |pulls, (_, docs)|
+ docs.each do |doc|
+ (pulls[doc.atomic_path] ||= []).push(doc.as_document)
+ doc.destroyed = true
+ doc.flagged_for_destroy = false
end
+ pulls
end
end
View
18 lib/mongoid/attributes.rb
@@ -123,16 +123,16 @@ def write_attribute(name, value)
if attribute_writable?(access)
_assigning do
localized = fields[access].try(:localized?)
- typed_value_for(access, value).tap do |typed_value|
- unless attributes[access] == typed_value || attribute_changed?(access)
- attribute_will_change!(access)
- end
- if localized
- (attributes[access] ||= {}).merge!(typed_value)
- else
- attributes[access] = typed_value
- end
+ typed_value = typed_value_for(access, value)
+ unless attributes[access] == typed_value || attribute_changed?(access)
+ attribute_will_change!(access)
end
+ if localized
+ (attributes[access] ||= {}).merge!(typed_value)
+ else
+ attributes[access] = typed_value
+ end
+ typed_value
end
end
end
View
14 lib/mongoid/config.rb
@@ -47,9 +47,9 @@ def destructive_fields
#
# @since 2.0.1
def load!(path, environment = nil)
- Environment.load_yaml(path, environment).tap do |settings|
- load_configuration(settings) if settings.present?
- end
+ settings = Environment.load_yaml(path, environment)
+ load_configuration(settings) if settings.present?
+ settings
end
# Connect to the provided database name on the default session.
@@ -128,10 +128,10 @@ def sessions
#
# @since 3.0.0
def sessions=(sessions)
- sessions.with_indifferent_access.tap do |sess|
- Validators::Session.validate(sess)
- @sessions = sess
- end
+ sess = sessions.with_indifferent_access
+ Validators::Session.validate(sess)
+ @sessions = sess
+ sess
end
private
View
26 lib/mongoid/contextual/map_reduce.rb
@@ -75,9 +75,8 @@ def emitted
#
# @since 3.0.0
def finalize(function)
- tap do |mr|
- mr.command[:finalize] = function
- end
+ command[:finalize] = function
+ self
end
# Initialize the new map/reduce directive.
@@ -110,9 +109,8 @@ def input
end
def js_mode
- tap do |mr|
- mr.command[:jsMode] = true
- end
+ command[:jsMode] = true
+ self
end
# Specifies where the map/reduce output is to be stored.
@@ -135,13 +133,12 @@ def js_mode
#
# @since 3.0.0
def out(location)
- tap do |mr|
- normalized = location.dup
- normalized.update_values do |value|
- value.is_a?(::Symbol) ? value.to_s : value
- end
- mr.command[:out] = normalized
+ normalized = location.dup
+ normalized.update_values do |value|
+ value.is_a?(::Symbol) ? value.to_s : value
end
+ command[:out] = normalized
+ self
end
# Get the number of documents output by the map/reduce.
@@ -191,9 +188,8 @@ def reduced
#
# @since 3.0.0
def scope(object)
- tap do |mr|
- mr.command[:scope] = object
- end
+ command[:scope] = object
+ self
end
# Get the execution time of the map/reduce.
View
20 lib/mongoid/contextual/memory.rb
@@ -36,12 +36,12 @@ def blank?
# @since 3.0.0
def delete
# @todo: Durran: Optimize to a single db call.
- count.tap do
- each do |doc|
- documents.delete_one(doc)
- doc.delete
- end
+ deleted = count
+ each do |doc|
+ documents.delete_one(doc)
+ doc.delete
end
+ deleted
end
alias :delete_all :delete
@@ -55,12 +55,12 @@ def delete
# @since 3.0.0
def destroy
# @todo: Durran: Optimize to a single db call.
- count.tap do
- each do |doc|
- documents.delete_one(doc)
- doc.destroy
- end
+ deleted = count
+ each do |doc|
+ documents.delete_one(doc)
+ doc.destroy
end
+ deleted
end
alias :destroy_all :destroy
View
24 lib/mongoid/contextual/mongo.rb
@@ -78,11 +78,11 @@ def delete
#
# @since 3.0.0
def destroy
- query.count.tap do
- each do |doc|
- doc.destroy
- end
+ destroyed = query.count
+ each do |doc|
+ doc.destroy
end
+ destroyed
end
alias :destroy_all :destroy
@@ -115,12 +115,12 @@ def each
if block_given?
selecting do
if eager_loadable?
- query.map{ |doc| Factory.from_db(klass, doc) }.tap do |docs|
- eager_load(docs)
- docs.each do |doc|
- yield doc
- end
+ docs = query.map{ |doc| Factory.from_db(klass, doc) }
+ eager_load(docs)
+ docs.each do |doc|
+ yield doc
end
+ docs
else
query.each do |doc|
yield Factory.from_db(klass, doc)
@@ -458,9 +458,9 @@ def selecting
def with_eager_loading(document)
selecting do
return nil unless document
- Factory.from_db(klass, document).tap do |doc|
- eager_load([ doc ]) if eager_loadable?
- end
+ doc = Factory.from_db(klass, document)
+ eager_load([ doc ]) if eager_loadable?
+ doc
end
end
end
View
38 lib/mongoid/criteria.rb
@@ -75,7 +75,9 @@ def build(attrs = {})
#
# @return [ Criteria ] The cloned criteria.
def cache
- clone.tap { |crit| crit.options.merge!(cache: true) }
+ crit = clone
+ crit.options.merge!(cache: true)
+ crit
end
# Will return true if the cache option has been set.
@@ -205,9 +207,9 @@ def extract_id
#
# @since 2.0.0
def extras(extras)
- clone.tap do |crit|
- crit.options.merge!(extras)
- end
+ crit = clone
+ crit.options.merge!(extras)
+ crit
end
# Get the list of included fields.
@@ -398,9 +400,9 @@ def inclusions=(value)
#
# @return [ Criteria ] A cloned self.
def merge(other)
- clone.tap do |criteria|
- criteria.merge!(other)
- end
+ crit = clone
+ crit.merge!(other)
+ crit
end
# Merge the other criteria into this one.
@@ -415,13 +417,12 @@ def merge(other)
# @since 3.0.0
def merge!(other)
criteria = other.to_criteria
- tap do |crit|
- crit.selector.update(criteria.selector)
- crit.options.update(criteria.options)
- crit.documents = criteria.documents.dup if criteria.documents.any?
- crit.scoping_options = criteria.scoping_options
- crit.inclusions = (crit.inclusions + criteria.inclusions.dup).uniq
- end
+ selector.update(criteria.selector)
+ options.update(criteria.options)
+ self.documents = criteria.documents.dup if criteria.documents.any?
+ self.scoping_options = criteria.scoping_options
+ self.inclusions = (inclusions + criteria.inclusions.dup).uniq
+ self
end
# Overriden to include _type in the fields.
@@ -511,12 +512,11 @@ def type(types)
# @since 3.0.0
def create_document(method, attrs = {})
klass.__send__(method,
- selector.inject(attrs) do |hash, (key, value)|
- hash.tap do |_attrs|
- unless key.to_s =~ /\$/ || value.is_a?(Hash)
- _attrs[key] = value
- end
+ selector.reduce(attrs) do |hash, (key, value)|
+ unless key.to_s =~ /\$/ || value.is_a?(Hash)
+ hash[key] = value
end
+ hash
end
)
end
View
49 lib/mongoid/criterion/scoping.rb
@@ -30,19 +30,18 @@ def apply_default_scope
#
# @since 3.0.0
def remove_scoping(other)
- tap do |criteria|
- if other
- criteria.selector.reject! do |key, value|
- other.selector[key] == value
- end
- criteria.options.reject! do |key, value|
- other.options[key] == value
- end
- other.inclusions.each do |meta|
- criteria.inclusions.delete_one(meta)
- end
+ if other
+ selector.reject! do |key, value|
+ other.selector[key] == value
+ end
+ options.reject! do |key, value|
+ other.options[key] == value
+ end
+ other.inclusions.each do |meta|
+ inclusions.delete_one(meta)
end
end
+ self
end
# Forces the criteria to be scoped, unless it's inside an unscoped block.
@@ -56,12 +55,12 @@ def remove_scoping(other)
#
# @since 3.0.0
def scoped(options = nil)
- clone.tap do |criteria|
- criteria.options.merge!(options || {})
- if klass.default_scopable? && !scoped?
- criteria.apply_default_scope
- end
+ crit = clone
+ crit.options.merge!(options || {})
+ if klass.default_scopable? && !scoped?
+ crit.apply_default_scope
end
+ crit
end
# Has the criteria had the default scope applied?
@@ -85,12 +84,12 @@ def scoped?
#
# @since 3.0.0
def unscoped
- clone.tap do |criteria|
- unless unscoped?
- criteria.scoping_options = false, true
- criteria.selector.clear; criteria.options.clear
- end
+ crit = clone
+ unless unscoped?
+ crit.scoping_options = false, true
+ crit.selector.clear; crit.options.clear
end
+ crit
end
# Is the criteria unscoped?
@@ -143,11 +142,11 @@ def scoping_options=(options)
#
# @since 3.0.0
def with_default_scope
- clone.tap do |criteria|
- if klass.default_scopable? && !unscoped? && !scoped?
- criteria.apply_default_scope
- end
+ crit = clone
+ if klass.default_scopable? && !unscoped? && !scoped?
+ crit.apply_default_scope
end
+ crit
end
end
end
View
38 lib/mongoid/dirty.rb
@@ -134,29 +134,29 @@ def remove_change(name)
#
# @return [ Hash ] A +Hash+ of atomic setters.
def setters
- {}.tap do |modifications|
- changes.each_pair do |name, changes|
- if changes
- old, new = changes
- field = fields[name]
- key = atomic_attribute_name(name)
- if field && field.resizable?
- field.add_atomic_changes(
- self,
- name,
- key,
- modifications,
- new,
- old
- )
- else
- unless atomic_unsets.include?(key)
- modifications[key] = new
- end
+ modifications = {}
+ changes.each_pair do |name, changes|
+ if changes
+ old, new = changes
+ field = fields[name]
+ key = atomic_attribute_name(name)
+ if field && field.resizable?
+ field.add_atomic_changes(
+ self,
+ name,
+ key,
+ modifications,
+ new,
+ old
+ )
+ else
+ unless atomic_unsets.include?(key)
+ modifications[key] = new
end
end
end
end
+ modifications
end
private
View
41 lib/mongoid/document.rb
@@ -68,7 +68,7 @@ def eql?(other)
#
# @since 2.0.0
def freeze
- tap { |doc| doc.as_document.freeze }
+ as_document.freeze and self
end
# Checks if the document is frozen
@@ -170,17 +170,16 @@ def to_a
#
# @return [ Hash ] A hash of all attributes in the hierarchy.
def as_document
- attributes.tap do |attrs|
- return attrs if frozen?
- relations.each_pair do |name, meta|
- if meta.embedded?
- without_autobuild do
- relation = send(name)
- attrs[meta.store_as] = relation.as_document unless relation.blank?
- end
+ return attributes if frozen?
+ relations.each_pair do |name, meta|
+ if meta.embedded?
+ without_autobuild do
+ relation = send(name)
+ attributes[meta.store_as] = relation.as_document unless relation.blank?
end
end
end
+ attributes
end
# Returns an instance of the specified class with the attributes
@@ -198,12 +197,12 @@ def becomes(klass)
unless klass.include?(Mongoid::Document)
raise ArgumentError, "A class which includes Mongoid::Document is expected"
end
- klass.instantiate(frozen? ? attributes.dup : attributes).tap do |became|
- became.instance_variable_set(:@errors, errors)
- became.instance_variable_set(:@new_record, new_record?)
- became.instance_variable_set(:@destroyed, destroyed?)
- became._type = klass.to_s
- end
+ became = klass.instantiate(frozen? ? attributes.dup : attributes)
+ became.instance_variable_set(:@errors, errors)
+ became.instance_variable_set(:@new_record, new_record?)
+ became.instance_variable_set(:@destroyed, destroyed?)
+ became._type = klass.to_s
+ became
end
# Print out the cache key. This will append different values on the
@@ -289,12 +288,12 @@ def ===(other)
# @return [ Document ] A new document.
def instantiate(attrs = nil)
attributes = attrs || {}
- allocate.tap do |doc|
- doc.instance_variable_set(:@attributes, attributes)
- doc.apply_defaults
- IdentityMap.set(doc) unless _loading_revision?
- doc.run_callbacks(:initialize) { doc }
- end
+ doc = allocate
+ doc.instance_variable_set(:@attributes, attributes)
+ doc.apply_defaults
+ IdentityMap.set(doc) unless _loading_revision?
+ doc.run_callbacks(:initialize) { doc }
+ doc
end
# Returns all types to query for when using this class as the base.
View
8 lib/mongoid/extensions/array.rb
@@ -12,11 +12,11 @@ module Array #:nodoc:
#
# @since 2.4.0
def _deep_copy
- [].tap do |copy|
- each do |value|
- copy.push(value._deep_copy)
- end
+ copy = []
+ each do |value|
+ copy.push(value._deep_copy)
end
+ copy
end
# Delete the first object in the array that is equal to the supplied
View
8 lib/mongoid/extensions/hash.rb
@@ -12,11 +12,11 @@ module Hash
#
# @since 2.4.0
def _deep_copy
- {}.tap do |copy|
- each_pair do |key, value|
- copy[key] = value._deep_copy
- end
+ copy = {}
+ each_pair do |key, value|
+ copy[key] = value._deep_copy
end
+ copy
end
# Get the id attribute from this hash, whether it's prefixed with an
View
9 lib/mongoid/extensions/object_id.rb
@@ -104,12 +104,11 @@ def convert_from_array(klass, object, reject_blank=true)
#
# @since 3.0.0
def convert_from_hash(klass, object, reject_blank=true)
- object.tap do |hash|
- hash.each_pair do |key, value|
- next unless klass.object_id_field?(key)
- hash[key] = convert(klass, value, reject_blank)
- end
+ object.each_pair do |key, value|
+ next unless klass.object_id_field?(key)
+ object[key] = convert(klass, value, reject_blank)
end
+ object
end
def evolve(object)
View
38 lib/mongoid/fields.rb
@@ -203,11 +203,11 @@ def attribute_names
def field(name, options = {})
named = name.to_s
check_field_name!(name)
- add_field(named, options).tap do
- descendants.each do |subclass|
- subclass.add_field(named, options)
- end
+ added = add_field(named, options)
+ descendants.each do |subclass|
+ subclass.add_field(named, options)
end
+ added
end
# When inheriting, we want to copy the fields from the parent class and
@@ -306,15 +306,15 @@ def add_field(name, options = {})
aliased = options[:as]
aliased_fields[aliased.to_s] = name if aliased
type = options[:localize] ? Fields::Internal::Localized : options[:type]
- Mappings.for(type, options[:identity]).instantiate(name, options).tap do |field|
- fields[name] = field
- add_defaults(field)
- create_accessors(name, name, options)
- create_accessors(name, aliased, options) if aliased
- process_options(field)
- create_dirty_methods(name, name)
- create_dirty_methods(name, aliased) if aliased
- end
+ field = Mappings.for(type, options[:identity]).instantiate(name, options)
+ fields[name] = field
+ add_defaults(field)
+ create_accessors(name, name, options)
+ create_accessors(name, aliased, options) if aliased
+ process_options(field)
+ create_dirty_methods(name, name)
+ create_dirty_methods(name, aliased) if aliased
+ field
end
# Run through all custom options stored in Mongoid::Fields.options and
@@ -400,11 +400,11 @@ def create_field_getter(name, meth, field)
end
else
re_define_method(meth) do
- read_attribute(name).tap do |value|
- if value.is_a?(Array) || value.is_a?(Hash)
- attribute_will_change!(name)
- end
+ value = read_attribute(name)
+ if value.is_a?(Array) || value.is_a?(Hash)
+ attribute_will_change!(name)
end
+ value
end
end
end
@@ -490,7 +490,9 @@ def create_translations_setter(name, meth)
# @since 2.0.0
def generated_methods
@generated_methods ||= begin
- Module.new.tap { |mod| include(mod) }
+ mod = Module.new
+ include(mod)
+ mod
end
end
View
14 lib/mongoid/fields/serializable.rb
@@ -251,13 +251,13 @@ module ClassMethods #:nodoc:
#
# @since 2.1.0
def instantiate(name, options = {})
- allocate.tap do |field|
- field.name = name
- field.options = options
- field.label = options[:label]
- field.localize = options[:localize]
- field.default_val = options[:default]
- end
+ field = allocate
+ field.name = name
+ field.options = options
+ field.label = options[:label]
+ field.localize = options[:localize]
+ field.default_val = options[:default]
+ field
end
private
View
27 lib/mongoid/hierarchy.rb
@@ -19,20 +19,23 @@ module Hierarchy #:nodoc
#
# @return [ Array<Document> ] All child documents in the hierarchy.
def _children
- @_children ||=
- [].tap do |children|
- relations.each_pair do |name, metadata|
- if metadata.embedded?
- without_autobuild do
- child = send(name)
- Array.wrap(child).each do |doc|
- children.push(doc)
- children.concat(doc._children) unless metadata.versioned?
- end if child
- end
- end
+ @_children ||= collect_children
+ end
+
+ def collect_children
+ children = []
+ relations.each_pair do |name, metadata|
+ if metadata.embedded?
+ without_autobuild do
+ child = send(name)
+ Array.wrap(child).each do |doc|
+ children.push(doc)
+ children.concat(doc._children) unless metadata.versioned?
+ end if child
end
end
+ end
+ children
end
# Determines if the document is a subclass of another document.
View
6 lib/mongoid/indexes.rb
@@ -95,9 +95,9 @@ def index(spec)
#
# @since 3.0.0
def normalize_index_options(spec)
- (spec.delete(:options) || {}).tap do |opts|
- opts[:dropDups] = opts.delete(:drop_dups) if opts.has_key?(:drop_dups)
- end
+ opts = (spec.delete(:options) || {})
+ opts[:dropDups] = opts.delete(:drop_dups) if opts.has_key?(:drop_dups)
+ opts
end
end
end
View
6 lib/mongoid/loggable.rb
@@ -47,9 +47,9 @@ def logger=(logger)
#
# @since 3.0.0
def default_logger
- Logger.new($stdout).tap do |logger|
- logger.level = Logger::INFO
- end
+ logger = Logger.new($stdout)
+ logger.level = Logger::INFO
+ logger
end
# Get the Rails logger if it's defined.
View
1 lib/mongoid/observer.rb
@@ -176,6 +176,5 @@ def disabled_for?(object)
return false unless klass.respond_to?(:observers)
klass.observers.disabled_for?(self) || Mongoid.observers.disabled_for?(self)
end
-
end
end
View
42 lib/mongoid/persistence.rb
@@ -29,11 +29,11 @@ module Persistence
# @return [ true, false ] True if successful, false if not.
def destroy(options = {})
self.flagged_for_destroy = true
- run_callbacks(:destroy) do
+ result = run_callbacks(:destroy) do
remove(options)
- end.tap do
- self.flagged_for_destroy = false
end
+ self.flagged_for_destroy = false
+ result
end
# Insert a new document into the database. Will return the document
@@ -138,12 +138,12 @@ def update_attributes(attributes = {}, options = {})
#
# @return [ true, false ] True if validation passed.
def update_attributes!(attributes = {}, options = {})
- update_attributes(attributes, options).tap do |result|
- unless result
- self.class.fail_validate!(self) if errors.any?
- self.class.fail_callback!(self, :update_attributes!)
- end
+ result = update_attributes(attributes, options)
+ unless result
+ self.class.fail_validate!(self) if errors.any?
+ self.class.fail_callback!(self, :update_attributes!)
end
+ result
end
# Upsert the document - will perform an insert if the document is new, and
@@ -180,7 +180,9 @@ module ClassMethods #:nodoc:
# @return [ Document ] The newly created document.
def create(attributes = {}, options = {}, &block)
_creating do
- new(attributes, options, &block).tap { |doc| doc.save }
+ doc = new(attributes, options, &block)
+ doc.save
+ doc
end
end
@@ -199,10 +201,10 @@ def create(attributes = {}, options = {}, &block)
# @return [ Document ] The newly created document.
def create!(attributes = {}, options = {}, &block)
_creating do
- new(attributes, options, &block).tap do |doc|
- fail_validate!(doc) if doc.insert.errors.any?
- fail_callback!(doc, :create!) if doc.new_record?
- end
+ doc = new(attributes, options, &block)
+ fail_validate!(doc) if doc.insert.errors.any?
+ fail_callback!(doc, :create!) if doc.new_record?
+ doc
end
end
@@ -224,10 +226,10 @@ def delete_all(conditions = nil)
selector = conds[:conditions] || conds
selector.merge!(_type: name) if hereditary?
coll = collection
- coll.find(selector).count.tap do
- coll.find(selector).remove_all
- Threaded.clear_options!
- end
+ deleted = coll.find(selector).count
+ coll.find(selector).remove_all
+ Threaded.clear_options!
+ deleted
end
# Delete all documents given the supplied conditions. If no conditions
@@ -246,9 +248,9 @@ def delete_all(conditions = nil)
def destroy_all(conditions = nil)
conds = conditions || {}
documents = where(conds[:conditions] || conds)
- documents.count.tap do
- documents.each { |doc| doc.destroy }
- end
+ destroyed = documents.count
+ documents.each { |doc| doc.destroy }
+ destroyed
end
# Raise an error if validation failed.
View
3 lib/mongoid/persistence/atomic/add_to_set.rb
@@ -23,7 +23,8 @@ def persist
Array.wrap(value).each do |val|
values.push(val) unless values.include?(val)
end
- values.tap { execute("$addToSet") }
+ execute("$addToSet")
+ values
end
end
View
3 lib/mongoid/persistence/atomic/bit.rb
@@ -25,7 +25,8 @@ def persist
result = result | val if bit.to_s == "or"
result
end
- document[field].tap { execute("$bit") }
+ execute("$bit")
+ document[field]
end
end
end
View
3 lib/mongoid/persistence/atomic/inc.rb
@@ -19,7 +19,8 @@ def persist
prepare do
current = document[field] || 0
document[field] = current + value
- document[field].tap { execute("$inc") }
+ execute("$inc")
+ document[field]
end
end
end
View
10 lib/mongoid/persistence/atomic/operation.rb
@@ -75,9 +75,9 @@ def path
#
# @since 2.1.0
def prepare
- yield(document).tap do
- Threaded.clear_options!
- end
+ doc = yield(document)
+ Threaded.clear_options!
+ doc
end
private
@@ -113,7 +113,9 @@ def execute(name)
def append_with(name)
prepare do
document[field] = [] unless document[field]
- document.send(field).concat(value.__array__).tap { execute(name) }
+ docs = document.send(field).concat(value.__array__)
+ execute(name)
+ docs
end
end
end
View
3 lib/mongoid/persistence/atomic/pop.rb
@@ -21,7 +21,8 @@ def persist
if document[field]
values = document.send(field)
value > 0 ? values.pop : values.shift
- values.tap { execute("$pop") }
+ execute("$pop")
+ values
end
end
end
View
3 lib/mongoid/persistence/atomic/pull.rb
@@ -21,7 +21,8 @@ def persist
if document[field]
values = document.send(field)
values.delete(value)
- values.tap { execute("$pull") }
+ execute("$pull")
+ values
end
end
end
View
3 lib/mongoid/persistence/atomic/pull_all.rb
@@ -21,7 +21,8 @@ def persist
if document[field]
values = document.send(field)
values.delete_if { |val| value.include?(val) }
- values.tap { execute("$pullAll") }
+ execute("$pullAll")
+ values
end
end
end
View
7 lib/mongoid/persistence/atomic/rename.rb
@@ -19,10 +19,9 @@ def persist
prepare do
@value = value.to_s
document[value] = document.attributes.delete(field)
- document[value].tap do
- execute("$rename")
- document.remove_change(value)
- end
+ execute("$rename")
+ document.remove_change(value)
+ document[value]
end
end
end
View
3 lib/mongoid/persistence/atomic/sets.rb
@@ -18,7 +18,8 @@ class Sets
def persist
prepare do
document[field] = value
- document[field].tap { execute("$set") }
+ execute("$set")
+ document[field]
end
end
end
View
17 lib/mongoid/persistence/insertion.rb
@@ -19,18 +19,17 @@ module Insertion
#
# @since 2.1.0
def prepare(&block)
- document.tap do |doc|
- unless validating? && document.invalid?(:create)
- result = doc.run_callbacks(:save) do
- doc.run_callbacks(:create) do
- yield(doc)
- doc.new_record = false
- true
- end
+ unless validating? && document.invalid?(:create)
+ result = document.run_callbacks(:save) do
+ document.run_callbacks(:create) do
+ yield(document)
+ document.new_record = false
+ true
end
- document.post_persist unless result == false
end
+ document.post_persist unless result == false
end
+ document
end
end
end
View
6 lib/mongoid/persistence/modification.rb
@@ -20,13 +20,13 @@ module Modification
# @since 2.1.0
def prepare(&block)
return false if validating? && document.invalid?(:update)
- document.run_callbacks(:save) do
+ result = document.run_callbacks(:save) do
document.run_callbacks(:update) do
yield(document); true
end
- end.tap do |result|
- document.post_persist unless result == false
end
+ document.post_persist unless result == false
+ result
end
end
end
View
6 lib/mongoid/persistence/operations.rb
@@ -141,9 +141,9 @@ def validating?
#
# @since 2.1.0
def init_updates
- document.atomic_updates.tap do |updates|
- @conflicts = updates.delete(:conflicts) || {}
- end
+ updates = document.atomic_updates
+ @conflicts = updates.delete(:conflicts) || {}
+ updates
end
class << self
View
14 lib/mongoid/relations/builders.rb
@@ -67,9 +67,9 @@ def builder(name, metadata)
attributes, options = parse_args(*args)
document = Factory.build(metadata.klass, attributes, options)
_building do
- send("#{name}=", document).tap do |child|
- child.run_callbacks(:build)
- end
+ child = send("#{name}=", document)
+ child.run_callbacks(:build)
+ child
end
end
self
@@ -91,10 +91,10 @@ def creator(name, metadata)
re_define_method("create_#{name}") do |*args|
attributes, options = parse_args(*args)
document = Factory.build(metadata.klass, attributes, options)
- send("#{name}=", document).tap do |doc|
- doc.save
- save if new_record? && metadata.stores_foreign_key?
- end
+ doc = send("#{name}=", document)
+ doc.save
+ save if new_record? && metadata.stores_foreign_key?
+ doc
end
self
end
View
14 lib/mongoid/relations/builders/embedded/many.rb
@@ -19,15 +19,15 @@ class Many < Builder #:nodoc:
def build(type = nil)
return [] if object.blank?
return object if object.first.is_a?(Document)
- [].tap do |docs|
- object.each do |attrs|
- if _loading? && base.persisted?
- docs.push(Factory.from_db(klass, attrs))
- else
- docs.push(Factory.build(klass, attrs))
- end
+ docs = []
+ object.each do |attrs|
+ if _loading? && base.persisted?
+ docs.push(Factory.from_db(klass, attrs))
+ else
+ docs.push(Factory.build(klass, attrs))
end
end
+ docs
end
end
end
View
3 lib/mongoid/relations/cascading.rb
@@ -48,7 +48,8 @@ module ClassMethods #:nodoc:
#
# @since 2.0.0.rc.1
def cascade(metadata)
- tap { self.cascades += [ metadata.name.to_s ] if metadata.dependent? }
+ self.cascades += [ metadata.name.to_s ] if metadata.dependent?
+ self
end
end
end
View
7 lib/mongoid/relations/conversions.rb
@@ -23,11 +23,10 @@ def flag(object, metadata)
if inverse.using_object_ids? || object.is_a?(BSON::ObjectId)
object
else
- object.tap do |obj|
- if obj.is_a?(String)
- obj.unconvertable_to_bson = true
- end
+ if object.is_a?(String)
+ object.unconvertable_to_bson = true
end
+ object
end
end
end
View
17 lib/mongoid/relations/embedded/in.rb
@@ -37,16 +37,15 @@ def initialize(base, target, metadata)
#
# @since 2.0.0.rc.1
def substitute(replacement)
- tap do |proxy|
- proxy.unbind_one
- unless replacement
- base.delete if persistable?
- return nil
- end
- base.new_record = true
- proxy.target = replacement
- proxy.bind_one
+ unbind_one
+ unless replacement
+ base.delete if persistable?
+ return nil
end
+ base.new_record = true
+ self.target = replacement
+ bind_one
+ self
end
private
View
125 lib/mongoid/relations/embedded/many.rb
@@ -37,11 +37,11 @@ def <<(*args)
#
# @since 2.0.0.rc.1
def as_document
- [].tap do |attributes|
- _unscoped.each do |doc|
- attributes.push(doc.as_document)
- end
+ attributes = []
+ _unscoped.each do |doc|
+ attributes.push(doc.as_document)
end
+ attributes
end
# Appends an array of documents to the relation. Performs a batch
@@ -121,10 +121,9 @@ def build(attributes = {}, options = {}, type = nil)
#
# @return [ Many ] The empty relation.
def clear
- tap do |proxy|
- proxy.delete_all
- _unscoped.clear
- end
+ delete_all
+ _unscoped.clear
+ self
end
# Returns a count of the number of documents in the association that have
@@ -158,7 +157,9 @@ def count
#
# @return [ Document ] The newly created document.
def create(attributes = {}, options = {}, type = nil, &block)
- build(attributes, options, type, &block).tap { |doc| doc.save }
+ doc = build(attributes, options, type, &block)
+ doc.save
+ doc
end
# Create a new document in the relation. This is essentially the same
@@ -181,7 +182,9 @@ def create(attributes = {}, options = {}, type = nil, &block)
#
# @return [ Document ] The newly created document.
def create!(attributes = {}, options = {}, type = nil, &block)
- build(attributes, options, type, &block).tap { |doc| doc.save! }
+ doc = build(attributes, options, type, &block)
+ doc.save!
+ doc
end
# Delete the supplied document from the target. This method is proxied
@@ -196,18 +199,18 @@ def create!(attributes = {}, options = {}, type = nil, &block)
#
# @since 2.0.0.rc.1
def delete(document)
- target.delete_one(document).tap do |doc|
- _unscoped.delete_one(doc)
- if doc && !_binding?
- if _assigning? && !doc.paranoid?
- base.add_atomic_pull(doc)
- else
- doc.delete(suppress: true)
- end
- unbind_one(doc)
+ doc = target.delete_one(document)
+ _unscoped.delete_one(doc)
+ if doc && !_binding?
+ if _assigning? && !doc.paranoid?
+ base.add_atomic_pull(doc)
+ else
+ doc.delete(suppress: true)
end
- reindex
+ unbind_one(doc)
end
+ reindex
+ doc
end
# Delete all the documents in the association without running callbacks.
@@ -379,10 +382,10 @@ def substitute(replacement)
#
# @since 2.4.0
def unscoped
- klass.unscoped.tap do |criterion|
- criterion.embedded = true
- criterion.documents = _unscoped
- end
+ criterion = klass.unscoped
+ criterion.embedded = true
+ criterion.documents = _unscoped
+ criterion
end
private
@@ -425,10 +428,10 @@ def binding
#
# @return [ Criteria ] A new criteria.
def criteria
- klass.scoped.tap do |criterion|
- criterion.embedded = true
- criterion.documents = target
- end
+ criterion = klass.scoped
+ criterion.embedded = true
+ criterion.documents = target
+ criterion
end
# Deletes one document from the target and unscoped.
@@ -517,10 +520,10 @@ def reindex
# @since 2.4.0
def scope(docs)
return docs unless metadata.order || metadata.klass.default_scoping?
- metadata.klass.order_by(metadata.order).tap do |crit|
- crit.embedded = true
- crit.documents = docs
- end.entries
+ crit = metadata.klass.order_by(metadata.order)
+ crit.embedded = true
+ crit.documents = docs
+ crit.entries
end
# Remove all documents from the relation, either with a delete or a
@@ -536,39 +539,39 @@ def scope(docs)
def remove_all(conditions = {}, method = :delete)
# @todo: Durran: test all examples and refactor.
criteria = where(conditions || {})
- criteria.size.tap do
- docs = criteria.map do |doc|
- target.delete_one(doc)
- _unscoped.delete_one(doc)
- if !_assigning? && !metadata.versioned?
- doc.cascade!
- doc.run_before_callbacks(:destroy) if method == :destroy
- end
- unbind_one(doc)
- doc
+ removed = criteria.size
+ docs = criteria.map do |doc|
+ target.delete_one(doc)
+ _unscoped.delete_one(doc)
+ if !_assigning? && !metadata.versioned?
+ doc.cascade!
+ doc.run_before_callbacks(:destroy) if method == :destroy
end
- if !docs.empty? && !_assigning?
- query = collection.find(base.atomic_selector)
- # @todo: Durran: Versioned docs have no atomic path?
- if metadata.versioned?
- query.update("$pull" => { metadata.name => conditions || {}})
- else
- query.update(
- "$pullAll" => { docs.first.atomic_path => docs.map(&:as_document) }
- )
- end
+ unbind_one(doc)
+ doc
+ end
+ if !docs.empty? && !_assigning?
+ query = collection.find(base.atomic_selector)
+ # @todo: Durran: Versioned docs have no atomic path?
+ if metadata.versioned?
+ query.update("$pull" => { metadata.name => conditions || {}})
+ else
+ query.update(
+ "$pullAll" => { docs.first.atomic_path => docs.map(&:as_document) }
+ )
end
- unless _assigning?
- docs.each do |doc|
- doc.run_after_callbacks(:destroy) if method == :destroy
- doc.freeze
- doc.destroyed = true
- IdentityMap.remove(doc)
- end
- Threaded.clear_options!
+ end
+ unless _assigning?
+ docs.each do |doc|
+ doc.run_after_callbacks(:destroy) if method == :destroy
+ doc.freeze
+ doc.destroyed = true
+ IdentityMap.remove(doc)
end
- reindex
+ Threaded.clear_options!
end
+ reindex
+ removed
end
# Get the internal unscoped documents.
View
19 lib/mongoid/relations/embedded/one.rb
@@ -35,17 +35,16 @@ def initialize(base, target, metadata)
#
# @since 2.0.0.rc.1
def substitute(replacement)
- tap do |proxy|
- if _assigning?
- base.atomic_unsets.push(proxy.atomic_path)
- else
- proxy.delete if persistable?
- end
- proxy.unbind_one
- return nil unless replacement
- proxy.target = replacement
- proxy.bind_one
+ if _assigning?
+ base.atomic_unsets.push(atomic_path)
+ else
+ delete if persistable?
end
+ unbind_one
+ return nil unless replacement
+ self.target = replacement
+ bind_one
+ self
end
private
View
78 lib/mongoid/relations/macros.rb
@@ -52,11 +52,11 @@ def embedded_in(name, options = {}, &block)
if ancestors.include?(Mongoid::Versioning)
raise Errors::VersioningNotOnRoot.new(self)
end
- characterize(name, Embedded::In, options, &block).tap do |meta|
- self.embedded = true
- relate(name, meta)
- builder(name, meta).creator(name, meta)
- end
+ meta = characterize(name, Embedded::In, options, &block)
+ self.embedded = true
+ relate(name, meta)
+ builder(name, meta).creator(name, meta)
+ meta
end
# Adds the relation from a parent document to its children. The name
@@ -79,11 +79,11 @@ def embedded_in(name, options = {}, &block)
# @param [ Hash ] options The relation options.
# @param [ Proc ] block Optional block for defining extensions.
def embeds_many(name, options = {}, &block)
- characterize(name, Embedded::Many, options, &block).tap do |meta|
- self.cyclic = true if options[:cyclic]
- relate(name, meta)
- validates_relation(meta)
- end
+ meta = characterize(name, Embedded::Many, options, &block)
+ self.cyclic = true if options[:cyclic]
+ relate(name, meta)
+ validates_relation(meta)
+ meta
end
# Adds the relation from a parent document to its child. The name
@@ -106,12 +106,12 @@ def embeds_many(name, options = {}, &block)
# @param [ Hash ] options The relation options.
# @param [ Proc ] block Optional block for defining extensions.
def embeds_one(name, options = {}, &block)
- characterize(name, Embedded::One, options, &block).tap do |meta|
- self.cyclic = true if options[:cyclic]
- relate(name, meta)
- builder(name, meta).creator(name, meta)
- validates_relation(meta)
- end
+ meta = characterize(name, Embedded::One, options, &block)
+ self.cyclic = true if options[:cyclic]
+ relate(name, meta)
+ builder(name, meta).creator(name, meta)
+ validates_relation(meta)
+ meta
end
# Adds a relational association from the child Document to a Document in
@@ -133,9 +133,9 @@ def embeds_one(name, options = {}, &block)
# @param [ Hash ] options The relation options.
# @param [ Proc ] block Optional block for defining extensions.
def belongs_to(name, options = {}, &block)
- reference_one_to_one(name, options, Referenced::In, &block).tap do |meta|
- aliased_fields[name.to_s] = meta.foreign_key
- end
+ meta = reference_one_to_one(name, options, Referenced::In, &block)
+ aliased_fields[name.to_s] = meta.foreign_key
+ meta
end
# Adds a relational association from a parent Document to many
@@ -157,13 +157,13 @@ def belongs_to(name, options = {}, &block)
# @param [ Hash ] options The relation options.
# @param [ Proc ] block Optional block for defining extensions.
def has_many(name, options = {}, &block)
- characterize(name, Referenced::Many, options, &block).tap do |meta|
- relate(name, meta)
- ids_getter(name, meta).ids_setter(name, meta)
- reference(meta)
- autosave(meta)
- validates_relation(meta)
- end
+ meta = characterize(name, Referenced::Many, options, &block)
+ relate(name, meta)
+ ids_getter(name, meta).ids_setter(name, meta)
+ reference(meta)
+ autosave(meta)
+ validates_relation(meta)
+ meta
end
# Adds a relational many-to-many association between many of this
@@ -187,13 +187,13 @@ def has_many(name, options = {}, &block)
#
# @since 2.0.0.rc.1
def has_and_belongs_to_many(name, options = {}, &block)
- characterize(name, Referenced::ManyToMany, options, &block).tap do |meta|
- relate(name, meta)
- reference(meta, Array)
- autosave(meta)
- validates_relation(meta)
- synced(meta)
- end
+ meta = characterize(name, Referenced::ManyToMany, options, &block)
+ relate(name, meta)
+ reference(meta, Array)
+ autosave(meta)
+ validates_relation(meta)
+ synced(meta)
+ meta
end
# Adds a relational association from the child Document to a Document in
@@ -310,12 +310,12 @@ def reference(metadata, type = Object)
#
# @since 3.0.0
def reference_one_to_one(name, options, relation, &block)
- characterize(name, relation, options, &block).tap do |meta|
- relate(name, meta)
- reference(meta)
- builder(name, meta).creator(name, meta).autosave(meta)
- validates_relation(meta)
- end
+ meta = characterize(name, relation, options, &block)
+ relate(name, meta)
+ reference(meta)
+ builder(name, meta).creator(name, meta).autosave(meta)
+ validates_relation(meta)
+ meta
end
# Creates a relation for the given name, metadata and relation. It adds
View
13 lib/mongoid/relations/polymorphic.rb
@@ -25,15 +25,14 @@ module ClassMethods #:nodoc:
#
# @since 2.0.0.rc.1
def polymorph(metadata)
- tap do |klass|
- if metadata.polymorphic?
- klass.polymorphic = true
- if metadata.relation.stores_foreign_key?
- field(metadata.inverse_type, type: String)
- field(metadata.inverse_of_field, type: Symbol)
- end
+ if metadata.polymorphic?
+ self.polymorphic = true
+ if metadata.relation.stores_foreign_key?
+ field(metadata.inverse_type, type: String)
+ field(metadata.inverse_of_field, type: Symbol)
end
end
+ self
end
end
end
View
5 lib/mongoid/relations/proxy.rb
@@ -75,9 +75,8 @@ def substitutable
#
# @since 3.0.0
def with(options)
- tap do
- Threaded.set_persistence_options(klass, options)
- end
+ Threaded.set_persistence_options(klass, options)
+ self
end
protected
View
11 lib/mongoid/relations/referenced/in.rb
@@ -50,12 +50,11 @@ def nullify
#
# @since 2.0.0.rc.1
def substitute(replacement)
- tap do |proxy|
- proxy.unbind_one
- return nil unless replacement
- proxy.target = replacement
- proxy.bind_one
- end
+ unbind_one
+ return nil unless replacement
+ self.target = replacement
+ bind_one
+ self
end
private
View
55 lib/mongoid/relations/referenced/many.rb
@@ -89,12 +89,12 @@ def build(attributes = {}, options = {}, type = nil)
options, type = {}, options
end
- Factory.build(type || klass, attributes, options).tap do |doc|
- append(doc)
- doc.apply_post_processed_defaults
- yield(doc) if block_given?
- doc.run_callbacks(:build) { doc }
- end
+ doc = Factory.build(type || klass, attributes, options)
+ append(doc)
+ doc.apply_post_processed_defaults
+ yield(doc) if block_given?
+ doc.run_callbacks(:build) { doc }
+ doc
end
alias :new :build
@@ -117,9 +117,9 @@ def build(attributes = {}, options = {}, type = nil)
#
# @since 2.0.0.beta.1
def create(attributes = nil, options = {}, type = nil, &block)
- build(attributes, options, type, &block).tap do |doc|
- base.persisted? ? doc.save : raise_unsaved(doc)
- end
+ doc = build(attributes, options, type, &block)
+ base.persisted? ? doc.save : raise_unsaved(doc)
+ doc
end
# Creates a new document on the references many relation. This will
@@ -144,9 +144,9 @@ def create(attributes = nil, options = {}, type = nil, &block)
#
# @since 2.0.0.beta.1
def create!(attributes = nil, options = {}, type = nil, &block)
- build(attributes, options, type, &block).tap do |doc|
- base.persisted? ? doc.save! : raise_unsaved(doc)
- end
+ doc = build(attributes, options, type, &block)
+ base.persisted? ? doc.save! : raise_unsaved(doc)
+ doc
end
# Delete the document from the relation. This will set the foreign key
@@ -309,19 +309,18 @@ def purge
#
# @since 2.0.0.rc.1
def substitute(replacement)
- tap do |proxy|
- if replacement
- new_docs, docs = replacement.compact.uniq, []
- new_ids = new_docs.map { |doc| doc.id }
- remove_not_in(new_ids)
- new_docs.each do |doc|
- docs.push(doc) if doc.send(metadata.foreign_key) != base.id
- end
- proxy.concat(docs)
- else
- proxy.purge
+ if replacement
+ new_docs, docs = replacement.compact.uniq, []
+ new_ids = new_docs.map { |doc| doc.id }
+ remove_not_in(new_ids)
+ new_docs.each do |doc|
+ docs.push(doc) if doc.send(metadata.foreign_key) != base.id
end
+ concat(docs)
+ else
+ purge
end
+ self
end
# Get a criteria for the documents without the default scoping
@@ -467,13 +466,13 @@ def persistable?
# @since 2.1.0
def remove_all(conditions = nil, method = :delete_all)
selector = conditions || {}
- klass.send(method, selector.merge!(criteria.selector)).tap do
- target.delete_if do |doc|
- if doc.matches?(selector)
- unbind_one(doc) and true
- end
+ removed = klass.send(method, selector.merge!(criteria.selector))
+ target.delete_if do |doc|
+ if doc.matches?(selector)
+ unbind_one(doc) and true
end
end
+ removed
end
# Remove all the documents in the proxy that do not have the provided
View
31 lib/mongoid/relations/referenced/many_to_many.rb
@@ -72,13 +72,13 @@ def build(attributes = {}, options = {}, type = nil)
options, type = {}, options
end
- Factory.build(type || klass, attributes, options).tap do |doc|
- base.send(metadata.foreign_key).push(doc.id)
- append(doc)
- doc.apply_post_processed_defaults
- doc.synced[metadata.inverse_foreign_key] = false
- yield(doc) if block_given?
- end
+ doc = Factory.build(type || klass, attributes, options)
+ base.send(metadata.foreign_key).push(doc.id)
+ append(doc)
+ doc.apply_post_processed_defaults
+ doc.synced[metadata.inverse_foreign_key] = false
+ yield(doc) if block_given?
+ doc
end
alias :new :build
@@ -95,12 +95,12 @@ def build(attributes = {}, options = {}, type = nil)
#
# @since 2.1.0
def delete(document)
- super.tap do |doc|
- if doc && persistable?
- base.pull(metadata.foreign_key, doc.id)
- base.synced[metadata.foreign_key] = false
- end
+ doc = super
+ if doc && persistable?
+ base.pull(metadata.foreign_key, doc.id)
+ base.synced[metadata.foreign_key] = false
end
+ doc
end
# Removes all associations between the base document and the target
@@ -142,10 +142,9 @@ def nullify
#
# @since 2.0.0.rc.1
def substitute(replacement)
- tap do |proxy|
- proxy.purge
- proxy.push(replacement.compact.uniq) unless replacement.blank?
- end
+ purge
+ push(replacement.compact.uniq) unless replacement.blank?
+ self
end
# Get a criteria for the documents without the default scoping
View
20 lib/mongoid/relations/targets/enumerable.rb
@@ -94,15 +94,15 @@ def clone
#
# @since 2.1.0
def delete(document)
- (loaded.delete(document) || added.delete(document)).tap do |doc|
- unless doc
- if unloaded && unloaded.where(_id: document.id).exists?
- yield(document) if block_given?
- return document
- end
+ doc = (loaded.delete(document) || added.delete(document))
+ unless doc
+ if unloaded && unloaded.where(_id: document.id).exists?
+ yield(document) if block_given?
+ return document
end
- yield(doc) if block_given?
end
+ yield(doc) if block_given?
+ doc
end
# Deletes every document in the enumerable for where the block returns
@@ -250,9 +250,9 @@ def inspect
#
# @since 2.1.0
def in_memory
- (loaded + added).tap do |docs|
- docs.each { |doc| yield(doc) } if block_given?
- end
+ docs = (loaded + added)
+ docs.each { |doc| yield(doc) } if block_given?
+ docs
end
# Get the last document in the enumerable. Will check the new
View
20 lib/mongoid/reloading.rb
@@ -16,18 +16,16 @@ module Reloading
#
# @since 1.0.0
def reload
- _reload.tap do |reloaded|
- if Mongoid.raise_not_found_error && reloaded.empty?
- raise Errors::DocumentNotFound.new(self.class, id, id)
- end
- @attributes = reloaded
- end
- tap do |doc|
- doc.changed_attributes.clear
- doc.apply_defaults
- doc.reload_relations
- doc.run_callbacks(:initialize)
+ reloaded = _reload
+ if Mongoid.raise_not_found_error && reloaded.empty?
+ raise Errors::DocumentNotFound.new(self.class, id, id)
end
+ @attributes = reloaded
+ changed_attributes.clear
+ apply_defaults
+ reload_relations
+ run_callbacks(:initialize)
+ self
end
private
View
6 lib/mongoid/scoping.rb
@@ -277,9 +277,9 @@ def define_scope_method(name)
def #{name}(*args)
scoping = scopes[:#{name}]
scope, extension = scoping[:scope][*args], scoping[:extension]
- with_default_scope.merge(scope).tap do |criteria|
- criteria.extend(extension)
- end
+ criteria = with_default_scope.merge(scope)
+ criteria.extend(extension)
+ criteria
end
SCOPE
end
View
20 lib/mongoid/serialization.rb
@@ -45,19 +45,19 @@ def serializable_hash(options = nil)
name.to_s if respond_to?(name)
end.compact
- {}.tap do |attrs|
- (attribute_names + method_names).each do |name|
- without_autobuild do
- value = send(name)
- if relations.has_key?(name)
- attrs[name] = value ? value.serializable_hash(options) : nil
- else
- attrs[name] = value
- end
+ attrs = {}
+ (attribute_names + method_names).each do |name|
+ without_autobuild do
+ value = send(name)
+ if relations.has_key?(name)
+ attrs[name] = value ? value.serializable_hash(options) : nil
+ else
+ attrs[name] = value
end
end
- serialize_relations(attrs, options) if options[:include]
end
+ serialize_relations(attrs, options) if options[:include]
+ attrs
end
class << self
View
28 lib/mongoid/sessions.rb
@@ -79,9 +79,8 @@ def mongo_session
#
# @since 3.0.0
def with(options)
- tap do
- Threaded.set_persistence_options(self.class, options)
- end
+ Threaded.set_persistence_options(self.class, options)
+ self
end
class << self
@@ -151,9 +150,9 @@ def clear_persistence_options
# @since 3.0.0
def collection
if opts = persistence_options
- mongo_session.with(opts)[opts[:collection] || collection_name].tap do
- clear_persistence_options
- end
+ coll = mongo_session.with(opts)[opts[:collection] || collection_name]
+ clear_persistence_options
+ coll
else
mongo_session[collection_name]
end
@@ -197,13 +196,13 @@ def database_name
#
# @since 3.0.0
def mongo_session
- __session__.tap do |session|
- if persistence_options && name = persistence_options[:database]
- session.use(name)
- else
- session.use(database_name)
- end
+ session = __session__
+ if persistence_options && name = persistence_options[:database]
+ session.use(name)
+ else
+ session.use(database_name)
end
+ session
end
# Get the persistence options from the current thread.
@@ -284,9 +283,8 @@ def store_in(options)
#
# @since 3.0.0
def with(options)
- tap do
- Threaded.set_persistence_options(self, options)
- end
+ Threaded.set_persistence_options(self, options)
+ self
end
private
View
10 lib/mongoid/sessions/factory.rb