Skip to content

Commit

Permalink
Merge pull request #10 from benSlaughter/modify-hash-keys
Browse files Browse the repository at this point in the history
Added functionality to modify hash keys
  • Loading branch information
benSlaughter authored Feb 7, 2017
2 parents e3bcc13 + fd80def commit 7194f65
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Metrics/LineLength:
- '*.gemspec'
- 'spec/**/*_spec.rb'

Metrics/BlockLength:
Exclude:
- 'spec/**/*_spec.rb'

Style/SymbolProc:
Exclude:
- 'lib/utilise/augment/hash.rb'
2 changes: 1 addition & 1 deletion lib/utilise.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'utilise/array'
require 'utilise/fixnum'
require 'utilise/hash'
require 'utilise/hashie'
require 'utilise/integer'
require 'utilise/object'
require 'utilise/string'
require 'utilise/symbol'
Expand Down
10 changes: 10 additions & 0 deletions lib/utilise/augment/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ def space_keys
utilise_deep_transform_keys(self) { |key| key.space }
end

# Transforms all keys to a string
def string_keys
utilise_deep_transform_keys(self) { |key| key.to_s }
end

# Transforms all keys to a symbol
def symbol_keys
utilise_deep_transform_keys(self) { |key| key.to_sym }
end

private

# Deep transform keys in object
Expand Down
File renamed without changes.
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 = '1.0.0'.freeze
VERSION = '1.1.0'.freeze
# The version update date
DATE = '2015-10-06'.freeze
DATE = '2017-02-07'.freeze
# Debug output message
MSG = 'Version %s %s (running on %s-%s)'.freeze

Expand Down
86 changes: 86 additions & 0 deletions spec/utilise/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,90 @@
expect(old_hash['red key'][:blueKey]).to eql('testing 123')
end
end

describe '#string_keys' do
let(:hash) { { oneKey: 'aString', two_key: ['a_string'], redKey: { blue_key: 'testing 123' } } }

it 'converts all keys to a string' do
new_hash = hash.string_keys
expect(new_hash.key?('oneKey')).to be_truthy
expect(new_hash.key?('two_key')).to be_truthy
expect(new_hash.key?('redKey')).to be_truthy
expect(new_hash['redKey'].key?('blue_key')).to be_truthy
end

it 'does not keep the old keys' do
new_hash = hash.string_keys
expect(new_hash.key?(:oneKey)).to be_falsey
expect(new_hash.key?(:two_key)).to be_falsey
expect(new_hash.key?(:redKey)).to be_falsey
expect(new_hash['redKey'].key?(:blue_key)).to be_falsey
end

it 'does not convert the values' do
new_hash = hash.string_keys

expect(new_hash['oneKey']).to eql(hash[:oneKey])
expect(new_hash['two_key']).to eql(hash[:two_key])
expect(new_hash['redKey']['blue_key']).to eql(hash[:redKey][:blue_key])
end

it 'does not modify the original hash' do
new_hash = hash
new_hash.string_keys

expect(new_hash.key?(:oneKey)).to be_truthy
expect(new_hash.key?(:two_key)).to be_truthy
expect(new_hash.key?(:redKey)).to be_truthy
expect(new_hash[:redKey].key?(:blue_key)).to be_truthy

expect(new_hash[:oneKey]).to be(hash[:oneKey])
expect(new_hash[:two_key]).to be(hash[:two_key])
expect(new_hash[:redKey]).to be(hash[:redKey])
expect(new_hash[:redKey][:blue_key]).to be(hash[:redKey][:blue_key])
end
end

describe '#symbol_keys' do
let(:hash) { { 'oneKey' => 'aString', 'two_key' => ['a_string'], 'redKey' => { 'blue_key' => 'testing 123' } } }

it 'converts all keys to a symbol' do
new_hash = hash.symbol_keys
expect(new_hash.key?(:oneKey)).to be_truthy
expect(new_hash.key?(:two_key)).to be_truthy
expect(new_hash.key?(:redKey)).to be_truthy
expect(new_hash[:redKey].key?(:blue_key)).to be_truthy
end

it 'does not keep the old keys' do
new_hash = hash.symbol_keys
expect(new_hash.key?('oneKey')).to be_falsey
expect(new_hash.key?('two_key')).to be_falsey
expect(new_hash.key?('redKey')).to be_falsey
expect(new_hash[:redKey].key?('blue_key')).to be_falsey
end

it 'does not convert the values' do
new_hash = hash.symbol_keys

expect(new_hash[:oneKey]).to eql(hash['oneKey'])
expect(new_hash[:two_key]).to eql(hash['two_key'])
expect(new_hash[:redKey][:blue_key]).to eql(hash['redKey']['blue_key'])
end

it 'does not modify the original hash' do
new_hash = hash
new_hash.symbol_keys

expect(new_hash.key?('oneKey')).to be_truthy
expect(new_hash.key?('two_key')).to be_truthy
expect(new_hash.key?('redKey')).to be_truthy
expect(new_hash['redKey'].key?('blue_key')).to be_truthy

expect(new_hash['oneKey']).to be(hash['oneKey'])
expect(new_hash['two_key']).to be(hash['two_key'])
expect(new_hash['redKey']).to be(hash['redKey'])
expect(new_hash['redKey']['blue_key']).to be(hash['redKey']['blue_key'])
end
end
end
File renamed without changes.
2 changes: 1 addition & 1 deletion spec/utilise/time_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
end

it 'returns a string number' do
expect(Time.unique).to match(/\d+/)
expect(Time.unique.to_i).to be_a Integer
end

it 'returns a number from time to 3 decimal places' do
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('1.0.0')
expect(Utilise.version).to eq('1.1.0')
end

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

0 comments on commit 7194f65

Please sign in to comment.