Skip to content
This repository
Browse code

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...
commit 8ce131d2f756b7bd634bcbe9359da6e5d2ca8230 1 parent 4e213f5
Durran Jordan durran authored

Showing 55 changed files with 522 additions and 523 deletions. Show diff stats Hide diff stats

  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
23 lib/mongoid/atomic.rb
@@ -99,12 +99,12 @@ def atomic_array_add_to_sets
99 99 #
100 100 # @since 2.1.0
101 101 def atomic_updates
102   - Modifiers.new.tap do |mods|
103   - generate_atomic_updates(mods, self)
104   - _children.each do |child|
105   - generate_atomic_updates(mods, child)
106   - end
  102 + mods = Modifiers.new
  103 + generate_atomic_updates(mods, self)
  104 + _children.each do |child|
  105 + generate_atomic_updates(mods, child)
107 106 end
  107 + mods
108 108 end
109 109 alias :_updates :atomic_updates
110 110
@@ -170,14 +170,13 @@ def atomic_attribute_name(name)
170 170 #
171 171 # @since 2.2.0
172 172 def atomic_pulls
173   - delayed_atomic_pulls.inject({}) do |pulls, (_, docs)|
174   - pulls.tap do |pull|
175   - docs.each do |doc|
176   - (pull[doc.atomic_path] ||= []).push(doc.as_document)
177   - doc.destroyed = true
178   - doc.flagged_for_destroy = false
179   - end
  173 + delayed_atomic_pulls.reduce({}) do |pulls, (_, docs)|
  174 + docs.each do |doc|
  175 + (pulls[doc.atomic_path] ||= []).push(doc.as_document)
  176 + doc.destroyed = true
  177 + doc.flagged_for_destroy = false
180 178 end
  179 + pulls
181 180 end
182 181 end
183 182
18 lib/mongoid/attributes.rb
@@ -123,16 +123,16 @@ def write_attribute(name, value)
123 123 if attribute_writable?(access)
124 124 _assigning do
125 125 localized = fields[access].try(:localized?)
126   - typed_value_for(access, value).tap do |typed_value|
127   - unless attributes[access] == typed_value || attribute_changed?(access)
128   - attribute_will_change!(access)
129   - end
130   - if localized
131   - (attributes[access] ||= {}).merge!(typed_value)
132   - else
133   - attributes[access] = typed_value
134   - end
  126 + typed_value = typed_value_for(access, value)
  127 + unless attributes[access] == typed_value || attribute_changed?(access)
  128 + attribute_will_change!(access)
135 129 end
  130 + if localized
  131 + (attributes[access] ||= {}).merge!(typed_value)
  132 + else
  133 + attributes[access] = typed_value
  134 + end
  135 + typed_value
136 136 end
137 137 end
138 138 end
14 lib/mongoid/config.rb
@@ -47,9 +47,9 @@ def destructive_fields
47 47 #
48 48 # @since 2.0.1
49 49 def load!(path, environment = nil)
50   - Environment.load_yaml(path, environment).tap do |settings|
51   - load_configuration(settings) if settings.present?
52   - end
  50 + settings = Environment.load_yaml(path, environment)
  51 + load_configuration(settings) if settings.present?
  52 + settings
53 53 end
54 54
55 55 # Connect to the provided database name on the default session.
@@ -128,10 +128,10 @@ def sessions
128 128 #
129 129 # @since 3.0.0
130 130 def sessions=(sessions)
131   - sessions.with_indifferent_access.tap do |sess|
132   - Validators::Session.validate(sess)
133   - @sessions = sess
134   - end
  131 + sess = sessions.with_indifferent_access
  132 + Validators::Session.validate(sess)
  133 + @sessions = sess
  134 + sess
135 135 end
136 136
137 137 private
26 lib/mongoid/contextual/map_reduce.rb
@@ -75,9 +75,8 @@ def emitted
75 75 #
76 76 # @since 3.0.0
77 77 def finalize(function)
78   - tap do |mr|
79   - mr.command[:finalize] = function
80   - end
  78 + command[:finalize] = function
  79 + self
81 80 end
82 81
83 82 # Initialize the new map/reduce directive.
@@ -110,9 +109,8 @@ def input
110 109 end
111 110
112 111 def js_mode
113   - tap do |mr|
114   - mr.command[:jsMode] = true
115   - end
  112 + command[:jsMode] = true
  113 + self
116 114 end
117 115
118 116 # Specifies where the map/reduce output is to be stored.
@@ -135,13 +133,12 @@ def js_mode
135 133 #
136 134 # @since 3.0.0
137 135 def out(location)
138   - tap do |mr|
139   - normalized = location.dup
140   - normalized.update_values do |value|
141   - value.is_a?(::Symbol) ? value.to_s : value
142   - end
143   - mr.command[:out] = normalized
  136 + normalized = location.dup
  137 + normalized.update_values do |value|
  138 + value.is_a?(::Symbol) ? value.to_s : value
