glob 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fde0d275a3a9a027d30178a5975f94d947c9fa50a184349365d7e2f7cf0c33f8
4
+ data.tar.gz: d69da7126314e270b9be1ed063a75027fe38e9a899633eb5dfe6ad2be974728e
5
+ SHA512:
6
+ metadata.gz: b2d6c9bcd1926c173326a2f10bc7fddaf2adeb906cf784ddc2d41484f3396481571df1af8ee08aa383a883f5f40da949e3bfbd6ba2fe0d9b1ea6f7ae080e6c2a
7
+ data.tar.gz: a87b5a59497738eff53ff8e8c8765b645e849e1f2c39981ef3fde1eaef7d70dec34c9e762157c4a08308d5c37e5d18f7d5465106a8bfc5e778c3c014d5c4328e
@@ -0,0 +1,2 @@
1
+ ---
2
+ custom: ["https://www.paypal.me/nandovieira/🍕"]
@@ -0,0 +1,47 @@
1
+ ---
2
+ name: tests
3
+
4
+ on: [push]
5
+
6
+ jobs:
7
+ build:
8
+ runs-on: ubuntu-latest
9
+
10
+ steps:
11
+ - name: Clone repo
12
+ uses: actions/checkout@v1
13
+
14
+ - name: Ruby cache
15
+ uses: actions/cache@v1
16
+ id: cache
17
+ with:
18
+ path: ~/local/rubies
19
+ key: ruby-2.6.5
20
+
21
+ - name: Rubygems cache
22
+ uses: actions/cache@v1
23
+ with:
24
+ path: vendor/bundle
25
+ key: ${{ runner.os }}-gem-${{ hashFiles('**/Gemfile.lock') }}
26
+ restore-keys: ${{ runner.os }}-gem-
27
+
28
+ - name: Install ruby
29
+ uses: clupprich/ruby-build-action@master
30
+ id: ruby
31
+ with:
32
+ ruby-version: 2.6.5
33
+ cache-available: ${{ steps.cache.outputs.cache-hit == 'true' }}
34
+
35
+ - name: Bundle install
36
+ run: |
37
+ gem install bundler
38
+ bundle config path vendor/bundle
39
+ bundle install --jobs 4 --retry 3
40
+
41
+ - name: Run ruby tests
42
+ run: |
43
+ bundle exec rake
44
+
45
+ - name: Run lint
46
+ run: |
47
+ bundle exec rubocop
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ ---
2
+ inherit_gem:
3
+ rubocop-fnando: .rubocop.yml
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.6
7
+
8
+ Metrics/BlockLength:
9
+ Exclude:
10
+ - test/**/*_test.rb
11
+ - glob.gemspec
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.5
7
+ before_install: gem install bundler -v 2.0.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## Unreleased
4
+
5
+ - Initial release
@@ -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@fnando.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
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in glob.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ glob (0.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ jaro_winkler (1.5.4)
11
+ minitest (5.13.0)
12
+ minitest-utils (0.4.6)
13
+ minitest
14
+ parallel (1.19.1)
15
+ parser (2.6.5.0)
16
+ ast (~> 2.4.0)
17
+ rainbow (3.0.0)
18
+ rake (13.0.1)
19
+ rubocop (0.77.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-fnando (0.0.3)
27
+ ruby-progressbar (1.10.1)
28
+ unicode-display_width (1.6.0)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ bundler
35
+ glob!
36
+ minitest
37
+ minitest-utils
38
+ rake
39
+ rubocop
40
+ rubocop-fnando
41
+
42
+ BUNDLED WITH
43
+ 2.0.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Nando Vieira
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,82 @@
1
+ # Glob
2
+
3
+ [![](https://github.com/fnando/glob/workflows/tests/badge.svg)](https://github.com/fnando/glob/actions?query=workflow%3Atests)
4
+
5
+ Create a list of hash paths that match a given pattern. You can also generate
6
+ a hash with only the matching paths.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem "glob"
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install glob
23
+
24
+ ## Usage
25
+
26
+ ```ruby
27
+ glob = Glob.new(
28
+ site: {
29
+ settings: {
30
+ name: "Site name",
31
+ url: "https://example.com"
32
+ }
33
+ },
34
+ user: {
35
+ settings: {
36
+ name: "User name"
37
+ }
38
+ }
39
+ )
40
+
41
+ result = glob.query("*.settings.*")
42
+
43
+ result.paths
44
+ #=> ["site.settings.name", "site.settings.url", "user.settings.name"]
45
+
46
+ result.to_h
47
+ #=> {
48
+ #=> site: {
49
+ #=> settings: {
50
+ #=> name: "Site name"
51
+ #=> }
52
+ #=> },
53
+ #=> user: {
54
+ #=> settings: {
55
+ #=> name: "User name"
56
+ #=> }
57
+ #=> }
58
+ #=> }
59
+ ```
60
+
61
+ Notice that the return result will have symbolized keys.
62
+
63
+ If you're planning to do one-off searches, then you can use
64
+ `Glob.query(target, paths)` instead.
65
+
66
+ ## Development
67
+
68
+ 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.
69
+
70
+ 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).
71
+
72
+ ## Contributing
73
+
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fnando/glob. 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.
75
+
76
+ ## License
77
+
78
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
79
+
80
+ ## Code of Conduct
81
+
82
+ Everyone interacting in the Glob project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/fnando/glob/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -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
data/glob.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "./lib/glob/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "glob"
7
+ spec.version = Glob::VERSION
8
+ spec.authors = ["Nando Vieira"]
9
+ spec.email = ["me@fnando.com"]
10
+
11
+ spec.summary = [
12
+ "Create a list of hash paths that match a given pattern.",
13
+ "You can also generate a hash with only the matching paths."
14
+ ].join(" ")
15
+ spec.description = spec.summary
16
+ spec.homepage = "https://github.com/fnando/glob"
17
+ spec.license = "MIT"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/fnando/glob"
21
+ spec.metadata["changelog_uri"] = "https://github.com/fnando/glob"
22
+
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`
25
+ .split("\x0")
26
+ .reject {|f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler"
33
+ spec.add_development_dependency "minitest"
34
+ spec.add_development_dependency "minitest-utils"
35
+ spec.add_development_dependency "rake"
36
+ spec.add_development_dependency "rubocop"
37
+ spec.add_development_dependency "rubocop-fnando"
38
+ end
data/lib/glob.rb ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "glob/map"
4
+ require "glob/query"
5
+ require "glob/result"
6
+ require "glob/version"
7
+
8
+ module Glob
9
+ def self.new(target)
10
+ Query.new(target)
11
+ end
12
+
13
+ def self.query(target, query)
14
+ Query.new(target).query(query)
15
+ end
16
+ end
data/lib/glob/map.rb ADDED
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Glob
4
+ class Map
5
+ def self.call(target)
6
+ new(target).call
7
+ end
8
+
9
+ def initialize(target)
10
+ @keys = []
11
+ @target = target
12
+ end
13
+
14
+ def call
15
+ @target
16
+ .map {|(key, value)| compute(value, key) }
17
+ .flatten
18
+ .sort
19
+ end
20
+
21
+ private def compute(value, path)
22
+ if value.is_a?(Hash)
23
+ value.map do |key, other_value|
24
+ compute(other_value, "#{path}.#{key}")
25
+ end
26
+ else
27
+ path.to_s
28
+ end
29
+ end
30
+ end
31
+ end
data/lib/glob/query.rb ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Glob
4
+ class Query
5
+ def initialize(target)
6
+ @target = target
7
+ end
8
+
9
+ def query(search)
10
+ map = Map.call(@target)
11
+ pattern = Regexp.escape(search).gsub(/\\\*/, "[^.]+")
12
+ regex = Regexp.new("^#{pattern}")
13
+
14
+ matches = map.select {|path| path.match?(regex) }
15
+
16
+ Result.new(paths: matches, target: @target)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Glob
4
+ class Result
5
+ def initialize(target:, paths:)
6
+ @target = target
7
+ @paths = paths
8
+ end
9
+
10
+ def paths
11
+ @paths.dup
12
+ end
13
+
14
+ def to_hash
15
+ target = symbolize_keys(@target)
16
+
17
+ @paths.each_with_object({}) do |path, buffer|
18
+ segments = path.split(".").map(&:to_sym)
19
+ value = target.dig(*segments)
20
+ set_path_value(segments, buffer, value)
21
+ end
22
+ end
23
+
24
+ def symbolize_keys(target)
25
+ case target
26
+ when Hash
27
+ target.each_with_object({}) do |(key, value), buffer|
28
+ buffer[key.to_sym] = symbolize_keys(value)
29
+ end
30
+ when Array
31
+ target.map {|item| symbolize_keys(item) }
32
+ else
33
+ target
34
+ end
35
+ end
36
+
37
+ def set_path_value(segments, target, value)
38
+ while (segment = segments.shift)
39
+ if segments.empty?
40
+ target[segment] = value
41
+ else
42
+ target[segment] ||= {}
43
+ target = target[segment]
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Glob
4
+ VERSION = "0.1.0"
5
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: glob
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nando Vieira
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-12-15 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest-utils
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '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: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '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'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-fnando
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Create a list of hash paths that match a given pattern. You can also
98
+ generate a hash with only the matching paths.
99
+ email:
100
+ - me@fnando.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".github/FUNDING.yml"
106
+ - ".github/workflows/tests.yml"
107
+ - ".gitignore"
108
+ - ".rubocop.yml"
109
+ - ".travis.yml"
110
+ - CHANGELOG.md
111
+ - CODE_OF_CONDUCT.md
112
+ - Gemfile
113
+ - Gemfile.lock
114
+ - LICENSE.txt
115
+ - README.md
116
+ - Rakefile
117
+ - glob.gemspec
118
+ - lib/glob.rb
119
+ - lib/glob/map.rb
120
+ - lib/glob/query.rb
121
+ - lib/glob/result.rb
122
+ - lib/glob/version.rb
123
+ homepage: https://github.com/fnando/glob
124
+ licenses:
125
+ - MIT
126
+ metadata:
127
+ homepage_uri: https://github.com/fnando/glob
128
+ source_code_uri: https://github.com/fnando/glob
129
+ changelog_uri: https://github.com/fnando/glob
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubygems_version: 3.0.3
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: Create a list of hash paths that match a given pattern. You can also generate
149
+ a hash with only the matching paths.
150
+ test_files: []