rename_keys 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 48bc4f666fdbf046ad4f3b3672ecf0b9eedd1d41
4
- data.tar.gz: 55871ff45aa912ccf2cf35237d9b45dcf0c7f83c
3
+ metadata.gz: 3a0dcc4059b50a3d13e2463253b9939079059107
4
+ data.tar.gz: 44c0c9825b795eaa266ba60366a1c92a71144245
5
5
  SHA512:
6
- metadata.gz: b650fc54ff4d331a0411b13c26872d571bf421bd4118da965df8af3e71f4de40d4ad2eda47e7e36afcc932576fc3b354fc932f4f7e623d9a0674eeab8835bfdb
7
- data.tar.gz: 9312acc3bd8d511fdb18c23a21e6d6bf0d2c9884cc440cfac513963f9808f29431e01922bb6d47708c86c502421716788abc984a646cc4c6052a84a3bb94bce8
6
+ metadata.gz: f6507221bec72678f87ff22b2261bb2fe11a00a7ed1add209cf92f05c25cea14efd1346975ac630b7a69d8395e75fddd0261aab01178782879aa821275a8a15e
7
+ data.tar.gz: 7dae8e2a726ae4c0558635b9819d4d9297faa43d42c4ab2d35018311edc666f5cc01ce41a9b1761c96186c45edd2a398add3fcc587a5fb78293cd16bb6068839
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # RenameKeys
2
2
 
3
- Allow renaming of Ruby hash keys, including deeply-nested keys.
3
+ Allow renaming of Ruby hash keys, including deeply-nested keys, by extending (Monkey-Patching) the Ruby core Hash class.
4
4
 
5
5
  ## Installation
6
6
 
@@ -0,0 +1,28 @@
1
+ require "rename_keys/version"
2
+
3
+ # Code taken from answers to Stack Overflow about renaming hash keys.
4
+ # See: http://stackoverflow.com/questions/4137824/how-to-elegantly-rename-all-keys-in-a-hash-in-ruby
5
+ # See: http://www.justinweiss.com/articles/3-ways-to-monkey-patch-without-making-a-mess
6
+ module CoreExtensions
7
+ module Hash
8
+ module Keys
9
+ def rename_keys(mapping)
10
+ result = {}
11
+ self.map do |k, v|
12
+ mapped_key = mapping[k] ? mapping[k] : k
13
+
14
+ # If this entry is a deep/nested hash, recursively change key names.
15
+ result[mapped_key] = v.class.to_s == "Hash" ? v.rename_keys(mapping) : v
16
+
17
+ result[mapped_key] = v.collect{ |obj| obj.rename_keys(mapping) if obj.class.to_s == "Hash" } if v.class.to_s == "Array"
18
+ end
19
+
20
+ # If used within a Rails project, allow reference by either symbol or string.
21
+ result.respond_to?(:with_indifferent_access) ? result.with_indifferent_access : result
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ # Actually monkey-patch the Ruby core Hash class
28
+ Hash.include CoreExtensions::Hash::Keys
@@ -1,3 +1,3 @@
1
1
  module RenameKeys
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rename_keys
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel LeGrand
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-04 00:00:00.000000000 Z
11
+ date: 2015-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -56,7 +56,7 @@ files:
56
56
  - Rakefile
57
57
  - bin/console
58
58
  - bin/setup
59
- - lib/rename_keys.rb
59
+ - lib/core_extensions/hash/keys.rb
60
60
  - lib/rename_keys/version.rb
61
61
  - rename_keys.gemspec
62
62
  homepage: https://github.com/dlegr250/rename_keys
data/lib/rename_keys.rb DELETED
@@ -1,20 +0,0 @@
1
- require "rename_keys/version"
2
-
3
- # Code taken from answers to Stack Overflow about renaming hash keys.
4
- # See: http://stackoverflow.com/questions/4137824/how-to-elegantly-rename-all-keys-in-a-hash-in-ruby
5
- class Hash
6
- def rename_keys(mapping)
7
- result = {}
8
- self.map do |k, v|
9
- mapped_key = mapping[k] ? mapping[k] : k
10
-
11
- # If this entry is a deep/nested hash, recursively change key names.
12
- result[mapped_key] = v.kind_of?(Hash) ? v.rename_keys(mapping) : v
13
-
14
- result[mapped_key] = v.collect{ |obj| obj.rename_keys(mapping) if obj.kind_of?(Hash)} if v.kind_of?(Array)
15
- end
16
-
17
- # If used within a Rails project, allow reference by either symbol or string.
18
- result.respond_to?(:with_indifferent_access) ? result.with_indifferent_access : result
19
- end
20
- end