Sha256: fab5d54798f5718547617b2550691353a3b93bdeb6dd75bbf610f76f53e18d7c

Contents?: true

Size: 1.16 KB

Versions: 29

Compression:

Stored size: 1.16 KB

Contents

# encoding: UTF-8
#
# Copyright (c) 2010-2015 GoodData Corporation. All rights reserved.
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

class Hash
  # Return a hash that includes everything but the given keys. This is useful for
  # limiting a set of parameters to everything but a few known toggles:
  #
  #   @person.update_attributes(params[:person].except(:admin))
  #
  # If the receiver responds to +convert_key+, the method is called on each of the
  # arguments. This allows +except+ to play nice with hashes with indifferent access
  # for instance:
  #
  #   {:a => 1}.with_indifferent_access.except(:a)  # => {}
  #   {:a => 1}.with_indifferent_access.except("a") # => {}
  #
  def except(*keys)
    dup.except!(*keys)
  end

  # Replaces the hash without the given keys.
  def except!(*keys)
    keys.each { |key| delete(key) }
    self
  end

  def slice(*keys)
    keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
    keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if key?(k) }
  end

  def compact
    select { |_, value| !value.nil? }
  end
end

Version data entries

29 entries across 29 versions & 2 rubygems

Version Path
gooddata-0.6.30 lib/gooddata/extensions/hash.rb
gooddata-0.6.29 lib/gooddata/extensions/hash.rb
gooddata-0.6.28 lib/gooddata/extensions/hash.rb
gooddata-0.6.27 lib/gooddata/extensions/hash.rb
gooddata-0.6.26 lib/gooddata/extensions/hash.rb
gooddata-0.6.25 lib/gooddata/extensions/hash.rb
gooddata-0.6.24 lib/gooddata/extensions/hash.rb
gooddata-0.6.23 lib/gooddata/extensions/hash.rb
gooddata-0.6.22 lib/gooddata/extensions/hash.rb