rename_keys 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +53 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/rename_keys/version.rb +3 -0
- data/lib/rename_keys.rb +20 -0
- data/rename_keys.gemspec +29 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 48bc4f666fdbf046ad4f3b3672ecf0b9eedd1d41
|
4
|
+
data.tar.gz: 55871ff45aa912ccf2cf35237d9b45dcf0c7f83c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b650fc54ff4d331a0411b13c26872d571bf421bd4118da965df8af3e71f4de40d4ad2eda47e7e36afcc932576fc3b354fc932f4f7e623d9a0674eeab8835bfdb
|
7
|
+
data.tar.gz: 9312acc3bd8d511fdb18c23a21e6d6bf0d2c9884cc440cfac513963f9808f29431e01922bb6d47708c86c502421716788abc984a646cc4c6052a84a3bb94bce8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Daniel LeGrand
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# RenameKeys
|
2
|
+
|
3
|
+
Allow renaming of Ruby hash keys, including deeply-nested keys.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem "rename_keys"
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install rename_keys
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```
|
24
|
+
# Simple renaming of keys
|
25
|
+
h = { "first" => "first value" }
|
26
|
+
|
27
|
+
h.rename_keys({ "first" => "a" })
|
28
|
+
|
29
|
+
puts h #=> { "a" => "first value" }
|
30
|
+
```
|
31
|
+
|
32
|
+
```
|
33
|
+
# Deep nesting of keys
|
34
|
+
h = { "first" => { "second" => { "third" => "third value" } } }
|
35
|
+
|
36
|
+
h.rename_keys({ "third" => "charlie" })
|
37
|
+
|
38
|
+
puts h #=> { "first" => { "second" => { "charlie" => "third value" } } }
|
39
|
+
```
|
40
|
+
|
41
|
+
## Development
|
42
|
+
|
43
|
+
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.
|
44
|
+
|
45
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
1. Fork it ( https://github.com/[my-github-username]/rename_keys/fork )
|
50
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
51
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
52
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
53
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rename_keys"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/rename_keys.rb
ADDED
@@ -0,0 +1,20 @@
|
|
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
|
data/rename_keys.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "rename_keys/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rename_keys"
|
8
|
+
spec.version = RenameKeys::VERSION
|
9
|
+
spec.authors = ["Daniel LeGrand"]
|
10
|
+
spec.email = ["dan.legrand@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Change key names of Ruby hashes (including deep/nested hashes).}
|
13
|
+
spec.description = %q{Especially useful when connecting to external APIs that return data in a format you do not like,
|
14
|
+
this gem allows you to easily convert those 3rd-party key names to more meaningful names.}
|
15
|
+
spec.homepage = "https://github.com/dlegr250/rename_keys"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
if spec.respond_to?(:metadata)
|
24
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to "http://mygemserver.com" to prevent pushes to rubygems.org, or delete to allow pushes to any server."
|
25
|
+
end
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rename_keys
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel LeGrand
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: |-
|
42
|
+
Especially useful when connecting to external APIs that return data in a format you do not like,
|
43
|
+
this gem allows you to easily convert those 3rd-party key names to more meaningful names.
|
44
|
+
email:
|
45
|
+
- dan.legrand@gmail.com
|
46
|
+
executables: []
|
47
|
+
extensions: []
|
48
|
+
extra_rdoc_files: []
|
49
|
+
files:
|
50
|
+
- ".gitignore"
|
51
|
+
- ".rspec"
|
52
|
+
- ".travis.yml"
|
53
|
+
- Gemfile
|
54
|
+
- LICENSE.txt
|
55
|
+
- README.md
|
56
|
+
- Rakefile
|
57
|
+
- bin/console
|
58
|
+
- bin/setup
|
59
|
+
- lib/rename_keys.rb
|
60
|
+
- lib/rename_keys/version.rb
|
61
|
+
- rename_keys.gemspec
|
62
|
+
homepage: https://github.com/dlegr250/rename_keys
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.4.6
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Change key names of Ruby hashes (including deep/nested hashes).
|
86
|
+
test_files: []
|