Sha256: f77c26f82e4b43f832d57e1d8a94d1a9405fa3b3b15f0b7d6e8e0e90f0dba028

Contents?: true

Size: 1.62 KB

Versions: 47

Compression:

Stored size: 1.62 KB

Contents

class Hash # :nodoc:

  def except(*items) # :nodoc:
    self.dup.except!(*items)
  end unless method_defined?(:except)

  def except!(*keys) # :nodoc:
    copy = self.dup
    keys.each { |key| copy.delete!(key) }
    copy
  end unless method_defined?(:except!)

  def symbolize_keys  # :nodoc:
    inject({}) do |hash, (key, value)|
      hash[(key.to_sym rescue key) || key] = value
      hash
    end
  end unless method_defined?(:symbolize_keys)

  def symbolize_keys! # :nodoc:
    hash = symbolize_keys
    hash.each do |key, val|
      hash[key] = case val
        when Hash
          val.symbolize_keys!
        when Array
          val.map do |item|
            item.is_a?(Hash) ? item.symbolize_keys! : item
          end
        else
          val
        end
    end
    return hash
  end unless method_defined?(:symbolize_keys!)

  def serialize # :nodoc:
    self.map { |key, val| [key, val].join("=") }.join("&")
  end unless method_defined?(:serialize)

  def all_keys # :nodoc:
    keys = self.keys
    keys.each do |key|
      if self[key].is_a?(Hash)
        keys << self[key].all_keys.compact.flatten
        next
      end
    end
    keys.flatten
  end unless method_defined?(:all_keys)

  def has_deep_key?(key)
    self.all_keys.include? key
  end unless method_defined?(:has_deep_key?)

  def self.hash_traverse(hash, &block)
    hash.each do |key, val|
      block.call(key)
      case val
      when Hash
        val.keys.each do |k|
          _hash_traverse(val, &block)
        end
      when Array
        val.each do |item|
          _hash_traverse(item, &block)
        end
      end
    end
    return hash
  end

end # Hash

Version data entries

47 entries across 47 versions & 2 rubygems

Version Path
nimbu-api-0.5.0 lib/nimbu-api/utils/extend_hash.rb
nimbu-api-0.4.4 lib/nimbu-api/utils/extend_hash.rb
nimbu-api-0.4.3 lib/nimbu-api/utils/extend_hash.rb
nimbu-api-0.4.2 lib/nimbu-api/utils/extend_hash.rb
nimbu-api-0.4.1 lib/nimbu-api/utils/extend_hash.rb
nimbu-api-0.4.0 lib/nimbu-api/utils/extend_hash.rb
nimbu-api-0.3.0 lib/nimbu-api/utils/extend_hash.rb
github_api-0.11.2 lib/github_api/core_ext/hash.rb
nimbu-api-0.2.1 lib/nimbu-api/utils/extend_hash.rb
nimbu-api-0.2 lib/nimbu-api/utils/extend_hash.rb
github_api-0.11.1 lib/github_api/core_ext/hash.rb
github_api-0.11.0 lib/github_api/core_ext/hash.rb
nimbu-api-0.2.beta.3 lib/nimbu-api/utils/extend_hash.rb
nimbu-api-0.2.beta.2 lib/nimbu-api/utils/extend_hash.rb
nimbu-api-0.2.beta.1 lib/nimbu-api/utils/extend_hash.rb
nimbu-api-0.2.beta lib/nimbu-api/utils/extend_hash.rb
nimbu-api-0.1.5 lib/nimbu-api/utils/extend_hash.rb
nimbu-api-0.1.4 lib/nimbu-api/utils/extend_hash.rb
nimbu-api-0.1.3 lib/nimbu-api/utils/extend_hash.rb
github_api-0.10.2 lib/github_api/core_ext/hash.rb