rename_keys 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/core_extensions/hash/keys.rb +28 -0
- data/lib/rename_keys/version.rb +1 -1
- metadata +3 -3
- data/lib/rename_keys.rb +0 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a0dcc4059b50a3d13e2463253b9939079059107
|
4
|
+
data.tar.gz: 44c0c9825b795eaa266ba60366a1c92a71144245
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6507221bec72678f87ff22b2261bb2fe11a00a7ed1add209cf92f05c25cea14efd1346975ac630b7a69d8395e75fddd0261aab01178782879aa821275a8a15e
|
7
|
+
data.tar.gz: 7dae8e2a726ae4c0558635b9819d4d9297faa43d42c4ab2d35018311edc666f5cc01ce41a9b1761c96186c45edd2a398add3fcc587a5fb78293cd16bb6068839
|
data/README.md
CHANGED
@@ -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
|
data/lib/rename_keys/version.rb
CHANGED
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.
|
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-
|
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/
|
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
|