144 139 end
  140 + command[:out] = normalized
  141 + self
145 142 end
146 143
147 144 # Get the number of documents output by the map/reduce.
@@ -191,9 +188,8 @@ def reduced
191 188 #
192 189 # @since 3.0.0
193 190 def scope(object)
194   - tap do |mr|
195   - mr.command[:scope] = object
196   - end
  191 + command[:scope] = object
  192 + self
197 193 end
198 194
199 195 # Get the execution time of the map/reduce.
20 lib/mongoid/contextual/memory.rb
@@ -36,12 +36,12 @@ def blank?
36 36 # @since 3.0.0
37 37 def delete
38 38 # @todo: Durran: Optimize to a single db call.
39   - count.tap do
40   - each do |doc|
41   - documents.delete_one(doc)
42   - doc.delete
43   - end
  39 + deleted = count
  40 + each do |doc|
  41 + documents.delete_one(doc)
  42 + doc.delete
44 43 end
  44 + deleted
45 45 end
46 46 alias :delete_all :delete
47 47
@@ -55,12 +55,12 @@ def delete
55 55 # @since 3.0.0
56 56 def destroy
57 57 # @todo: Durran: Optimize to a single db call.
58   - count.tap do
59   - each do |doc|
60   - documents.delete_one(doc)
61   - doc.destroy
62   - end
  58 + deleted = count
  59 + each do |doc|
  60 + documents.delete_one(doc)
  61 + doc.destroy
63 62 end
  63 + deleted
64 64 end
65 65 alias :destroy_all :destroy
66 66
24 lib/mongoid/contextual/mongo.rb
@@ -78,11 +78,11 @@ def delete
78 78 #
79 79 # @since 3.0.0
80 80 def destroy
81   - query.count.tap do
82   - each do |doc|
83   - doc.destroy
84   - end
  81 + destroyed = query.count
  82 + each do |doc|
  83 + doc.destroy
85 84 end
  85 + destroyed
86 86 end
87 87 alias :destroy_all :destroy
88 88
@@ -115,12 +115,12 @@ def each
115 115 if block_given?
116 116 selecting do
117 117 if eager_loadable?
118   - query.map{ |doc| Factory.from_db(klass, doc) }.tap do |docs|
119   - eager_load(docs)
120   - docs.each do |doc|
121   - yield doc
122   - end
  118 + docs = query.map{ |doc| Factory.from_db(klass, doc) }
  119 + eager_load(docs)
  120 + docs.each do |doc|
  121 + yield doc
123 122 end
  123 + docs
124 124 else
125 125 query.each do |doc|
126 126 yield Factory.from_db(klass, doc)
@@ -458,9 +458,9 @@ def selecting
458 458 def with_eager_loading(document)
459 459 selecting do
460 460 return nil unless document
461   - Factory.from_db(klass, document).tap do |doc|
462   - eager_load([ doc ]) if eager_loadable?
463   - end
  461 + doc = Factory.from_db(klass, document)
  462 + eager_load([ doc ]) if eager_loadable?
  463 + doc
464 464 end
465 465 end
466 466 end
38 lib/mongoid/criteria.rb
@@ -75,7 +75,9 @@ def build(attrs = {})
75 75 #
76 76 # @return [ Criteria ] The cloned criteria.
77 77 def cache
78   - clone.tap { |crit| crit.options.merge!(cache: true) }
  78 + crit = clone
  79 + crit.options.merge!(cache: true)
  80 + crit
79 81 end
80 82
81 83 # Will return true if the cache option has been set.
@@ -205,9 +207,9 @@ def extract_id
205 207 #
206 208 # @since 2.0.0
207 209 def extras(extras)
208   - clone.tap do |crit|
209   - crit.options.merge!(extras)
210   - end
  210 + crit = clone
  211 + crit.options.merge!(extras)
  212 + crit
211 213 end
212 214
213 215 # Get the list of included fields.
@@ -398,9 +400,9 @@ def inclusions=(value)
398 400 #
399 401 # @return [ Criteria ] A cloned self.
400 402 def merge(other)
401   - clone.tap do |criteria|
402   - criteria.merge!(other)
403   - end
  403 + crit = clone
  404 + crit.merge!(other)
  405 + crit
