Skip to content

Commit

Permalink
Merge pull request #8 from benSlaughter/transform_keys_update
Browse files Browse the repository at this point in the history
Transform keys update
  • Loading branch information
benSlaughter committed Oct 6, 2015
2 parents 1a13f8d + 6094581 commit 756b705
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
16 changes: 8 additions & 8 deletions lib/utilise/augment/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@ module Augment
module Hash
# Transforms all keys to snake case
def snake_keys
transform_keys(self) { |key| key.snake }
utilise_deep_transform_keys(self) { |key| key.snake }
end

# Transforms all keys to camel case
def camel_keys
transform_keys(self) { |key| key.camel }
utilise_deep_transform_keys(self) { |key| key.camel }
end

# Transforms all keys to space case
def space_keys
transform_keys(self) { |key| key.space }
utilise_deep_transform_keys(self) { |key| key.space }
end

private

# Deep transform keys
# Deep transform keys in object
# An awesome piece of code from rails
# apidock.com/rails/v4.2.1/Hash/_deep_transform_keys_in_object%21
def transform_keys(object, &block)
# http://apidock.com/rails/Hash/_deep_transform_keys_in_object
def utilise_deep_transform_keys(object, &block)
case object
when Hash
object.each_with_object({}) do |(key, value), result|
result[yield(key)] = transform_keys(value, &block)
result[yield(key)] = utilise_deep_transform_keys(value, &block)
end
when Array
object.map { |e| transform_keys(e, &block) }
object.map { |e| utilise_deep_transform_keys(e, &block) }
else
object
end
Expand Down
4 changes: 2 additions & 2 deletions lib/utilise/version.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Utilise
module Utilise
# The current gem version
VERSION = '0.6.1'
VERSION = '0.6.2'
# The version update date
DATE = '2015-09-16'
DATE = '2015-10-06'
# Debug output message
MSG = 'Version %s %s (running on %s-%s)'

Expand Down
2 changes: 1 addition & 1 deletion spec/utilise/version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe Utilise do
describe '.version' do
it 'should return the current gem version' do
expect(Utilise.version).to eq('0.6.1')
expect(Utilise.version).to eq('0.6.2')
end

it 'should return the current gem version with debug information' do
Expand Down

0 comments on commit 756b705

Please sign in to comment.