hash_remapper 0.1.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 +7 -0
- data/.editorconfig +12 -0
- data/.gitignore +88 -0
- data/.rspec +3 -0
- data/.rubocop.yml +34 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +285 -0
- data/Rakefile +10 -0
- data/bin/console +16 -0
- data/bin/setup +8 -0
- data/hash_remapper.gemspec +36 -0
- data/img/hash_remapper.png +0 -0
- data/lib/hash_remapper.rb +94 -0
- metadata +198 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fbfe47cb8ee736cddb808ede7db0b136fcf8f07d
|
4
|
+
data.tar.gz: bbf5f08842c8d821332f2085dbef2c7f6560d396
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c80302b1468b4b300c99bd281022b282863d059d0d4fdc4e758213c42fc00473022768d1358743dbb78cdd32ac81c36c17dda1995115b6577294f7e0dde83838
|
7
|
+
data.tar.gz: 76d4c1d617e18f89240ff75b3e107aaaab466e5082a00362eaf4fd8d05244dfa8069019b7c3ae68748bec6923b31b955962d214431b858a546e77a9df3c9bac9
|
data/.editorconfig
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/Gemfile.lock
|
4
|
+
/_yardoc/
|
5
|
+
/coverage/
|
6
|
+
/doc/
|
7
|
+
/pkg/
|
8
|
+
/spec/reports/
|
9
|
+
/tmp/
|
10
|
+
|
11
|
+
# rspec failure tracking
|
12
|
+
.rspec_status
|
13
|
+
|
14
|
+
### Ruby ###
|
15
|
+
.byebug_history
|
16
|
+
*.gem
|
17
|
+
|
18
|
+
### macOS ###
|
19
|
+
*.DS_Store
|
20
|
+
.AppleDouble
|
21
|
+
.LSOverride
|
22
|
+
|
23
|
+
# Icon must end with two \r
|
24
|
+
Icon
|
25
|
+
|
26
|
+
# Thumbnails
|
27
|
+
._*
|
28
|
+
|
29
|
+
# Files that might appear in the root of a volume
|
30
|
+
.DocumentRevisions-V100
|
31
|
+
.fseventsd
|
32
|
+
.Spotlight-V100
|
33
|
+
.TemporaryItems
|
34
|
+
.Trashes
|
35
|
+
.VolumeIcon.icns
|
36
|
+
.com.apple.timemachine.donotpresent
|
37
|
+
|
38
|
+
# Directories potentially created on remote AFP share
|
39
|
+
.AppleDB
|
40
|
+
.AppleDesktop
|
41
|
+
Network Trash Folder
|
42
|
+
Temporary Items
|
43
|
+
.apdisk
|
44
|
+
|
45
|
+
### Config ###
|
46
|
+
/config/*
|
47
|
+
!/config/*.sample
|
48
|
+
|
49
|
+
# Used by dotenv library to load environment variables.
|
50
|
+
.env
|
51
|
+
.env.*
|
52
|
+
.env.local
|
53
|
+
.env.local.*
|
54
|
+
!.env.local.test
|
55
|
+
|
56
|
+
## Documentation cache and generated files:
|
57
|
+
/.yardoc/
|
58
|
+
/_yardoc/
|
59
|
+
/doc/
|
60
|
+
/rdoc/
|
61
|
+
|
62
|
+
## Environment normalization:
|
63
|
+
/.bundle/
|
64
|
+
/vendor/bundle
|
65
|
+
/lib/bundler/man/
|
66
|
+
|
67
|
+
# for a library or gem, you might want to ignore these files since the code is
|
68
|
+
# intended to run in multiple environments; otherwise, check them in:
|
69
|
+
Gemfile.lock
|
70
|
+
.ruby-version
|
71
|
+
.ruby-gemset
|
72
|
+
|
73
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
74
|
+
.rvmrc
|
75
|
+
|
76
|
+
### Vim ###
|
77
|
+
# swap
|
78
|
+
[._]*.s[a-v][a-z]
|
79
|
+
[._]*.sw[a-p]
|
80
|
+
[._]s[a-v][a-z]
|
81
|
+
[._]sw[a-p]
|
82
|
+
# session
|
83
|
+
Session.vim
|
84
|
+
# temporary
|
85
|
+
.netrwhist
|
86
|
+
*~
|
87
|
+
# auto-generated tag files
|
88
|
+
tags
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
AllCops:
|
2
|
+
DisabledByDefault: false
|
3
|
+
TargetRubyVersion: 2.4
|
4
|
+
Exclude:
|
5
|
+
- 'spec/fixtures/**/*'
|
6
|
+
|
7
|
+
Metrics/LineLength:
|
8
|
+
Max: 120
|
9
|
+
# To make it possible to copy or click on URIs in the code, we allow lines
|
10
|
+
# containing a URI to be longer than Max.
|
11
|
+
AllowHeredoc: true
|
12
|
+
AllowURI: true
|
13
|
+
URISchemes:
|
14
|
+
- http
|
15
|
+
- https
|
16
|
+
# The IgnoreCopDirectives option causes the LineLength rule to ignore cop
|
17
|
+
# directives like '# rubocop: enable ...' when calculating a line's length.
|
18
|
+
IgnoreCopDirectives: false
|
19
|
+
# The IgnoredPatterns option is a list of !ruby/regexp and/or string
|
20
|
+
# elements. Strings will be converted to Regexp objects. A line that matches
|
21
|
+
# any regular expression listed in this option will be ignored by LineLength.
|
22
|
+
IgnoredPatterns: []
|
23
|
+
|
24
|
+
Metrics/BlockLength:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
Style/AsciiComments:
|
28
|
+
Enabled: false
|
29
|
+
|
30
|
+
Metrics/MethodLength:
|
31
|
+
Max: 25
|
32
|
+
|
33
|
+
Metrics/AbcSize:
|
34
|
+
Max: 20
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at smileart21@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 smileart
|
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,285 @@
|
|
1
|
+
# 🗺 HashRemapper
|
2
|
+
|
3
|
+
<p align="center">
|
4
|
+
<img width="360" title="logo" src ="./img/hash_remapper.png" />
|
5
|
+
</p>
|
6
|
+
|
7
|
+
> A little lib which maps original keys to the new ones <sup>[*](#features)</sup>
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'hash_remapper'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install hash_remapper
|
24
|
+
|
25
|
+
## Features [<a name="features">*</a>]
|
26
|
+
|
27
|
+
* maps original keys to the new ones [1](#map)
|
28
|
+
* auto-ignores all the skipped keys [2](#ignore)
|
29
|
+
* preprocesses the value with a lambda [3](#lambda)
|
30
|
+
* allows remap keys within preprocessing [4](#preprocessing)
|
31
|
+
* allows to keep data subsets only [5](#subset)
|
32
|
+
* allows to include data with the original keynames [6](#originals)
|
33
|
+
* allows to use global context to create composite fields [7](#composite)
|
34
|
+
* merges values if the key already exists and supports #merge [8](#merge)
|
35
|
+
* replaces values if the key already exists and doesn't support #merge [9](#replace)
|
36
|
+
* allows to assign static defaults through lambdas [10](#defaults)
|
37
|
+
* allows to remap to the deep values within the context [11](#deep)
|
38
|
+
* allows to create completely new keys [12](#new_keys)
|
39
|
+
|
40
|
+
## Usage
|
41
|
+
|
42
|
+
```rb
|
43
|
+
# Having such a Hash
|
44
|
+
original_hash = {
|
45
|
+
test: 42,
|
46
|
+
'data' => [
|
47
|
+
1,
|
48
|
+
2,
|
49
|
+
'string!'
|
50
|
+
],
|
51
|
+
ignore: :me,
|
52
|
+
nested: {
|
53
|
+
hash: :data,
|
54
|
+
really: {
|
55
|
+
deep: true
|
56
|
+
}
|
57
|
+
}
|
58
|
+
}
|
59
|
+
```
|
60
|
+
|
61
|
+
## 0: Basic Idea
|
62
|
+
|
63
|
+
```rb
|
64
|
+
HashRemapper.remap(
|
65
|
+
{ a: 1, b: 2 },
|
66
|
+
:a => :hello,
|
67
|
+
:b => :world
|
68
|
+
)
|
69
|
+
|
70
|
+
# => { hello: 1, world: 2 }
|
71
|
+
```
|
72
|
+
|
73
|
+
## <a name="map">1</a>: Map original keys to the new ones
|
74
|
+
|
75
|
+
```rb
|
76
|
+
HashRemapper.remap(
|
77
|
+
original_hash, # pass original Hash
|
78
|
+
test: :magic_number, # List old_key => new_key pairs
|
79
|
+
'data' => :data, # including type change if needed
|
80
|
+
ignore: :dont_ignore,
|
81
|
+
nested: :internal # values will be just passed to the new keys
|
82
|
+
)
|
83
|
+
|
84
|
+
# =>
|
85
|
+
# {
|
86
|
+
# magic_number: 42,
|
87
|
+
# data: [1, 2, 'string!'],
|
88
|
+
# dont_ignore: :me,
|
89
|
+
# internal: { hash: :data, really: { deep: true } },
|
90
|
+
# }
|
91
|
+
```
|
92
|
+
|
93
|
+
## <a name="ignore">2</a>: Auto-ignore all the skipped keys
|
94
|
+
|
95
|
+
```rb
|
96
|
+
HashRemapper.remap(
|
97
|
+
original_hash,
|
98
|
+
test: :magic_number
|
99
|
+
)
|
100
|
+
|
101
|
+
# =>
|
102
|
+
# {
|
103
|
+
# magic_number: 42
|
104
|
+
# }
|
105
|
+
|
106
|
+
HashRemapper.remap(
|
107
|
+
original_hash,
|
108
|
+
false, # a flag to pass through the original key => value pairs
|
109
|
+
test: :magic_number
|
110
|
+
)
|
111
|
+
|
112
|
+
# =>
|
113
|
+
# {
|
114
|
+
# "data" => [1, 2, "string!"],
|
115
|
+
# :ignore => :me,
|
116
|
+
# :magic_number => 42,
|
117
|
+
# :nested => {:hash=>:data, :really=>{:deep=>true}}
|
118
|
+
# }
|
119
|
+
```
|
120
|
+
|
121
|
+
## <a name="lambda">3</a>: Preprocess the values with lambdas
|
122
|
+
|
123
|
+
```rb
|
124
|
+
HashRemapper.remap(
|
125
|
+
original_hash,
|
126
|
+
test: ->(_, __) { [:test, 21] } # do whatever you want and return "key: value" Array
|
127
|
+
)
|
128
|
+
|
129
|
+
# =>
|
130
|
+
# {
|
131
|
+
# test: 21
|
132
|
+
# }
|
133
|
+
```
|
134
|
+
|
135
|
+
## <a name="preprocessing">4</a>: Remap keys within preprocessing
|
136
|
+
|
137
|
+
```rb
|
138
|
+
HashRemapper.remap(
|
139
|
+
original_hash,
|
140
|
+
test: ->(data, _) { [:magic_number, data.to_s] }
|
141
|
+
)
|
142
|
+
|
143
|
+
# =>
|
144
|
+
# {
|
145
|
+
# magic_number: '42'
|
146
|
+
# }
|
147
|
+
```
|
148
|
+
|
149
|
+
## <a name="subset">5</a>: Keep data subsets only
|
150
|
+
|
151
|
+
```rb
|
152
|
+
HashRemapper.remap(
|
153
|
+
original_hash,
|
154
|
+
'data' => ->(data, _) { ['data', data[0..1]] },
|
155
|
+
)
|
156
|
+
|
157
|
+
# =>
|
158
|
+
# {
|
159
|
+
# 'data' => [1, 2]
|
160
|
+
# }
|
161
|
+
```
|
162
|
+
|
163
|
+
## <a name="originals">6</a>: Include data with the original keyname
|
164
|
+
|
165
|
+
```rb
|
166
|
+
HashRemapper.remap(
|
167
|
+
original_hash,
|
168
|
+
test: :magic_number,
|
169
|
+
ignore: :ignore
|
170
|
+
)
|
171
|
+
|
172
|
+
# =>
|
173
|
+
# {
|
174
|
+
# magic_number: 42,
|
175
|
+
# ignore: :me
|
176
|
+
# }
|
177
|
+
```
|
178
|
+
|
179
|
+
## <a name="composite">7</a>: Use global context to create composite fields
|
180
|
+
|
181
|
+
```rb
|
182
|
+
HashRemapper.remap(
|
183
|
+
original_hash,
|
184
|
+
test: ->(data, context) { [:magic_number, data + context['data'][1]] }
|
185
|
+
)
|
186
|
+
|
187
|
+
# =>
|
188
|
+
# {
|
189
|
+
# magic_number: 44
|
190
|
+
# }
|
191
|
+
```
|
192
|
+
|
193
|
+
## <a name="merge">8</a>: Merges values
|
194
|
+
|
195
|
+
```rb
|
196
|
+
HashRemapper.remap(
|
197
|
+
original_hash,
|
198
|
+
test: ->(_, __) { [:magic_number, { one: 1 }] },
|
199
|
+
whatever: ->(_, __) { [:magic_number, { two: 2 }] }
|
200
|
+
)
|
201
|
+
|
202
|
+
# =>
|
203
|
+
# {
|
204
|
+
# magic_number: { one: 1, two: 2 }
|
205
|
+
# }
|
206
|
+
```
|
207
|
+
|
208
|
+
## <a name="replace">9</a>: Replace values if the key already exists and doesn't support `#merge`
|
209
|
+
|
210
|
+
```rb
|
211
|
+
HashRemapper.remap(
|
212
|
+
original_hash,
|
213
|
+
test: ->(_, __) { [:magic_number, 42] },
|
214
|
+
whatever: ->(_, __) { [:magic_number, 21] }
|
215
|
+
)
|
216
|
+
|
217
|
+
# =>
|
218
|
+
# {
|
219
|
+
# magic_number: 21
|
220
|
+
# }
|
221
|
+
```
|
222
|
+
|
223
|
+
## <a name="defaults">10</a>: Assign static defaults
|
224
|
+
|
225
|
+
```rb
|
226
|
+
HashRemapper.remap(
|
227
|
+
original_hash,
|
228
|
+
test: ->(_, __) { [:magic_number, 21] }
|
229
|
+
)
|
230
|
+
|
231
|
+
# =>
|
232
|
+
# {
|
233
|
+
# magic_number: 21
|
234
|
+
# }
|
235
|
+
```
|
236
|
+
|
237
|
+
## <a name="deep">11</a>: Remap to the deep values
|
238
|
+
|
239
|
+
```rb
|
240
|
+
HashRemapper.remap(
|
241
|
+
original_hash,
|
242
|
+
test: [:magic_bool, %i[nested really deep]]
|
243
|
+
)
|
244
|
+
|
245
|
+
# =>
|
246
|
+
# {
|
247
|
+
# magic_bool: true
|
248
|
+
# }
|
249
|
+
```
|
250
|
+
|
251
|
+
## <a name="new_keys">12</a>: Create completely new keys
|
252
|
+
|
253
|
+
```rb
|
254
|
+
HashRemapper.remap(
|
255
|
+
original_hash,
|
256
|
+
test: :magic_number,
|
257
|
+
absolutely_new_key: ->(_, __) { [:absolutely_new_key, 'shiny new value'] }
|
258
|
+
)
|
259
|
+
|
260
|
+
# =>
|
261
|
+
# {
|
262
|
+
# magic_number: 42,
|
263
|
+
# absolutely_new_key: 'shiny new value'
|
264
|
+
# }
|
265
|
+
```
|
266
|
+
|
267
|
+
## Development
|
268
|
+
|
269
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
270
|
+
|
271
|
+
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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
272
|
+
|
273
|
+
## Contributing
|
274
|
+
|
275
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hash_remapper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
276
|
+
|
277
|
+
Initial version of this lib was written in pair with @bronislav, so thank him for invaluabale contribution and help.
|
278
|
+
|
279
|
+
## License
|
280
|
+
|
281
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
282
|
+
|
283
|
+
## Code of Conduct
|
284
|
+
|
285
|
+
Everyone interacting in the HashRemapper project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/hash_remapper/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
require 'bundler/setup'
|
6
|
+
require 'hash_remapper'
|
7
|
+
|
8
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
9
|
+
# with your gem easier. You can also use a different console, if you like.
|
10
|
+
|
11
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
12
|
+
# require "pry"
|
13
|
+
# Pry.start
|
14
|
+
|
15
|
+
require 'irb'
|
16
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# coding: utf-8
|
3
|
+
|
4
|
+
lib = File.expand_path('../lib', __FILE__)
|
5
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
6
|
+
require 'hash_remapper'
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = 'hash_remapper'
|
10
|
+
spec.version = HashRemapper::VERSION
|
11
|
+
spec.authors = ['Serge Bedzhyk']
|
12
|
+
spec.email = 'smileart21@gmail.com'
|
13
|
+
|
14
|
+
spec.summary = 'A little lib which maps original keys to the new ones'
|
15
|
+
spec.description = 'A little lib which maps original keys to the new ones and more'
|
16
|
+
spec.homepage = 'https://rubygems.org/gems/hash_remapper'
|
17
|
+
spec.license = 'MIT'
|
18
|
+
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
20
|
+
f.match(%r{^(test|spec|features)/})
|
21
|
+
end
|
22
|
+
spec.bindir = 'exe'
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ['lib']
|
25
|
+
|
26
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
27
|
+
spec.add_development_dependency 'byebug', '~> 9.1'
|
28
|
+
spec.add_development_dependency 'inch', '~> 0.7'
|
29
|
+
spec.add_development_dependency 'letters', '~> 0.4'
|
30
|
+
spec.add_development_dependency 'rake', '~> 12.2'
|
31
|
+
spec.add_development_dependency 'rspec', '~> 3.7'
|
32
|
+
spec.add_development_dependency 'rubocop', '~> 0.51'
|
33
|
+
spec.add_development_dependency 'rubygems-tasks', '~> 0.2'
|
34
|
+
spec.add_development_dependency 'simplecov', '~> 0.15'
|
35
|
+
spec.add_development_dependency 'yard', '~> 0.8'
|
36
|
+
end
|
Binary file
|
@@ -0,0 +1,94 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
# Utility class to map original Hash keys to the new ones
|
6
|
+
class HashRemapper
|
7
|
+
# Current Library Version
|
8
|
+
VERSION = '0.1.0'
|
9
|
+
|
10
|
+
class << self
|
11
|
+
# Remaps `data` Hash by renaming keys, creating new ones and
|
12
|
+
# optionally aggregating values
|
13
|
+
#
|
14
|
+
# @example
|
15
|
+
# HashRemapper.remap(
|
16
|
+
# {a: 1, b: 2, c: 3},
|
17
|
+
# true
|
18
|
+
# a: :one,
|
19
|
+
# b: :two
|
20
|
+
# ) # => { one: 1, two: 2, c: 3 }
|
21
|
+
#
|
22
|
+
# @param [Hash] data the original Hash to remap
|
23
|
+
# @param [Boolean] pass_trough the flag to pass the original key/value pairs (default: false)
|
24
|
+
# @param [Hash] mapping the Hash which in the simplest case tells how to rename keys
|
25
|
+
#
|
26
|
+
# @return [Hash] remapped version of the original Hash
|
27
|
+
# (selected keys only or all the keys if we passed originals)
|
28
|
+
def remap(data, pass_trough = false, mapping)
|
29
|
+
mapping = pass_trough_mapping(data, mapping) if pass_trough
|
30
|
+
|
31
|
+
mapping.each_with_object({}) do |(from, to), acc|
|
32
|
+
key, value = try_callable(from, to, data, acc) ||
|
33
|
+
try_digging(to, data) ||
|
34
|
+
[to, data[from]]
|
35
|
+
|
36
|
+
acc[key] = value
|
37
|
+
acc
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
# Method to try to handle callable mapping Hash value
|
44
|
+
# (if the mapping value is callable)
|
45
|
+
#
|
46
|
+
# @param [Object] from the source key to handle
|
47
|
+
# @param [Object] to the target key to map to
|
48
|
+
# @param [Hash] data the whole original Hash to use as the context in the lambda
|
49
|
+
# @param [Hash] acc the accumulated result hash to check and merge existed data
|
50
|
+
#
|
51
|
+
# @return [Array(Object,Object)] key and its value to put to the resulting Hash
|
52
|
+
def try_callable(from, to, data, acc)
|
53
|
+
return unless to.respond_to?(:call)
|
54
|
+
|
55
|
+
target_name, target_data = to.call(data[from], data)
|
56
|
+
|
57
|
+
if acc.key?(target_name) && acc[target_name].respond_to?(:merge)
|
58
|
+
target_data = acc[target_name].merge(target_data)
|
59
|
+
end
|
60
|
+
|
61
|
+
[target_name, target_data]
|
62
|
+
end
|
63
|
+
|
64
|
+
# Method to try to handle data digging
|
65
|
+
# (if the mapping value is enumerable)
|
66
|
+
#
|
67
|
+
# @param [Object] to the target key to map to
|
68
|
+
# @param [Hash] data the whole original Hash to use as the digging target
|
69
|
+
#
|
70
|
+
# @return [Array(Object,Object)] key and its value to put to the resulting Hash
|
71
|
+
def try_digging(to, data)
|
72
|
+
return unless to.respond_to?(:each)
|
73
|
+
|
74
|
+
[to.first, data.dig(*to.last)]
|
75
|
+
end
|
76
|
+
|
77
|
+
# Method which automatically prepares direct mapping (e.g. { :a => :a })
|
78
|
+
# for the keys that weren't used in the mapping Hash (to pass them through "as is")
|
79
|
+
#
|
80
|
+
# @param [Hash] data the whole original Hash to take keys from
|
81
|
+
# @param [Hash] mapping the mapping to use as the reference of the used keys
|
82
|
+
#
|
83
|
+
# @return [Hash] new mapping Hash containing original mapping + direct mappings
|
84
|
+
def pass_trough_mapping(data, mapping)
|
85
|
+
original_keys = Set.new(data.keys)
|
86
|
+
mapping_keys = Set.new(mapping.keys)
|
87
|
+
|
88
|
+
pass_trough_keys = original_keys.difference(mapping_keys)
|
89
|
+
pass_trough_mapping = Hash[pass_trough_keys.zip(pass_trough_keys)]
|
90
|
+
|
91
|
+
mapping.merge(pass_trough_mapping)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
metadata
ADDED
@@ -0,0 +1,198 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_remapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Serge Bedzhyk
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-12-05 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: byebug
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '9.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '9.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: inch
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: letters
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.4'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.4'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '12.2'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '12.2'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.7'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.7'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.51'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.51'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubygems-tasks
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0.2'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0.2'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: simplecov
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0.15'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0.15'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: yard
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0.8'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0.8'
|
153
|
+
description: A little lib which maps original keys to the new ones and more
|
154
|
+
email: smileart21@gmail.com
|
155
|
+
executables: []
|
156
|
+
extensions: []
|
157
|
+
extra_rdoc_files: []
|
158
|
+
files:
|
159
|
+
- ".editorconfig"
|
160
|
+
- ".gitignore"
|
161
|
+
- ".rspec"
|
162
|
+
- ".rubocop.yml"
|
163
|
+
- ".travis.yml"
|
164
|
+
- CODE_OF_CONDUCT.md
|
165
|
+
- Gemfile
|
166
|
+
- LICENSE.txt
|
167
|
+
- README.md
|
168
|
+
- Rakefile
|
169
|
+
- bin/console
|
170
|
+
- bin/setup
|
171
|
+
- hash_remapper.gemspec
|
172
|
+
- img/hash_remapper.png
|
173
|
+
- lib/hash_remapper.rb
|
174
|
+
homepage: https://rubygems.org/gems/hash_remapper
|
175
|
+
licenses:
|
176
|
+
- MIT
|
177
|
+
metadata: {}
|
178
|
+
post_install_message:
|
179
|
+
rdoc_options: []
|
180
|
+
require_paths:
|
181
|
+
- lib
|
182
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
183
|
+
requirements:
|
184
|
+
- - ">="
|
185
|
+
- !ruby/object:Gem::Version
|
186
|
+
version: '0'
|
187
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
188
|
+
requirements:
|
189
|
+
- - ">="
|
190
|
+
- !ruby/object:Gem::Version
|
191
|
+
version: '0'
|
192
|
+
requirements: []
|
193
|
+
rubyforge_project:
|
194
|
+
rubygems_version: 2.6.14
|
195
|
+
signing_key:
|
196
|
+
specification_version: 4
|
197
|
+
summary: A little lib which maps original keys to the new ones
|
198
|
+
test_files: []
|