Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#camelize_keys(first_letter = :lower) ⇒ Object

Returns a new hash with all keys converted to camelcase.

hash = { first_name: "Surim", last_name: "Kim" }

hash.camelize_keys
# => {:firstName=>"Surim", :lastName=>"Kim"}


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

def camelize_keys(first_letter = :lower)
  transform_keys { |key| camelize_key(key, first_letter) }
end

#deep_camelize_keys(first_letter = :lower) ⇒ Object

Returns a new hash with all keys converted to camelcase. This includes the keys from the root hash and from all nested hashes and arrays.

hash = { person: { first_name: "Surim", last_name: "Kim" } }

hash.deep_camelize_keys
# => {:person=>{:firstName=>"Surim", :lastName=>"Kim"}}


42
43
44
# File 'lib/hashash/core_ext/hash/keys.rb', line 42

def deep_camelize_keys(first_letter = :lower)
  deep_transform_keys_in_object(self) { |key| camelize_key(key, first_letter) }
end

#deep_underscore_keysObject

Returns a new hash with all keys converted to underscore. This includes the keys from the root hash and from all nested hashes and arrays.

hash = { person: { firstName: "Surim", lastName: "Kim" } }

hash.deep_underscore_keys
# => {:person=>{:first_name=>"Surim", :last_name=>"Kim"}}


30
31
32
# File 'lib/hashash/core_ext/hash/keys.rb', line 30

def deep_underscore_keys
  deep_transform_keys_in_object(self) { |key| underscore_key(key) }
end

#underscore_keysObject

Returns a new hash with all keys converted to underscore.

hash = { firstName: "Surim", lastName: "Kim" }

hash.underscore_keys
# => {:first_name=>"Surim", :last_name=>"Kim"}


8
9
10
# File 'lib/hashash/core_ext/hash/keys.rb', line 8

def underscore_keys
  transform_keys { |key| underscore_key(key) }
end