404 406 end
405 407
406 408 # Merge the other criteria into this one.
@@ -415,13 +417,12 @@ def merge(other)
415 417 # @since 3.0.0
416 418 def merge!(other)
417 419 criteria = other.to_criteria
418   - tap do |crit|
419   - crit.selector.update(criteria.selector)
420   - crit.options.update(criteria.options)
421   - crit.documents = criteria.documents.dup if criteria.documents.any?
422   - crit.scoping_options = criteria.scoping_options
423   - crit.inclusions = (crit.inclusions + criteria.inclusions.dup).uniq
424   - end
  420 + selector.update(criteria.selector)
  421 + options.update(criteria.options)
  422 + self.documents = criteria.documents.dup if criteria.documents.any?
  423 + self.scoping_options = criteria.scoping_options
  424 + self.inclusions = (inclusions + criteria.inclusions.dup).uniq
  425 + self
425 426 end
426 427
427 428 # Overriden to include _type in the fields.
@@ -511,12 +512,11 @@ def type(types)
511 512 # @since 3.0.0
512 513 def create_document(method, attrs = {})
513 514 klass.__send__(method,
514   - selector.inject(attrs) do |hash, (key, value)|
515   - hash.tap do |_attrs|
516   - unless key.to_s =~ /\$/ || value.is_a?(Hash)
517   - _attrs[key] = value
518   - end
  515 + selector.reduce(attrs) do |hash, (key, value)|
  516 + unless key.to_s =~ /\$/ || value.is_a?(Hash)
  517 + hash[key] = value
519 518 end
  519 + hash
520 520 end
521 521 )
522 522 end
49 lib/mongoid/criterion/scoping.rb
@@ -30,19 +30,18 @@ def apply_default_scope
30 30 #
31 31 # @since 3.0.0
32 32 def remove_scoping(other)
33   - tap do |criteria|
34   - if other
35   - criteria.selector.reject! do |key, value|
36   - other.selector[key] == value
37   - end
38   - criteria.options.reject! do |key, value|
39   - other.options[key] == value
40   - end
41   - other.inclusions.each do |meta|
42   - criteria.inclusions.delete_one(meta)
43   - end
  33 + if other
  34 + selector.reject! do |key, value|
  35 + other.selector[key] == value
  36 + end
  37 + options.reject! do |key, value|
  38 + other.options[key] == value
  39 + end
  40 + other.inclusions.each do |meta|
  41 + inclusions.delete_one(meta)
44 42 end
45 43 end
  44 + self
46 45 end
47 46
48 47 # Forces the criteria to be scoped, unless it's inside an unscoped block.
@@ -56,12 +55,12 @@ def remove_scoping(other)
56 55 #
57 56 # @since 3.0.0
58 57 def scoped(options = nil)
59   - clone.tap do |criteria|
60   - criteria.options.merge!(options || {})
61   - if klass.default_scopable? && !scoped?
62   - criteria.apply_default_scope
63   - end
  58 + crit = clone
  59 + crit.options.merge!(options || {})
  60 + if klass.default_scopable? && !scoped?
  61 + crit.apply_default_scope
64 62 end
  63 + crit
65 64 end
66 65
67 66 # Has the criteria had the default scope applied?
@@ -85,12 +84,12 @@ def scoped?
85 84 #
86 85 # @since 3.0.0
87 86 def unscoped
88   - clone.tap do |criteria|
89   - unless unscoped?
90   - criteria.scoping_options = false, true
91   - criteria.selector.clear; criteria.options.clear
92   - end
  87 + crit = clone
  88 + unless unscoped?
  89 + crit.scoping_options = false, true
  90 + crit.selector.clear; crit.options.clear
93 91 end
  92 + crit
94 93 end
95 94
96 95 # Is the criteria unscoped?
@@ -143,11 +142,11 @@ def scoping_options=(options)
143 142 #
144 143 # @since 3.0.0
145 144 def with_default_scope
146   - clone.tap do |criteria|
147   - if klass.default_scopable? && !unscoped? && !scoped?
148   - criteria.apply_default_scope
149   - end
  145 + crit = clone
  146 + if klass.default_scopable? && !unscoped? && !scoped?
  147 + crit.apply_default_scope
150 148 end
  149 + crit
151 150 end
152 151 end
153 152 end
38 lib/mongoid/dirty.rb
@@ -134,29 +134,29 @@ def remove_change(name)
134 134 #
135 135 # @return [ Hash ] A +Hash+ of atomic setters.
136 136 def setters
137   - {}.tap do |modifications|
138   - changes.each_pair do |name, changes|
139   - if changes
140   - old, new = changes
141   - field = fields[name]
142   - key = atomic_attribute_name(name)
143   - if field && field.resizable?
144   - field.add_atomic_changes(
145   - self,
146   - name,
147   - key,
148   - modifications,
149   - new,
150   - old
151   - )
152   - else
153   - unless atomic_unsets.include?(key)
154   - modifications[key] = new
155   - end
  137 + modifications = {}
  138 + changes.each_pair do |name, changes|
  139 + if changes
  140 + old, new = changes
  141 + field = fields[name]
  142 + key = atomic_attribute_name(name)
  143 + if field && field.resizable?
  144 + field.add_atomic_changes(
  145 + self,
  146 + name,
  147 + key,
  148 + modifications,
  149 + new,
  150 + old
  151 + )
  152 + else
  153 + unless atomic_unsets.include?(key)
  154 + modifications[key] = new
