Sha256: a88b5c46d19f812b8cb257990e6bb2398e2ec46b65d1733028fd2e10b56390e6

Contents?: true

Size: 843 Bytes

Versions: 82

Compression:

Stored size: 843 Bytes

Contents

require 'set'

module ActiveSupport #:nodoc:
  module CoreExtensions #:nodoc:
    module Hash #:nodoc:
      # Slice a hash to include only the given keys. This is useful for
      # limiting an options hash to valid keys before passing to a method:
      #
      #   def search(criteria = {})
      #     assert_valid_keys(:mass, :velocity, :time)
      #   end
      #
      #   search(options.slice(:mass, :velocity, :time))
      module Slice
        # Returns a new hash with only the given keys.
        def slice(*keys)
          allowed = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)
          reject { |key,| !allowed.include?(key) }
        end

        # Replaces the hash with only the given keys.
        def slice!(*keys)
          replace(slice(*keys))
        end
      end
    end
  end
end

Version data entries

82 entries across 82 versions & 7 rubygems

Version Path
swivel-0.0.175 vendor/activesupport-2.0.2-/lib/active_support/core_ext/hash/slice.rb
swivel-0.0.160 vendor/activesupport-2.0.2-/lib/active_support/core_ext/hash/slice.rb