rename_keys 0.4.0 → 0.5.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 +24 -4
- data/lib/core_extensions/array/iterate_over_hashes.rb +24 -0
- data/lib/rename_keys/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b3688359ea366f3b3463e1ee01fddf0841b54da4
|
4
|
+
data.tar.gz: 4b62214ca40e1d2eeeaf8c225b3038c9b8fc7ed5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c1dad9fee4c88e403ef89260d1e662dbae192250acd9e5a7355e0f2c3cff6a020999cc3765ad54543325f175e57265c4f8351e0c1ebb6819a4fc274fbff80fde
|
7
|
+
data.tar.gz: 26399405910f0edf7aed6c88ba35958a73ce6f9623eaf60ff6afecdb2bf048b53f8f529cb412814725bfcd44f112cb35a88cc62efdb63559bb8d0104d593f6df
|
data/README.md
CHANGED
@@ -24,20 +24,40 @@ Or install it yourself as:
|
|
24
24
|
# Simple renaming of keys
|
25
25
|
h = { "first" => "first value" }
|
26
26
|
|
27
|
-
h.rename_keys({ "first" => "a" })
|
27
|
+
renamed_hash = h.rename_keys({ "first" => "a" })
|
28
28
|
|
29
|
-
puts
|
29
|
+
puts renamed_hash #=> { "a" => "first value" }
|
30
30
|
```
|
31
31
|
|
32
32
|
```
|
33
33
|
# Deep nesting of keys
|
34
34
|
h = { "first" => { "second" => { "third" => "third value" } } }
|
35
35
|
|
36
|
-
h.rename_keys({ "third" => "charlie" })
|
36
|
+
renamed_hash = h.rename_keys({ "third" => "charlie" })
|
37
37
|
|
38
|
-
puts
|
38
|
+
puts renamed_hash #=> { "first" => { "second" => { "charlie" => "third value" } } }
|
39
39
|
```
|
40
40
|
|
41
|
+
```
|
42
|
+
# Iterating over array of hashes (does not alter non-hash elements)
|
43
|
+
hashes = [
|
44
|
+
{ "a" => "first" },
|
45
|
+
{ "b" => "second" },
|
46
|
+
{ "c" => "third" },
|
47
|
+
4
|
48
|
+
]
|
49
|
+
|
50
|
+
renamed_hashes = hashes.rename_keys({ "a" => "alpha", "b" => "bravo", "c" => "charlie" })
|
51
|
+
|
52
|
+
puts renamed_hashes # => [{ "alpha" => "first" }, { "bravo" => "second" }, { "charlie" => "third" }, 4]
|
53
|
+
```
|
54
|
+
|
55
|
+
## Performance
|
56
|
+
|
57
|
+
The use case I created this gem for was retrieving small amounts of JSON from external APIs (small as in a JSON object with maybe 50-100 keys).
|
58
|
+
I do not know how performant this gem would be if you had, say, 100+ JSON objects, each with 50-100 keys, and you were changing 20+ keys at once.
|
59
|
+
If I find that performance is a big necessity, I may optimize it at that point and start doing some benchmarking.
|
60
|
+
|
41
61
|
## Development
|
42
62
|
|
43
63
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require "rename_keys/version"
|
2
|
+
|
3
|
+
module CoreExtensions
|
4
|
+
module Array
|
5
|
+
module IterateOverHashes
|
6
|
+
def rename_keys(mapping)
|
7
|
+
result = []
|
8
|
+
|
9
|
+
self.each_with_index do |element, index|
|
10
|
+
if element.kind_of?(::Hash)
|
11
|
+
result[index] = element.rename_keys(mapping)
|
12
|
+
else
|
13
|
+
result[index] = element
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
result
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Actually monkey-patch the Ruby core Array class
|
24
|
+
Array.include ::CoreExtensions::Array::IterateOverHashes
|
data/lib/rename_keys/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rename_keys
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel LeGrand
|
@@ -56,6 +56,7 @@ files:
|
|
56
56
|
- Rakefile
|
57
57
|
- bin/console
|
58
58
|
- bin/setup
|
59
|
+
- lib/core_extensions/array/iterate_over_hashes.rb
|
59
60
|
- lib/core_extensions/hash/keys.rb
|
60
61
|
- lib/rename_keys/version.rb
|
61
62
|
- rename_keys.gemspec
|