156 155 end
157 156 end
158 157 end
159 158 end
  159 + modifications
160 160 end
161 161
162 162 private
41 lib/mongoid/document.rb
@@ -68,7 +68,7 @@ def eql?(other)
68 68 #
69 69 # @since 2.0.0
70 70 def freeze
71   - tap { |doc| doc.as_document.freeze }
  71 + as_document.freeze and self
72 72 end
73 73
74 74 # Checks if the document is frozen
@@ -170,17 +170,16 @@ def to_a
170 170 #
171 171 # @return [ Hash ] A hash of all attributes in the hierarchy.
172 172 def as_document
173   - attributes.tap do |attrs|
174   - return attrs if frozen?
175   - relations.each_pair do |name, meta|
176   - if meta.embedded?
177   - without_autobuild do
178   - relation = send(name)
179   - attrs[meta.store_as] = relation.as_document unless relation.blank?
180   - end
  173 + return attributes if frozen?
  174 + relations.each_pair do |name, meta|
  175 + if meta.embedded?
  176 + without_autobuild do
  177 + relation = send(name)
  178 + attributes[meta.store_as] = relation.as_document unless relation.blank?
181 179 end
182 180 end
183 181 end
  182 + attributes
184 183 end
185 184
186 185 # Returns an instance of the specified class with the attributes
@@ -198,12 +197,12 @@ def becomes(klass)
198 197 unless klass.include?(Mongoid::Document)
199 198 raise ArgumentError, "A class which includes Mongoid::Document is expected"
200 199 end
201   - klass.instantiate(frozen? ? attributes.dup : attributes).tap do |became|
202   - became.instance_variable_set(:@errors, errors)
203   - became.instance_variable_set(:@new_record, new_record?)
204   - became.instance_variable_set(:@destroyed, destroyed?)
205   - became._type = klass.to_s
206   - end
  200 + became = klass.instantiate(frozen? ? attributes.dup : attributes)
  201 + became.instance_variable_set(:@errors, errors)
  202 + became.instance_variable_set(:@new_record, new_record?)
  203 + became.instance_variable_set(:@destroyed, destroyed?)
  204 + became._type = klass.to_s
  205 + became
207 206 end
208 207
209 208 # Print out the cache key. This will append different values on the
@@ -289,12 +288,12 @@ def ===(other)
289 288 # @return [ Document ] A new document.
290 289 def instantiate(attrs = nil)
291 290 attributes = attrs || {}
292   - allocate.tap do |doc|
293   - doc.instance_variable_set(:@attributes, attributes)
294   - doc.apply_defaults
295   - IdentityMap.set(doc) unless _loading_revision?
296   - doc.run_callbacks(:initialize) { doc }
297   - end
  291 + doc = allocate
  292 + doc.instance_variable_set(:@attributes, attributes)
  293 + doc.apply_defaults
  294 + IdentityMap.set(doc) unless _loading_revision?
  295 + doc.run_callbacks(:initialize) { doc }
  296 + doc
298 297 end
299 298
300 299 # Returns all types to query for when using this class as the base.
8 lib/mongoid/extensions/array.rb
@@ -12,11 +12,11 @@ module Array #:nodoc:
12 12 #
13 13 # @since 2.4.0
14 14 def _deep_copy
15   - [].tap do |copy|
16   - each do |value|
17   - copy.push(value._deep_copy)
18   - end
  15 + copy = []
  16 + each do |value|
  17 + copy.push(value._deep_copy)
19 18 end
  19 + copy
20 20 end
21 21
22 22 # Delete the first object in the array that is equal to the supplied
8 lib/mongoid/extensions/hash.rb
@@ -12,11 +12,11 @@ module Hash
12 12 #
13 13 # @since 2.4.0
14 14 def _deep_copy
15   - {}.tap do |copy|
16   - each_pair do |key, value|
17   - copy[key] = value._deep_copy
18   - end
  15 + copy = {}
  16 + each_pair do |key, value|
  17 + copy[key] = value._deep_copy
19 18 end
  19 + copy
