dotpath 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 20dc2372acb629a309f9e5c8f7e108e9a706d86acf6b74d6466dc42fbfcc3126
4
+ data.tar.gz: '09dba08d2011a07897e132011fd0a87a4a0cd11415d83d530cd4020a13a4da42'
5
+ SHA512:
6
+ metadata.gz: e3bf838e80547f3fa02ba806b744c4bc83038e7d55d9b60c2de804ada4d71af6f534d26b323f0aa99c884f838e9cf59d39e1190bffeedcef88eb52c9e4eb77ad
7
+ data.tar.gz: c8018af868fd813ffaf95cc81b1d2a1ba0f4ff7d10aa1994ddc89516b7dc4019e7985fca194d606a95b5380e05c3b0dc8e9ec5432276ca6a46a777a562ca1750
@@ -0,0 +1,22 @@
1
+ name: Rubocop
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+ timeout-minutes: 10
8
+ runs-on: ubuntu-latest
9
+
10
+ steps:
11
+ - uses: actions/checkout@v2
12
+
13
+ - uses: actions/setup-ruby@v1
14
+ with:
15
+ ruby-version: 2.6
16
+
17
+ - name: Install dependencies
18
+ run: |
19
+ gem install bundler
20
+ bundle install --local
21
+
22
+ - run: bin/rubocop
@@ -0,0 +1,46 @@
1
+ name: Ruby
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+ timeout-minutes: 10
8
+ runs-on: ubuntu-latest
9
+ strategy:
10
+ matrix:
11
+ ruby:
12
+ - "2.5"
13
+ - "2.6"
14
+ - "2.7"
15
+ name: Ruby ${{ matrix.ruby }} test
16
+ steps:
17
+ - uses: actions/checkout@v2
18
+
19
+ - uses: actions/setup-ruby@v1
20
+ with:
21
+ ruby-version: ${{ matrix.ruby }}
22
+
23
+ - name: Install dependencies
24
+ run: |
25
+ gem install bundler
26
+ bundle install --local
27
+
28
+ - name: Run Tests
29
+ run: |
30
+ bundle exec rake test
31
+
32
+ - name: Generate yard documentation
33
+ run: |
34
+ bundle exec yard
35
+
36
+ - name: Upload test coverage report
37
+ uses: actions/upload-artifact@v2
38
+ with:
39
+ name: coverage
40
+ path: coverage/**/*
41
+
42
+ - name: Upload generated yard documentation
43
+ uses: actions/upload-artifact@v2
44
+ with:
45
+ name: yard
46
+ path: doc/**/*
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,76 @@
1
+ AllCops:
2
+ Exclude:
3
+ - "bin/**/*"
4
+
5
+ Style/Documentation:
6
+ Enabled: false
7
+
8
+ Style/TrailingCommaInHashLiteral:
9
+ EnforcedStyleForMultiline: comma
10
+
11
+ Style/TrailingCommaInArrayLiteral:
12
+ EnforcedStyleForMultiline: comma
13
+
14
+ Style/TrailingCommaInArguments:
15
+ EnforcedStyleForMultiline: comma
16
+
17
+ Metrics/CyclomaticComplexity:
18
+ Enabled: false
19
+
20
+ Metrics/BlockLength:
21
+ Enabled: false
22
+
23
+ Metrics/LineLength:
24
+ Enabled: false
25
+
26
+ Metrics/ClassLength:
27
+ Enabled: false
28
+
29
+ Metrics/ModuleLength:
30
+ Enabled: false
31
+
32
+ Naming/FileName:
33
+ Exclude:
34
+ - lib/cased-ruby.rb
35
+
36
+ Naming/MethodParameterName:
37
+ Enabled: false
38
+
39
+ Metrics/AbcSize:
40
+ Enabled: false
41
+
42
+ Style/ConditionalAssignment:
43
+ Enabled: false
44
+
45
+ Style/IfUnlessModifier:
46
+ Enabled: false
47
+
48
+ Metrics/PerceivedComplexity:
49
+ Enabled: false
50
+
51
+ Metrics/MethodLength:
52
+ Enabled: false
53
+
54
+ Layout/RescueEnsureAlignment:
55
+ Enabled: false
56
+
57
+ Layout/ArgumentAlignment:
58
+ Enabled: false
59
+
60
+ Layout/EndAlignment:
61
+ Enabled: false
62
+
63
+ Layout/ElseAlignment:
64
+ Enabled: false
65
+
66
+ Layout/IndentationWidth:
67
+ Enabled: false
68
+
69
+ Layout/FirstHashElementIndentation:
70
+ Enabled: false
71
+
72
+ Layout/MultilineMethodCallIndentation:
73
+ Enabled: false
74
+
75
+ Layout/CaseIndentation:
76
+ Enabled: false
@@ -0,0 +1 @@
1
+ 2.6.5
@@ -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 me@garrettbjerkhoel.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 [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in dotpath.gemspec
6
+ gemspec
7
+
8
+ gem 'minitest', '~> 5.0'
9
+ gem 'rake', '~> 12.0'
10
+ gem 'simplecov', '0.18.5', require: false
@@ -0,0 +1,51 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ dotpath (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.1)
10
+ byebug (11.0.1)
11
+ docile (1.3.2)
12
+ jaro_winkler (1.5.4)
13
+ minitest (5.14.2)
14
+ parallel (1.19.2)
15
+ parser (2.7.1.4)
16
+ ast (~> 2.4.1)
17
+ rainbow (3.0.0)
18
+ rake (12.3.2)
19
+ rubocop (0.78.0)
20
+ jaro_winkler (~> 1.5.1)
21
+ parallel (~> 1.10)
22
+ parser (>= 2.6)
23
+ rainbow (>= 2.2.2, < 4.0)
24
+ ruby-progressbar (~> 1.7)
25
+ unicode-display_width (>= 1.4.0, < 1.7)
26
+ rubocop-performance (1.5.2)
27
+ rubocop (>= 0.71.0)
28
+ ruby-progressbar (1.10.1)
29
+ simplecov (0.18.5)
30
+ docile (~> 1.1)
31
+ simplecov-html (~> 0.11)
32
+ simplecov-html (0.12.2)
33
+ unicode-display_width (1.6.1)
34
+ yard (0.9.24)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ bundler (= 2.1.4)
41
+ byebug (= 11.0.1)
42
+ dotpath!
43
+ minitest (~> 5.0)
44
+ rake (~> 12.0)
45
+ rubocop (= 0.78.0)
46
+ rubocop-performance (= 1.5.2)
47
+ simplecov (= 0.18.5)
48
+ yard (= 0.9.24)
49
+
50
+ BUNDLED WITH
51
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Garrett Bjerkhoel
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.
@@ -0,0 +1,240 @@
1
+ # dotpath
2
+
3
+ dotpath gives you utilities to navigate and modify hashes and arrays in Ruby
4
+ with the JSON path to each value in the hash or array.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'dotpath'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install dotpath
21
+
22
+ ## Usage
23
+
24
+ ### Navigating hashes and arrays
25
+
26
+ **Hash**
27
+
28
+ ```ruby
29
+ hash = {
30
+ action: "user.login",
31
+ users: [
32
+ {
33
+ user: "olddewski",
34
+ user_id: "1"
35
+ },
36
+ {
37
+ user: "dewski",
38
+ user_id: "2"
39
+ },
40
+ ],
41
+ }
42
+
43
+ hash.each_with_json_path do |path, value|
44
+ p [path, value]
45
+ end
46
+
47
+ # [".action", "user.login"]
48
+ # [".users[0].user", "olddewski"]
49
+ # [".users[0].user_id", "1"]
50
+ # [".users[1].user", "dewski"]
51
+ # [".users[1].user_id", "2"]
52
+ ```
53
+
54
+ **Array**
55
+
56
+ ```ruby
57
+ array = [
58
+ {
59
+ action: "user.login",
60
+ users: [
61
+ {
62
+ user: "olddewski",
63
+ user_id: "1"
64
+ },
65
+ {
66
+ user: "dewski",
67
+ user_id: "2"
68
+ },
69
+ ],
70
+ },
71
+ ]
72
+
73
+ array.each_with_json_path do |path, value|
74
+ p [path, value]
75
+ end
76
+
77
+ # [".[0].action", "user.login"]
78
+ # [".[0].users[0].user", "olddewski"]
79
+ # [".[0].users[0].user_id", "1"]
80
+ # [".[0].users[1].user", "dewski"]
81
+ # [".[0].users[1].user_id", "2"]
82
+ ```
83
+
84
+ ### Selecting values from hashes and arrays using JSON paths
85
+
86
+ **Hash**
87
+
88
+ ```ruby
89
+ hash = {
90
+ action: "user.login",
91
+ users: [
92
+ {
93
+ user: "olddewski",
94
+ user_id: "1"
95
+ },
96
+ {
97
+ user: "dewski",
98
+ user_id: "2"
99
+ },
100
+ ],
101
+ }
102
+
103
+ hash.value_at_path(".action") # => "user.login"
104
+ hash.value_at_path(".users[0].user") # => "olddewski"
105
+ hash.value_at_path(".users[0].user_id") # => "1"
106
+ hash.value_at_path(".users[1].user") # => "dewski"
107
+ hash.value_at_path(".users[1].user_id") # => "2"
108
+ ```
109
+
110
+ **Array**
111
+
112
+ ```ruby
113
+ array = [
114
+ {
115
+ action: "user.login",
116
+ users: [
117
+ {
118
+ user: "olddewski",
119
+ user_id: "1"
120
+ },
121
+ {
122
+ user: "dewski",
123
+ user_id: "2"
124
+ },
125
+ ],
126
+ },
127
+ ]
128
+
129
+ array.value_at_path(".[0].action") # => "user.login"
130
+ array.value_at_path(".[0].users[0].user") # => "olddewski"
131
+ array.value_at_path(".[0].users[0].user_id") # => "1"
132
+ array.value_at_path(".[0].users[1].user") # => "dewski"
133
+ array.value_at_path(".[0].users[1].user_id") # => "2"
134
+ ```
135
+
136
+ ### Mutating hashes and arrays
137
+
138
+ ```ruby
139
+ hash = {
140
+ action: "user.login",
141
+ users: [
142
+ {
143
+ user: "olddewski",
144
+ user_id: "1"
145
+ },
146
+ {
147
+ user: "dewski",
148
+ user_id: "2"
149
+ },
150
+ ],
151
+ }
152
+
153
+ hash.collect_with_json_path do |path, value|
154
+ if path == ".action"
155
+ value.gsub('.', '_')
156
+ else
157
+ value
158
+ end
159
+ end
160
+
161
+ pp hash
162
+
163
+ # {
164
+ # action: "user_login",
165
+ # users: [
166
+ # {
167
+ # user: "olddewski",
168
+ # user_id: "1"
169
+ # },
170
+ # {
171
+ # user: "dewski",
172
+ # user_id: "2"
173
+ # },
174
+ # ],
175
+ # }
176
+ ```
177
+
178
+ **Array**
179
+
180
+ ```ruby
181
+ array = [
182
+ {
183
+ action: "user.login",
184
+ users: [
185
+ {
186
+ user: "olddewski",
187
+ user_id: "1"
188
+ },
189
+ {
190
+ user: "dewski",
191
+ user_id: "2"
192
+ },
193
+ ],
194
+ },
195
+ ]
196
+
197
+ array.collect_with_json_path do |path, value|
198
+ if path == ".action"
199
+ value.gsub('.', '_')
200
+ else
201
+ value
202
+ end
203
+ end
204
+
205
+ pp array
206
+
207
+ # [
208
+ # {
209
+ # action: "user_login",
210
+ # users: [
211
+ # {
212
+ # user: "olddewski",
213
+ # user_id: "1"
214
+ # },
215
+ # {
216
+ # user: "dewski",
217
+ # user_id: "2"
218
+ # },
219
+ # ],
220
+ # },
221
+ # ]
222
+ ```
223
+
224
+ ## Development
225
+
226
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
227
+
228
+ 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).
229
+
230
+ ## Contributing
231
+
232
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dewski/dotpath. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/dewski/dotpath/blob/master/CODE_OF_CONDUCT.md).
233
+
234
+ ## License
235
+
236
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
237
+
238
+ ## Code of Conduct
239
+
240
+ Everyone interacting in the dotpath project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/dewski/dotpath/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dotpath"
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(__FILE__)
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rubocop' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rubocop", "rubocop")
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/dotpath/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'dotpath'
7
+ spec.version = Dotpath::VERSION
8
+ spec.authors = ['Garrett Bjerkhoel']
9
+ spec.email = ['me@garrettbjerkhoel.com']
10
+
11
+ spec.summary = 'dotpath gives you utilities to navigate and modify hashes and arrays in Ruby with the JSON path to each value in the hash or array.'
12
+ spec.description = 'dotpath gives you utilities to navigate and modify hashes and arrays in Ruby with the JSON path to each value in the hash or array.'
13
+ spec.homepage = 'https://github.com/dewski/dotpath'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://github.com/dewski/dotpath'
19
+ spec.metadata['changelog_uri'] = 'https://github.com/dewski/dotpath'
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_development_dependency 'bundler', '2.1.4'
31
+ spec.add_development_dependency 'byebug', '11.0.1'
32
+ spec.add_development_dependency 'minitest', '5.13.0'
33
+ spec.add_development_dependency 'rake', '10.5.0'
34
+ spec.add_development_dependency 'rubocop', '0.78.0'
35
+ spec.add_development_dependency 'rubocop-performance', '1.5.2'
36
+ spec.add_development_dependency 'yard', '0.9.24'
37
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dotpath/version'
4
+
5
+ module Dotpath
6
+ DELIMITER = '.'
7
+
8
+ autoload :Walk, 'dotpath/walk'
9
+ autoload :Mutate, 'dotpath/mutate'
10
+ autoload :Extension, 'dotpath/extension'
11
+
12
+ def self.encode(key)
13
+ key = key.to_s
14
+ key.include?(DELIMITER) ? "\"#{key}\"" : key
15
+ end
16
+
17
+ # Retrieve a value at a given JSON path for an Array or Hash.
18
+ #
19
+ # @param object [Hash, Array] The object that can be navigated using JSON path.
20
+ # @param json_path [String] The JSON path for the requested value.
21
+ #
22
+ # @return the value at JSON path.
23
+ def self.value_at_path(object, json_path)
24
+ raise ArgumentError, "#{object.class} does not support JSON path" unless object.respond_to?(:each_with_json_path)
25
+
26
+ val = nil
27
+ object.each_with_json_path do |path, value|
28
+ if json_path == path
29
+ val = value
30
+ break
31
+ end
32
+ end
33
+ val
34
+ end
35
+ end
36
+
37
+ Hash.prepend(Dotpath::Extension)
38
+ Array.prepend(Dotpath::Extension)
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dotpath
4
+ module Extension
5
+ def each_with_json_path(&block)
6
+ Dotpath::Walk.walk(self, &block)
7
+ end
8
+
9
+ def collect_with_json_path(&block)
10
+ Dotpath::Mutate.mutate(self, &block)
11
+ end
12
+
13
+ def value_at_path(json_path)
14
+ Dotpath.value_at_path(self, json_path)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dotpath
4
+ class Mutate
5
+ def self.mutate(obj, &block)
6
+ case obj
7
+ when Hash
8
+ hash(obj, path: '', &block)
9
+ when Array
10
+ array(obj, &block)
11
+ else
12
+ obj
13
+ end
14
+ end
15
+
16
+ def self.hash(obj, path: DELIMITER, &block)
17
+ new_hash = {}
18
+ obj.each do |key, value|
19
+ new_path = [path, key].join(DELIMITER)
20
+ case value
21
+ when Hash
22
+ new_hash[key] = hash(value, path: new_path, &block)
23
+ when Array
24
+ new_hash[key] = array(value, path: new_path, &block)
25
+ else
26
+ new_hash[key] = yield(new_path, value)
27
+ end
28
+ end
29
+ new_hash
30
+ end
31
+
32
+ def self.array(obj, path: DELIMITER, &block)
33
+ new_array = []
34
+ index = 0
35
+ obj.each do |value|
36
+ new_path = "#{path}[#{index}]"
37
+ case value
38
+ when Hash
39
+ new_array << hash(value, path: new_path, &block)
40
+ when Array
41
+ new_array << array(value, path: new_path, &block)
42
+ else
43
+ new_array << yield(new_path, value)
44
+ end
45
+
46
+ index += 1
47
+ end
48
+ new_array
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dotpath
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dotpath
4
+ class Walk
5
+ def self.walk(obj, &block)
6
+ case obj
7
+ when Hash
8
+ hash(obj, path: '', &block)
9
+ when Array
10
+ array(obj, &block)
11
+ else
12
+ obj
13
+ end
14
+ end
15
+
16
+ def self.hash(obj, path: DELIMITER, &block)
17
+ obj.each do |key, value|
18
+ new_path = [path, Dotpath.encode(key)].join(DELIMITER)
19
+ case value
20
+ when Hash
21
+ hash(value, path: new_path, &block)
22
+ when Array
23
+ array(value, path: new_path, &block)
24
+ else
25
+ yield(new_path, value)
26
+ end
27
+ end
28
+ end
29
+
30
+ def self.array(obj, path: DELIMITER, &block)
31
+ index = 0
32
+ obj.each do |value|
33
+ new_path = "#{path}[#{index}]"
34
+ case value
35
+ when Hash
36
+ hash(value, path: new_path, &block)
37
+ when Array
38
+ array(value, path: new_path, &block)
39
+ else
40
+ yield(new_path, value)
41
+ end
42
+
43
+ index += 1
44
+ end
45
+ end
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,182 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dotpath
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Garrett Bjerkhoel
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-10-24 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: 2.1.4
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 11.0.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 11.0.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 5.13.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 5.13.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 10.5.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 10.5.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 0.78.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 0.78.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-performance
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 1.5.2
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 1.5.2
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '='
102
+ - !ruby/object:Gem::Version
103
+ version: 0.9.24
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '='
109
+ - !ruby/object:Gem::Version
110
+ version: 0.9.24
111
+ description: dotpath gives you utilities to navigate and modify hashes and arrays
112
+ in Ruby with the JSON path to each value in the hash or array.
113
+ email:
114
+ - me@garrettbjerkhoel.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".github/workflows/rubocop.yml"
120
+ - ".github/workflows/ruby.yml"
121
+ - ".gitignore"
122
+ - ".rubocop.yml"
123
+ - ".ruby-version"
124
+ - CODE_OF_CONDUCT.md
125
+ - Gemfile
126
+ - Gemfile.lock
127
+ - LICENSE.txt
128
+ - README.md
129
+ - Rakefile
130
+ - bin/console
131
+ - bin/rubocop
132
+ - bin/setup
133
+ - dotpath.gemspec
134
+ - lib/dotpath.rb
135
+ - lib/dotpath/extension.rb
136
+ - lib/dotpath/mutate.rb
137
+ - lib/dotpath/version.rb
138
+ - lib/dotpath/walk.rb
139
+ - vendor/cache/ast-2.4.1.gem
140
+ - vendor/cache/byebug-11.0.1.gem
141
+ - vendor/cache/docile-1.3.2.gem
142
+ - vendor/cache/jaro_winkler-1.5.4.gem
143
+ - vendor/cache/minitest-5.14.2.gem
144
+ - vendor/cache/parallel-1.19.2.gem
145
+ - vendor/cache/parser-2.7.1.4.gem
146
+ - vendor/cache/rainbow-3.0.0.gem
147
+ - vendor/cache/rake-12.3.2.gem
148
+ - vendor/cache/rubocop-0.78.0.gem
149
+ - vendor/cache/rubocop-performance-1.5.2.gem
150
+ - vendor/cache/ruby-progressbar-1.10.1.gem
151
+ - vendor/cache/simplecov-0.18.5.gem
152
+ - vendor/cache/simplecov-html-0.12.2.gem
153
+ - vendor/cache/unicode-display_width-1.6.1.gem
154
+ - vendor/cache/yard-0.9.24.gem
155
+ homepage: https://github.com/dewski/dotpath
156
+ licenses:
157
+ - MIT
158
+ metadata:
159
+ homepage_uri: https://github.com/dewski/dotpath
160
+ source_code_uri: https://github.com/dewski/dotpath
161
+ changelog_uri: https://github.com/dewski/dotpath
162
+ post_install_message:
163
+ rdoc_options: []
164
+ require_paths:
165
+ - lib
166
+ required_ruby_version: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: 2.3.0
171
+ required_rubygems_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ requirements: []
177
+ rubygems_version: 3.0.3
178
+ signing_key:
179
+ specification_version: 4
180
+ summary: dotpath gives you utilities to navigate and modify hashes and arrays in Ruby
181
+ with the JSON path to each value in the hash or array.
182
+ test_files: []