mongo-ruby-driver/lib/mongo/util/core_ext.rb
Mislav Marohnić 56f37e49b6 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.
2010-09-13 01:10:38 +08:00

51 lines
1.1 KiB
Ruby

# encoding: UTF-8
# --
# Copyright (C) 2008-2010 10gen Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ++
#:nodoc:
class Object
#:nodoc:
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
#:nodoc:
class Hash
#:nodoc:
def assert_valid_keys(*valid_keys)
unknown_keys = keys - [valid_keys].flatten
raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
end
end