20 20 end
21 21
22 22 # Get the id attribute from this hash, whether it's prefixed with an
9 lib/mongoid/extensions/object_id.rb
@@ -104,12 +104,11 @@ def convert_from_array(klass, object, reject_blank=true)
104 104 #
105 105 # @since 3.0.0
106 106 def convert_from_hash(klass, object, reject_blank=true)
107   - object.tap do |hash|
108   - hash.each_pair do |key, value|
109   - next unless klass.object_id_field?(key)
110   - hash[key] = convert(klass, value, reject_blank)
111   - end
  107 + object.each_pair do |key, value|
  108 + next unless klass.object_id_field?(key)
  109 + object[key] = convert(klass, value, reject_blank)
112 110 end
  111 + object
113 112 end
114 113
115 114 def evolve(object)
38 lib/mongoid/fields.rb
@@ -203,11 +203,11 @@ def attribute_names
203 203 def field(name, options = {})
204 204 named = name.to_s
205 205 check_field_name!(name)
206   - add_field(named, options).tap do
207   - descendants.each do |subclass|
208   - subclass.add_field(named, options)
209   - end
  206 + added = add_field(named, options)
  207 + descendants.each do |subclass|
  208 + subclass.add_field(named, options)
210 209 end
  210 + added
211 211 end
212 212
213 213 # When inheriting, we want to copy the fields from the parent class and
@@ -306,15 +306,15 @@ def add_field(name, options = {})
306 306 aliased = options[:as]
307 307 aliased_fields[aliased.to_s] = name if aliased
308 308 type = options[:localize] ? Fields::Internal::Localized : options[:type]
309   - Mappings.for(type, options[:identity]).instantiate(name, options).tap do |field|
310   - fields[name] = field
311   - add_defaults(field)
312   - create_accessors(name, name, options)
313   - create_accessors(name, aliased, options) if aliased
314   - process_options(field)
315   - create_dirty_methods(name, name)
316   - create_dirty_methods(name, aliased) if aliased
317   - end
  309 + field = Mappings.for(type, options[:identity]).instantiate(name, options)
  310 + fields[name] = field
  311 + add_defaults(field)
  312 + create_accessors(name, name, options)
  313 + create_accessors(name, aliased, options) if aliased
  314 + process_options(field)
  315 + create_dirty_methods(name, name)
  316 + create_dirty_methods(name, aliased) if aliased
  317 + field
318 318 end
319 319
320 320 # Run through all custom options stored in Mongoid::Fields.options and
@@ -400,11 +400,11 @@ def create_field_getter(name, meth, field)
400 400 end
401 401 else
402 402 re_define_method(meth) do
403   - read_attribute(name).tap do |value|
404   - if value.is_a?(Array) || value.is_a?(Hash)
405   - attribute_will_change!(name)
406   - end
  403 + value = read_attribute(name)
  404 + if value.is_a?(Array) || value.is_a?(Hash)
  405 + attribute_will_change!(name)
407 406 end
  407 + value
408 408 end
409 409 end
410 410 end
@@ -490,7 +490,9 @@ def create_translations_setter(name, meth)
490 490 # @since 2.0.0
491 491 def generated_methods
492 492 @generated_methods ||= begin
493   - Module.new.tap { |mod| include(mod) }
  493 + mod = Module.new
  494 + include(mod)
  495 + mod
494 496 end
495 497 end
496 498
14 lib/mongoid/fields/serializable.rb
@@ -251,13 +251,13 @@ module ClassMethods #:nodoc:
251 251 #
252 252 # @since 2.1.0
253 253 def instantiate(name, options = {})
254   - allocate.tap do |field|
255   - field.name = name
256   - field.options = options
257   - field.label = options[:label]
258   - field.localize = options[:localize]
259   - field.default_val = options[:default]
260   - end
  254 + field = allocate
  255 + field.name = name
  256 + field.options = options
  257 + field.label = options[:label]
  258 + field.localize = options[:localize]
  259 + field.default_val = options[:default]
  260 + field
261 261 end
262 262
263 263 private
27 lib/mongoid/hierarchy.rb
@@ -19,20 +19,23 @@ module Hierarchy #:nodoc
19 19 #
20 20 # @return [ Array<Document> ] All child documents in the hierarchy.
21 21 def _children
22   - @_children ||=
23   - [].tap do |children|
24   - relations.each_pair do |name, metadata|
25   - if metadata.embedded?
26   - without_autobuild do
27   - child = send(name)
28   - Array.wrap(child).each do |doc|
29   - children.push(doc)
30   - children.concat(doc._children) unless metadata.versioned?
31   - end if child
32   - end
33   - end
  22 + @_children ||= collect_children
  23 + end
  24 +
  25 + def collect_children
  26 + children = []
  27 + relations.each_pair do |name, metadata|
  28 + if metadata.embedded?
  29 + without_autobuild do
  30 + child = send(name)
  31 + Array.wrap(child).each do |doc|
  32 + children.push(doc)
  33 + children.concat(doc._children) unless metadata.versioned?
  34 + end if child
