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-edge-0.6.27.edge lib/gooddata/extensions/hash.rb
gooddata-0.6.49 lib/gooddata/extensions/hash.rb
gooddata-0.6.48 lib/gooddata/extensions/hash.rb
gooddata-0.6.47 lib/gooddata/extensions/hash.rb
gooddata-0.6.46 lib/gooddata/extensions/hash.rb
gooddata-0.6.45 lib/gooddata/extensions/hash.rb
gooddata-0.6.44 lib/gooddata/extensions/hash.rb
gooddata-0.6.43 lib/gooddata/extensions/hash.rb
gooddata-0.6.42 lib/gooddata/extensions/hash.rb
gooddata-0.6.41 lib/gooddata/extensions/hash.rb
gooddata-0.6.40 lib/gooddata/extensions/hash.rb
gooddata-0.6.39 lib/gooddata/extensions/hash.rb
gooddata-0.6.38 lib/gooddata/extensions/hash.rb
gooddata-0.6.37 lib/gooddata/extensions/hash.rb
gooddata-0.6.36 lib/gooddata/extensions/hash.rb
gooddata-0.6.35 lib/gooddata/extensions/hash.rb
gooddata-0.6.34 lib/gooddata/extensions/hash.rb
gooddata-0.6.33 lib/gooddata/extensions/hash.rb
gooddata-0.6.32 lib/gooddata/extensions/hash.rb
gooddata-0.6.31 lib/gooddata/extensions/hash.rb