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
backlog-0.5.9 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.5.8 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.6.2 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.6.0 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.6.1 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.6.4 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.6.3 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.6.5 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.6.6 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.7.0 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.7.1 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.7.10 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.7.12 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.7.11 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.7.2 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.7.3 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.7.5 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.7.4 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.7.7 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb
backlog-0.7.6 vendor/rails/activesupport/lib/active_support/core_ext/hash/slice.rb