34 35 end
35 36 end
  37 + end
  38 + children
36 39 end
37 40
38 41 # Determines if the document is a subclass of another document.
6 lib/mongoid/indexes.rb
@@ -95,9 +95,9 @@ def index(spec)
95 95 #
96 96 # @since 3.0.0
97 97 def normalize_index_options(spec)
98   - (spec.delete(:options) || {}).tap do |opts|
99   - opts[:dropDups] = opts.delete(:drop_dups) if opts.has_key?(:drop_dups)
100   - end
  98 + opts = (spec.delete(:options) || {})
  99 + opts[:dropDups] = opts.delete(:drop_dups) if opts.has_key?(:drop_dups)
  100 + opts
101 101 end
102 102 end
103 103 end
6 lib/mongoid/loggable.rb
@@ -47,9 +47,9 @@ def logger=(logger)
47 47 #
48 48 # @since 3.0.0
49 49 def default_logger
50   - Logger.new($stdout).tap do |logger|
51   - logger.level = Logger::INFO
52   - end
  50 + logger = Logger.new($stdout)
  51 + logger.level = Logger::INFO
  52 + logger
53 53 end
54 54
55 55 # Get the Rails logger if it's defined.
1  lib/mongoid/observer.rb
@@ -176,6 +176,5 @@ def disabled_for?(object)
176 176 return false unless klass.respond_to?(:observers)
177 177 klass.observers.disabled_for?(self) || Mongoid.observers.disabled_for?(self)
178 178 end
179   -
180 179 end
181 180 end
42 lib/mongoid/persistence.rb
@@ -29,11 +29,11 @@ module Persistence
29 29 # @return [ true, false ] True if successful, false if not.
30 30 def destroy(options = {})
31 31 self.flagged_for_destroy = true
32   - run_callbacks(:destroy) do
  32 + result = run_callbacks(:destroy) do
33 33 remove(options)
34   - end.tap do
35   - self.flagged_for_destroy = false
36 34 end
  35 + self.flagged_for_destroy = false
  36 + result
37 37 end
38 38
39 39 # Insert a new document into the database. Will return the document
@@ -138,12 +138,12 @@ def update_attributes(attributes = {}, options = {})
138 138 #
139 139 # @return [ true, false ] True if validation passed.
140 140 def update_attributes!(attributes = {}, options = {})
141   - update_attributes(attributes, options).tap do |result|
142   - unless result
143   - self.class.fail_validate!(self) if errors.any?
144   - self.class.fail_callback!(self, :update_attributes!)
145   - end
  141 + result = update_attributes(attributes, options)
  142 + unless result
  143 + self.class.fail_validate!(self) if errors.any?
  144 + self.class.fail_callback!(self, :update_attributes!)
146 145 end
  146 + result
147 147 end
148 148
149 149 # Upsert the document - will perform an insert if the document is new, and
@@ -180,7 +180,9 @@ module ClassMethods #:nodoc:
180 180 # @return [ Document ] The newly created document.
181 181 def create(attributes = {}, options = {}, &block)
182 182 _creating do
183   - new(attributes, options, &block).tap { |doc| doc.save }
  183 + doc = new(attributes, options, &block)
  184 + doc.save
  185 + doc
184 186 end
185 187 end
186 188
@@ -199,10 +201,10 @@ def create(attributes = {}, options = {}, &block)
199 201 # @return [ Document ] The newly created document.
200 202 def create!(attributes = {}, options = {}, &block)
201 203 _creating do
202   - new(attributes, options, &block).tap do |doc|
203   - fail_validate!(doc) if doc.insert.errors.any?
204   - fail_callback!(doc, :create!) if doc.new_record?
205   - end
  204 + doc = new(attributes, options, &block)
  205 + fail_validate!(doc) if doc.insert.errors.any?
  206 + fail_callback!(doc, :create!) if doc.new_record?
  207 + doc
206 208 end
207 209 end
208 210
@@ -224,10 +226,10 @@ def delete_all(conditions = nil)
224 226 selector = conds[:conditions] || conds
225 227 selector.merge!(_type: name) if hereditary?
226 228 coll = collection
227   - coll.find(selector).count.tap do
228   - coll.find(selector).remove_all
229   - Threaded.clear_options!
230   - end
  229 + deleted = coll.find(selector).count
  230 + coll.find(selector).remove_all
  231 + Threaded.clear_options!
  232 + deleted
231 233 end
232 234
233 235 # Delete all documents given the supplied conditions. If no conditions
@@ -246,9 +248,9 @@ def delete_all(conditions = nil)
246 248 def destroy_all(conditions = nil)
247 249 conds = conditions || {}
248 250 documents = where(conds[:conditions] || conds)
249   - documents.count.tap do
250   - documents.each { |doc| doc.destroy }
251   - end
  251 + destroyed = documents.count
  252 + documents.each { |doc| doc.destroy }
  253 + destroyed
