Class: Hash

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

Defined Under Namespace

Classes: DeepHashConstructor, KeyChanger, ValueChanger

Instance Method Summary collapse

Instance Method Details

#append_to_keys(str, options = {}) ⇒ Object

append a string to all keys options

recursive: true,
type: :keep [keep, string, symbol] (key type to be returned)

ex: { :a => 1, “b”=> 2 }.append_to_keys(“_bar”) == { :a_bar => 1, “b_bar”=> 2 }



141
142
143
144
145
# File 'lib/hash.rb', line 141

def append_to_keys(str, options = {})
  change_key_text(options) do |key|
    "#{key}#{str}"
  end
end

#camelize_keys(options = {}) ⇒ Object



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

def camelize_keys(options = {})
  dup.camelize_keys!(options)
end

#camelize_keys!(options = {}) ⇒ Object



71
72
73
# File 'lib/hash.rb', line 71

def camelize_keys!(options = {})
  Hash::KeyChanger.new(self).camelize_keys(options)
end

#chain_change_keys(*calls) ⇒ Object

change all publicaly sending method calls options: { recursive: true } ex: { a: 1 }.chain_change_keys(:to_s, :upcase) == { “A” =>1 }



108
109
110
# File 'lib/hash.rb', line 108

def chain_change_keys(*calls)
  deep_dup.chain_change_keys!(*calls)
end

#chain_change_keys!(*calls) ⇒ Object

change all publicaly sending method calls options: { recursive: true } ex: { a: 1 }.chain_change_keys(:to_s, :upcase) == { “A” =>1 }



115
116
117
118
119
120
121
# File 'lib/hash.rb', line 115

def chain_change_keys!(*calls)
  options = calls.extract_options!

  calls.inject(self) do |h, m|
    h.change_keys!(options, &m)
  end
end

#chain_fetch(*keys) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/hash.rb', line 6

def chain_fetch(*keys)
  value = self

  if block_given?
    value = value.fetch(keys.shift) do |*args|
      missed_keys = keys
      keys = []
      yield(*(args + [missed_keys]))
    end until keys.empty?
  else
    value = value.fetch(keys.shift) until keys.empty?
  end

  value
end

#change_keys(options = {}, &block) ⇒ Object

change all keys returning the new map options: { recursive: true } ex: { “a” =>1 }.change_keys{ |key| key.upcase } == { “A” => 1 }



94
95
96
# File 'lib/hash.rb', line 94

def change_keys(options = {}, &block)
  deep_dup.change_keys!(options, &block)
end

#change_keys!(options = {}, &block) ⇒ Object

change all keys returning the new map options: { recursive: true } ex: { “a”:1 }.change_keys{ |key| key.upcase } == { “A”:1 }



101
102
103
# File 'lib/hash.rb', line 101

def change_keys!(options = {}, &block)
  Hash::KeyChanger.new(self).change_keys(options, &block)
end

#change_values(options = {}, &block) ⇒ Object

creates a new hash with changes in its values options:

recursive: true,
skip_hash:true

ex: { a:1, b:2 }.change_values{ |v| v+1 } == { a:2, b:3 } ex: { a:1, b:{ c:1 } }.change_values(skip_hash:false) { |v| v.to_s } == { a:“1”, b:“{ c=>1 } ex: { a:1, b:{ c:1 } }.change_values(skip_hash:true) { |v| v.to_s } == { a:”1“, b:{ c=>”1“ } }



172
173
174
# File 'lib/hash.rb', line 172

def change_values(options = {}, &block)
  deep_dup.change_values!(options, &block)
end

#change_values!(options = {}, &block) ⇒ Object



176
177
178
# File 'lib/hash.rb', line 176

def change_values!(options = {}, &block)
  Hash::ValueChanger.new(options, &block).change(self)
end

#exclusive_merge(hash) ⇒ Object



83
84
85
# File 'lib/hash.rb', line 83

def exclusive_merge(hash)
  dup.exclusive_merge!(hash)
end

#exclusive_merge!(hash) ⇒ Object



87
88
89
# File 'lib/hash.rb', line 87

def exclusive_merge!(hash)
  merge!(hash.slice(*keys))
end

#lower_camelize_keys(options = {}) ⇒ Object



57
58
59
# File 'lib/hash.rb', line 57

def lower_camelize_keys(options = {})
  dup.lower_camelize_keys!(options)
end

#lower_camelize_keys!(options = {}) ⇒ Object



61
62
63
64
65
# File 'lib/hash.rb', line 61

def lower_camelize_keys!(options = {})
  options = options.merge({ uppercase_first_letter: false })

  camelize_keys!(options)
end

#map_to_hashObject



37
38
39
40
41
42
43
# File 'lib/hash.rb', line 37

def map_to_hash
  {}.tap do |hash|
    each do |k, v|
      hash[k] = yield(k, v)
    end
  end
end

#prepend_to_keys(str, options = {}) ⇒ Object

prepend a string to all keys options

recursive: true,
type: :keep [keep, string, symbol] (key type to be returned)

ex: { :a => 1, “b”=> 2 }.prepend_to_keys(“foo_”) == { :foo_a => 1, “foo_b”=> 2 }



129
130
131
132
133
# File 'lib/hash.rb', line 129

def prepend_to_keys(str, options = {})
  change_key_text(options) do |key|
    "#{str}#{key}"
  end
end

#remap_keys(remap) ⇒ Object



45
46
47
# File 'lib/hash.rb', line 45

def remap_keys(remap)
  dup.remap_keys!(remap)
end

#remap_keys!(remap) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/hash.rb', line 49

def remap_keys!(remap)
  new_hash = {}
  remap.each do |o, n|
    new_hash[n] = delete o
  end
  merge! new_hash
end

#sort_keys(options = {}) ⇒ Object

sorts keys for hash options: { recursive: true } ex: { b:1, a:2 }.sort_keys == { a:2, b:1 }



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/hash.rb', line 150

def sort_keys(options = {})
  options = {
    recursive: true
  }.merge(options)

  {}.tap do |hash|
    keys.sort.each do |key|
      value = self[key]
      hash[key] = value unless value.is_a?(Hash) && options[:recursive]
      hash[key] = value.sort_keys(options) if value.is_a?(Hash) && options[:recursive]
    end
  end
end

#squashObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hash.rb', line 22

def squash
  {}.tap do |hash|
    each do |key, value|
      if value.is_a? Hash
        value.squash.each do |k, v|
          new_key = [key, k].join('.')
          hash[new_key] = v
        end
      else
        hash[key] = value
      end
    end
  end
end

#to_deep_hash(separator = '.') ⇒ Object



180
181
182
# File 'lib/hash.rb', line 180

def to_deep_hash(separator = '.')
  Hash::DeepHashConstructor.new(separator).deep_hash(self)
end

#underscore_keys(options = {}) ⇒ Object



75
76
77
# File 'lib/hash.rb', line 75

def underscore_keys(options = {})
  dup.underscore_keys!(options)
end

#underscore_keys!(options = {}) ⇒ Object



79
80
81
# File 'lib/hash.rb', line 79

def underscore_keys!(options = {})
  Hash::KeyChanger.new(self).underscore_keys(options)
end