Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#camelize(first_letter = :upper) ⇒ Object



11
12
13
# File 'lib/hash-tweaks.rb', line 11

def camelize(first_letter = :upper)
  dup.camelize!(first_letter)
end

#camelize!(first_letter = :upper) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/hash-tweaks.rb', line 17

def camelize!(first_letter = :upper)
  keys.each do |key|
    # Check if key is symbol or string and if it is not CONSTANT_VARIABLE.
    if (Symbol === key || String === key) && !key.match?(/\A[A-Z_][A-Z_0-9]*\z/)
      new_key = (Symbol === key ? key.to_s : key).camelize(first_letter)
      self[Symbol === key ? new_key.to_sym : new_key] = self.delete(key)
    end
  end
  self
end

#deep_with_indifferent_accessObject



30
31
32
33
34
# File 'lib/hash-tweaks.rb', line 30

def deep_with_indifferent_access
  each_with_object({}) do |(k, v), m|
    m[k] = v.respond_to?(:deep_with_indifferent_access) ? v.deep_with_indifferent_access : v
  end.with_indifferent_access
end