252 254 end
253 255
254 256 # Raise an error if validation failed.
3  lib/mongoid/persistence/atomic/add_to_set.rb
@@ -23,7 +23,8 @@ def persist
23 23 Array.wrap(value).each do |val|
24 24 values.push(val) unless values.include?(val)
25 25 end
26   - values.tap { execute("$addToSet") }
  26 + execute("$addToSet")
  27 + values
27 28 end
28 29 end
29 30
3  lib/mongoid/persistence/atomic/bit.rb
@@ -25,7 +25,8 @@ def persist
25 25 result = result | val if bit.to_s == "or"
26 26 result
27 27 end
28   - document[field].tap { execute("$bit") }
  28 + execute("$bit")
  29 + document[field]
29 30 end
30 31 end
31 32 end
3  lib/mongoid/persistence/atomic/inc.rb
@@ -19,7 +19,8 @@ def persist
19 19 prepare do
20 20 current = document[field] || 0
21 21 document[field] = current + value
22   - document[field].tap { execute("$inc") }
  22 + execute("$inc")
  23 + document[field]
23 24 end
24 25 end
25 26 end
10 lib/mongoid/persistence/atomic/operation.rb
@@ -75,9 +75,9 @@ def path
75 75 #
76 76 # @since 2.1.0
77 77 def prepare
78   - yield(document).tap do
79   - Threaded.clear_options!
80   - end
  78 + doc = yield(document)
  79 + Threaded.clear_options!
  80 + doc
81 81 end
82 82
83 83 private
@@ -113,7 +113,9 @@ def execute(name)
113 113 def append_with(name)
114 114 prepare do
115 115 document[field] = [] unless document[field]
116   - document.send(field).concat(value.__array__).tap { execute(name) }
  116 + docs = document.send(field).concat(value.__array__)
  117 + execute(name)
  118 + docs
117 119 end
118 120 end
119 121 end
3  lib/mongoid/persistence/atomic/pop.rb
@@ -21,7 +21,8 @@ def persist
21 21 if document[field]
22 22 values = document.send(field)
23 23 value > 0 ? values.pop : values.shift
24   - values.tap { execute("$pop") }
  24 + execute("$pop")
  25 + values
25 26 end
26 27 end
27 28 end
3  lib/mongoid/persistence/atomic/pull.rb
@@ -21,7 +21,8 @@ def persist
21 21 if document[field]
22 22 values = document.send(field)
23 23 values.delete(value)
24   - values.tap { execute("$pull") }
  24 + execute("$pull")
  25 + values
25 26 end
26 27 end
27 28 end
3  lib/mongoid/persistence/atomic/pull_all.rb
@@ -21,7 +21,8 @@ def persist
21 21 if document[field]
22 22 values = document.send(field)
23 23 values.delete_if { |val| value.include?(val) }
24   - values.tap { execute("$pullAll") }
  24 + execute("$pullAll")
  25 + values
25 26 end
26 27 end
27 28 end
7 lib/mongoid/persistence/atomic/rename.rb
@@ -19,10 +19,9 @@ def persist
19 19 prepare do
20 20 @value = value.to_s
21 21 document[value] = document.attributes.delete(field)
22   - document[value].tap do
23   - execute("$rename")
24   - document.remove_change(value)
25   - end
  22 + execute("$rename")
  23 + document.remove_change(value)
  24 + document[value]
26 25 end
27 26 end
28 27 end
3  lib/mongoid/persistence/atomic/sets.rb
@@ -18,7 +18,8 @@ class Sets
18 18 def persist
19 19 prepare do
20 20 document[field] = value
21   - document[field].tap { execute("$set") }
  21 + execute("$set")
  22 + document[field]
22 23 end
23 24 end
24 25 end
17 lib/mongoid/persistence/insertion.rb
@@ -19,18 +19,17 @@ module Insertion
19 19 #
20 20 # @since 2.1.0
21 21 def prepare(&block)
22   - document.tap do |doc|
23   - unless validating? && document.invalid?(:create)
24   - result = doc.run_callbacks(:save) do
25   - doc.run_callbacks(:create) do
26   - yield(doc)
27   - doc.new_record = false
28   - true
29   - end
  22 + unless validating? && document.invalid?(:create)
  23 + result = document.run_callbacks(:save) do
  24 + document.run_callbacks(:create) do
  25 + yield(document)
  26 + document.new_record = false
  27 + true
30 28 end
31   - document.post_persist unless result == false
32 29 end
  30 + document.post_persist unless result == false
