Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/github_api/core_ext/hash.rb

Overview

:nodoc:

Direct Known Subclasses

Github::ParamsHash

Instance Method Summary collapse

Instance Method Details

#deep_key?(key) ⇒ Boolean

Returns true if the given key is present inside deeply nested hash

Returns:

  • (Boolean)


67
68
69
# File 'lib/github_api/core_ext/hash.rb', line 67

def deep_key?(key)
  self.deep_keys.include? key
end

#deep_keysObject

Searches for all deeply nested keys



54
55
56
57
58
59
60
61
62
63
# File 'lib/github_api/core_ext/hash.rb', line 54

def deep_keys
  keys = self.keys
  keys.each do |key|
    if self[key].is_a?(Hash)
      keys << self[key].deep_keys.compact.flatten
      next
    end
  end
  keys.flatten
end

#deep_merge(other, &block) ⇒ Object

Recursively merges self with other hash and returns new hash.



73
74
75
# File 'lib/github_api/core_ext/hash.rb', line 73

def deep_merge(other, &block)
  dup.deep_merge!(other, &block)
end

#deep_merge!(other, &block) ⇒ Object

Similar as deep_merge but modifies self



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/github_api/core_ext/hash.rb', line 79

def deep_merge!(other, &block)
  other.each_pair do |key, val|
    tval = self[key]
    if tval.is_a?(Hash) && val.is_a?(Hash)
      self[key] = tval.deep_merge(val)
    else
      self[key] = block && tval ? block.call(k, tval, val) : val
    end
  end
  self
end

#except(*items) ⇒ Object

Returns a new hash with keys removed



7
8
9
# File 'lib/github_api/core_ext/hash.rb', line 7

def except(*items)
  self.dup.except!(*items)
end

#except!(*keys) ⇒ Object

Similar to except but modifies self



13
14
15
16
# File 'lib/github_api/core_ext/hash.rb', line 13

def except!(*keys)
  keys.each { |key| delete(key) }
  self
end

#serializeObject

Returns hash collapsed into a query string



48
49
50
# File 'lib/github_api/core_ext/hash.rb', line 48

def serialize
  self.map { |key, val| [key, val].join("=") }.join("&")
end

#symbolize_keysObject

Returns a new hash with all the keys converted to symbols



20
21
22
23
24
25
# File 'lib/github_api/core_ext/hash.rb', line 20

def symbolize_keys
  inject({}) do |hash, (key, value)|
    hash[(key.to_sym rescue key) || key] = value
    hash
  end
end

#symbolize_keys!Object

Similar to symbolize_keys but modifies self



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/github_api/core_ext/hash.rb', line 29

def symbolize_keys!
  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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy