From 56f37e49b62832cc0445edbcf53034236422bba6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Sat, 4 Sep 2010 21:14:46 +0800 Subject: [PATCH] replace usage of `returning` with `each_with_object` `each_with_object` is a Ruby 1.9 method, here re-implemented in core_ext.rb in case it's missing (for older Ruby versions). Using `returning` is bad in combination with Ruby on Rails because each usage of the method will emit a Rails deprecation warning. This might be considered an Active Support bug, but it's better to avoid using `returning` altogether and use `tap` from Ruby 1.8.7, also re-implemented here in case it's missing. Since existing usages or `returning` were better suited for `each_with_object` than `tap`, they were rewritten using the former instead. --- lib/mongo/connection.rb | 4 ++-- lib/mongo/cursor.rb | 4 +--- lib/mongo/util/core_ext.rb | 19 +++++++++++++++---- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/mongo/connection.rb b/lib/mongo/connection.rb index 8b8b3e7..c88983b 100644 --- a/lib/mongo/connection.rb +++ b/lib/mongo/connection.rb @@ -277,8 +277,8 @@ module Mongo # @return [Hash] def database_info doc = self['admin'].command({:listDatabases => 1}) - returning({}) do |info| - doc['databases'].each { |db| info[db['name']] = db['sizeOnDisk'].to_i } + doc['databases'].each_with_object({}) do |db, info| + info[db['name']] = db['sizeOnDisk'].to_i end end diff --git a/lib/mongo/cursor.rb b/lib/mongo/cursor.rb index a2e1a04..a0ed322 100644 --- a/lib/mongo/cursor.rb +++ b/lib/mongo/cursor.rb @@ -321,9 +321,7 @@ module Mongo {fields => 1} when Array return nil if fields.length.zero? - returning({}) do |hash| - fields.each { |field| hash[field] = 1 } - end + fields.each_with_object({}) { |field, hash| hash[field] = 1 } when Hash return fields end diff --git a/lib/mongo/util/core_ext.rb b/lib/mongo/util/core_ext.rb index 7f19f05..bc034e5 100644 --- a/lib/mongo/util/core_ext.rb +++ b/lib/mongo/util/core_ext.rb @@ -20,10 +20,21 @@ class Object #:nodoc: - def returning(value) - yield value - value - end + def tap + yield self + self + end unless respond_to? :tap + +end + +#:nodoc: +module Enumerable + + #:nodoc: + def each_with_object(memo) + each { |element| yield(element, memo) } + memo + end unless [].respond_to?(:each_with_object) end