33 31 end
  32 + document
34 33 end
35 34 end
36 35 end
6 lib/mongoid/persistence/modification.rb
@@ -20,13 +20,13 @@ module Modification
20 20 # @since 2.1.0
21 21 def prepare(&block)
22 22 return false if validating? && document.invalid?(:update)
23   - document.run_callbacks(:save) do
  23 + result = document.run_callbacks(:save) do
24 24 document.run_callbacks(:update) do
25 25 yield(document); true
26 26 end
27   - end.tap do |result|
28   - document.post_persist unless result == false
29 27 end
  28 + document.post_persist unless result == false
  29 + result
30 30 end
31 31 end
32 32 end
6 lib/mongoid/persistence/operations.rb
@@ -141,9 +141,9 @@ def validating?
141 141 #
142 142 # @since 2.1.0
143 143 def init_updates
144   - document.atomic_updates.tap do |updates|
145   - @conflicts = updates.delete(:conflicts) || {}
146   - end
  144 + updates = document.atomic_updates
  145 + @conflicts = updates.delete(:conflicts) || {}
  146 + updates
147 147 end
148 148
149 149 class << self
14 lib/mongoid/relations/builders.rb
@@ -67,9 +67,9 @@ def builder(name, metadata)
67 67 attributes, options = parse_args(*args)
68 68 document = Factory.build(metadata.klass, attributes, options)
69 69 _building do
70   - send("#{name}=", document).tap do |child|
71   - child.run_callbacks(:build)
72   - end
  70 + child = send("#{name}=", document)
  71 + child.run_callbacks(:build)
  72 + child
73 73 end
74 74 end
75 75 self
@@ -91,10 +91,10 @@ def creator(name, metadata)
91 91 re_define_method("create_#{name}") do |*args|
92 92 attributes, options = parse_args(*args)
93 93 document = Factory.build(metadata.klass, attributes, options)
94   - send("#{name}=", document).tap do |doc|
95   - doc.save
96   - save if new_record? && metadata.stores_foreign_key?
97   - end
  94 + doc = send("#{name}=", document)
  95 + doc.save
  96 + save if new_record? && metadata.stores_foreign_key?
  97 + doc
98 98 end
99 99 self
100 100 end
14 lib/mongoid/relations/builders/embedded/many.rb
@@ -19,15 +19,15 @@ class Many < Builder #:nodoc:
19 19 def build(type = nil)
20 20 return [] if object.blank?
21 21 return object if object.first.is_a?(Document)
22   - [].tap do |docs|
23   - object.each do |attrs|
24   - if _loading? && base.persisted?
25   - docs.push(Factory.from_db(klass, attrs))
26   - else
27   - docs.push(Factory.build(klass, attrs))
28   - end
  22 + docs = []
  23 + object.each do |attrs|
  24 + if _loading? && base.persisted?
  25 + docs.push(Factory.from_db(klass, attrs))
  26 + else
  27 + docs.push(Factory.build(klass, attrs))
29 28 end
30 29 end
  30 + docs
31 31 end
32 32 end
33 33 end
3  lib/mongoid/relations/cascading.rb
@@ -48,7 +48,8 @@ module ClassMethods #:nodoc:
48 48 #
49 49 # @since 2.0.0.rc.1
50 50 def cascade(metadata)
51   - tap { self.cascades += [ metadata.name.to_s ] if metadata.dependent? }
  51 + self.cascades += [ metadata.name.to_s ] if metadata.dependent?
  52 + self
52 53 end
53 54 end
54 55 end
7 lib/mongoid/relations/conversions.rb
@@ -23,11 +23,10 @@ def flag(object, metadata)
23 23 if inverse.using_object_ids? || object.is_a?(BSON::ObjectId)
24 24 object
25 25 else
26   - object.tap do |obj|
27   - if obj.is_a?(String)
28   - obj.unconvertable_to_bson = true
29   - end
  26 + if object.is_a?(String)
  27 + object.unconvertable_to_bson = true
30 28 end
  29 + object
31 30 end
32 31 end
33 32 end
17 lib/mongoid/relations/embedded/in.rb
@@ -37,16 +37,15 @@ def initialize(base, target, metadata)
37 37 #
38 38 # @since 2.0.0.rc.1
39 39 def substitute(replacement)
40   - tap do |proxy|
41   - proxy.unbind_one
42   - unless replacement
43   - base.delete if persistable?
44   - return nil
45   - end
46   - base.new_record = true
47   - proxy.target = replacement
48   - proxy.bind_one
  40 + unbind_one
  41 + unless replacement
  42 + base.delete if persistable?
  43 + return nil
49 44 end
  45 + base.new_record = true
  46 + self.target = replacement
  47 + bind_one
  48 + self
50 49 end