Class: Hash

Inherits:
Object show all
Defined in:
lib/happy_support/core_ext/hash/keys.rb,
lib/happy_support/core_ext/hash/slice.rb,
lib/happy_support/core_ext/hash/except.rb,
lib/happy_support/core_ext/object/blank.rb,
lib/happy_support/core_ext/hash/deep_merge.rb,
lib/happy_support/core_ext/object/to_param.rb,
lib/happy_support/core_ext/object/to_query.rb,
lib/happy_support/core_ext/hash/reverse_merge.rb,
lib/happy_support/core_ext/array/extract_options.rb

Instance Method Summary collapse

Instance Method Details

#deep_merge(other_hash, &block) ⇒ Object



3
4
5
# File 'lib/happy_support/core_ext/hash/deep_merge.rb', line 3

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

#deep_merge!(other_hash, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/happy_support/core_ext/hash/deep_merge.rb', line 7

def deep_merge!(other_hash, &block)
  other_hash.each_pair do |k,v|
    tv = self[k]
    if tv.is_a?(Hash) && v.is_a?(Hash)
      self[k] = tv.deep_merge(v, &block)
    else
      self[k] = block && tv ? block.call(k, tv, v) : v
    end
  end
  self
end

#deep_stringify_keysObject



52
53
54
# File 'lib/happy_support/core_ext/hash/keys.rb', line 52

def deep_stringify_keys
  deep_transform_keys{ |key| key.to_s }
end

#deep_stringify_keys!Object



56
57
58
# File 'lib/happy_support/core_ext/hash/keys.rb', line 56

def deep_stringify_keys!
  deep_transform_keys!{ |key| key.to_s }
end

#deep_symbolize_keysObject



60
61
62
# File 'lib/happy_support/core_ext/hash/keys.rb', line 60

def deep_symbolize_keys
  deep_transform_keys{ |key| key.to_sym rescue key }
end

#deep_symbolize_keys!Object



64
65
66
# File 'lib/happy_support/core_ext/hash/keys.rb', line 64

def deep_symbolize_keys!
  deep_transform_keys!{ |key| key.to_sym rescue key }
end

#deep_transform_keys(&block) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/happy_support/core_ext/hash/keys.rb', line 36

def deep_transform_keys(&block)
  result = {}
  each do |key, value|
    result[yield(key)] = value.is_a?(Hash) ? value.deep_transform_keys(&block) : value
  end
  result
end

#deep_transform_keys!(&block) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/happy_support/core_ext/hash/keys.rb', line 44

def deep_transform_keys!(&block)
  keys.each do |key|
    value = delete(key)
    self[yield(key)] = value.is_a?(Hash) ? value.deep_transform_keys!(&block) : value
  end
  self
end

#except(*keys) ⇒ Object



3
4
5
# File 'lib/happy_support/core_ext/hash/except.rb', line 3

def except(*keys)
  dup.except!(*keys)
end

#except!(*keys) ⇒ Object



7
8
9
10
# File 'lib/happy_support/core_ext/hash/except.rb', line 7

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

#extract!(*keys) ⇒ Object



16
17
18
# File 'lib/happy_support/core_ext/hash/slice.rb', line 16

def extract!(*keys)
  keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) }
end

#extractable_options?Boolean

Returns:

  • (Boolean)


2
3
4
# File 'lib/happy_support/core_ext/array/extract_options.rb', line 2

def extractable_options?
  instance_of?(Hash)
end

#reverse_merge(other_hash) ⇒ Object



3
4
5
# File 'lib/happy_support/core_ext/hash/reverse_merge.rb', line 3

def reverse_merge(other_hash)
  other_hash.merge(self)
end

#reverse_merge!(other_hash) ⇒ Object Also known as: reverse_update



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

def reverse_merge!(other_hash)
  merge!( other_hash ){|key,left,right| left }
end

#slice(*keys) ⇒ Object



3
4
5
6
# File 'lib/happy_support/core_ext/hash/slice.rb', line 3

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 has_key?(k) }
end

#slice!(*keys) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/happy_support/core_ext/hash/slice.rb', line 8

def slice!(*keys)
  keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
  omit = slice(*self.keys - keys)
  hash = slice(*keys)
  replace(hash)
  omit
end

#stringify_keysObject



18
19
20
# File 'lib/happy_support/core_ext/hash/keys.rb', line 18

def stringify_keys
  transform_keys{ |key| key.to_s }
end

#stringify_keys!Object



22
23
24
# File 'lib/happy_support/core_ext/hash/keys.rb', line 22

def stringify_keys!
  transform_keys!{ |key| key.to_s }
end

#symbolize_keysObject Also known as: to_options



26
27
28
# File 'lib/happy_support/core_ext/hash/keys.rb', line 26

def symbolize_keys
  transform_keys{ |key| key.to_sym rescue key }
end

#symbolize_keys!Object Also known as: to_options!



31
32
33
# File 'lib/happy_support/core_ext/hash/keys.rb', line 31

def symbolize_keys!
  transform_keys!{ |key| key.to_sym rescue key }
end

#to_param(namespace = nil) ⇒ Object Also known as: to_query



32
33
34
35
36
# File 'lib/happy_support/core_ext/object/to_param.rb', line 32

def to_param(namespace = nil)
  collect do |key, value|
    value.to_query(namespace ? "#{namespace}[#{key}]" : key)
  end.sort * '&'
end

#transform_keysObject



3
4
5
6
7
8
9
# File 'lib/happy_support/core_ext/hash/keys.rb', line 3

def transform_keys
  result = {}
  each_key do |key|
    result[yield(key)] = self[key]
  end
  result
end

#transform_keys!Object



11
12
13
14
15
16
# File 'lib/happy_support/core_ext/hash/keys.rb', line 11

def transform_keys!
  keys.each do |key|
    self[yield(key)] = delete(key)
  end
  self
end