glob 0.2.1 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby-tests.yml +2 -2
- data/CHANGELOG.md +12 -0
- data/README.md +35 -0
- data/lib/glob/map.rb +6 -2
- data/lib/glob/matcher.rb +9 -1
- data/lib/glob/query.rb +5 -1
- data/lib/glob/symbolize_keys.rb +1 -1
- data/lib/glob/version.rb +1 -1
- data/test/glob/symbolize_keys_test.rb +2 -2
- data/test/glob_test.rb +51 -0
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: daa19b4757ede88d7bc2ff99732f6e0a42b33608fbbef748c21c18f73a01538c
|
4
|
+
data.tar.gz: c8ee66c99f7ce11218ff7fc4031ad0a53e8fe7afb9eaa0de8b9220be92c88690
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56d9f2f4ba26236f4ce29c51a806c6a158c5576222ab2ccc7d801423a6be0ae759ef2b8db21f465d03de2f97aac4b2b7de0a21dcc9d35ceaa74397e720783014
|
7
|
+
data.tar.gz: 8ebc481895cc295261e1914b690ccd4b3807419564c833e258a9efccabf8f2ead910e599007e663227821ca7d03ced3dae9dc491608c832f8e0886956dce579a
|
data/CHANGELOG.md
CHANGED
@@ -11,6 +11,18 @@ Prefix your message with one of the following:
|
|
11
11
|
- [Security] in case of vulnerabilities.
|
12
12
|
-->
|
13
13
|
|
14
|
+
## v0.3.1 - 2022-09-01
|
15
|
+
|
16
|
+
- [Fixed] Handle keys with dots properly by using `\\.` as a escape sequence.
|
17
|
+
|
18
|
+
## v0.3.0 - 2022-08-01
|
19
|
+
|
20
|
+
- [Added] Patterns can have groups like in `{a,b}.*`.
|
21
|
+
|
22
|
+
## v0.2.2 - 2022-02-01
|
23
|
+
|
24
|
+
- [Fixed] Properly handle numeric keys.
|
25
|
+
|
14
26
|
## v0.2.1 - 2022-01-13
|
15
27
|
|
16
28
|
- [Changed] .gem package now include tests files.
|
data/README.md
CHANGED
@@ -28,6 +28,9 @@ There are two types of paths: `include` and `exclude`.
|
|
28
28
|
- The `exclude` path is the one started by `!`, and will prevent that path from
|
29
29
|
being added.
|
30
30
|
|
31
|
+
Rules may also have groups. Let's say you want to target `en.*` and `pt.*`; you
|
32
|
+
case set `{en,pt}.*` rather than having two separate rules.
|
33
|
+
|
31
34
|
The latest rules have more precedence; that means that if you have the rule
|
32
35
|
`*.messages.*`, then add a following rule as `!*.messages.bye`, all
|
33
36
|
`*.messages.*` but `*.messages.bye` will be included.
|
@@ -69,6 +72,38 @@ glob.to_h
|
|
69
72
|
|
70
73
|
Notice that the return result will have symbolized keys.
|
71
74
|
|
75
|
+
If the contains dots, then the result will use `\\.` as the escape sequence.
|
76
|
+
Similarly, you need to escape keys with dots when filtering results.
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
glob = Glob.new(
|
80
|
+
formats: {
|
81
|
+
".txt" => "Text",
|
82
|
+
".json" => "JSON",
|
83
|
+
".rb" => "Ruby"
|
84
|
+
}
|
85
|
+
)
|
86
|
+
|
87
|
+
glob << "*"
|
88
|
+
|
89
|
+
glob.paths
|
90
|
+
#=> ["formats.\\.json", "formats.\\.rb", "formats.\\.txt"]
|
91
|
+
|
92
|
+
glob.to_h
|
93
|
+
#=> {:formats=>{:".json"=>"JSON", :".rb"=>"Ruby", :".txt"=>"Text"}}
|
94
|
+
|
95
|
+
# Remove all existing matchers
|
96
|
+
glob.matchers.clear
|
97
|
+
|
98
|
+
glob << "formats.\\.rb"
|
99
|
+
|
100
|
+
glob.paths
|
101
|
+
#=> ["formats.\\.rb"]
|
102
|
+
|
103
|
+
glob.to_h
|
104
|
+
#=> {:formats=>{:".rb"=>"Ruby"}}
|
105
|
+
```
|
106
|
+
|
72
107
|
## Maintainer
|
73
108
|
|
74
109
|
- [Nando Vieira](https://github.com/fnando)
|
data/lib/glob/map.rb
CHANGED
@@ -13,15 +13,19 @@ module Glob
|
|
13
13
|
|
14
14
|
def call
|
15
15
|
@target
|
16
|
-
.map {|(key, value)| compute(value, key) }
|
16
|
+
.map {|(key, value)| compute(value, escape(key)) }
|
17
17
|
.flatten
|
18
18
|
.sort
|
19
19
|
end
|
20
20
|
|
21
|
+
private def escape(key)
|
22
|
+
key.to_s.gsub(".", "\\.")
|
23
|
+
end
|
24
|
+
|
21
25
|
private def compute(value, path)
|
22
26
|
if value.is_a?(Hash)
|
23
27
|
value.map do |key, other_value|
|
24
|
-
compute(other_value, "#{path}.#{key}")
|
28
|
+
compute(other_value, "#{path}.#{escape(key)}")
|
25
29
|
end
|
26
30
|
else
|
27
31
|
path.to_s
|
data/lib/glob/matcher.rb
CHANGED
@@ -8,7 +8,9 @@ module Glob
|
|
8
8
|
@path = path
|
9
9
|
@reject = path.start_with?("!")
|
10
10
|
|
11
|
-
pattern = Regexp.escape(path.gsub(/^!/, ""))
|
11
|
+
pattern = Regexp.escape(path.gsub(/^!/, ""))
|
12
|
+
.gsub(/(\\{.*?\\})/) {|match| process_group(match) }
|
13
|
+
.gsub(/\\\*/, "[^.]+")
|
12
14
|
@regex = Regexp.new("^#{pattern}")
|
13
15
|
end
|
14
16
|
|
@@ -23,5 +25,11 @@ module Glob
|
|
23
25
|
def reject?
|
24
26
|
@reject
|
25
27
|
end
|
28
|
+
|
29
|
+
def process_group(group)
|
30
|
+
group = group.gsub(/[{}\\]/, "").split(",").join("|")
|
31
|
+
|
32
|
+
"(#{group})"
|
33
|
+
end
|
26
34
|
end
|
27
35
|
end
|
data/lib/glob/query.rb
CHANGED
@@ -18,7 +18,7 @@ module Glob
|
|
18
18
|
symbolized_target = SymbolizeKeys.call(@target)
|
19
19
|
|
20
20
|
paths.each_with_object({}) do |path, buffer|
|
21
|
-
segments = path.split(
|
21
|
+
segments = path.split(/(?<!\\)\./).map {|key| unescape(key).to_sym }
|
22
22
|
value = symbolized_target.dig(*segments)
|
23
23
|
set_path_value(segments, buffer, value)
|
24
24
|
end
|
@@ -41,6 +41,10 @@ module Glob
|
|
41
41
|
@map ||= Map.call(@target)
|
42
42
|
end
|
43
43
|
|
44
|
+
private def unescape(key)
|
45
|
+
key.to_s.gsub(/\\\./, ".")
|
46
|
+
end
|
47
|
+
|
44
48
|
private def set_path_value(segments, target, value)
|
45
49
|
while (segment = segments.shift)
|
46
50
|
if segments.empty?
|
data/lib/glob/symbolize_keys.rb
CHANGED
@@ -6,7 +6,7 @@ module Glob
|
|
6
6
|
case target
|
7
7
|
when Hash
|
8
8
|
target.each_with_object({}) do |(key, value), buffer|
|
9
|
-
buffer[key.to_sym] = SymbolizeKeys.call(value)
|
9
|
+
buffer[key.to_s.to_sym] = SymbolizeKeys.call(value)
|
10
10
|
end
|
11
11
|
when Array
|
12
12
|
target.map {|item| SymbolizeKeys.call(item) }
|
data/lib/glob/version.rb
CHANGED
@@ -4,8 +4,8 @@ require "test_helper"
|
|
4
4
|
|
5
5
|
class SymbolizeKeysTest < Minitest::Test
|
6
6
|
test "symbolizes hash" do
|
7
|
-
input = {"a" => {"b" => 1}}
|
8
|
-
expected = {a: {b: 1}}
|
7
|
+
input = {"a" => {"b" => 1}, 42 => "the answer"}
|
8
|
+
expected = {a: {b: 1}, "42": "the answer"}
|
9
9
|
actual = Glob::SymbolizeKeys.call(input)
|
10
10
|
|
11
11
|
assert_equal expected, actual
|
data/test/glob_test.rb
CHANGED
@@ -189,4 +189,55 @@ class GlobTest < Minitest::Test
|
|
189
189
|
|
190
190
|
assert_equal expected, actual
|
191
191
|
end
|
192
|
+
|
193
|
+
test "using groups as root key" do
|
194
|
+
glob = Glob.new(
|
195
|
+
pt: {hello: "Olá!"},
|
196
|
+
en: {hello: "Hello!"},
|
197
|
+
es: {hello: "¡Hola!"}
|
198
|
+
)
|
199
|
+
|
200
|
+
glob << "{pt,en}.*"
|
201
|
+
|
202
|
+
assert_equal ["en.hello", "pt.hello"], glob.paths
|
203
|
+
end
|
204
|
+
|
205
|
+
test "using groups in mixed positions" do
|
206
|
+
glob = Glob.new(
|
207
|
+
pt: {messages: {hello: "Olá!", goodbye: "Tchau!"}},
|
208
|
+
en: {messages: {hello: "Hello!", goodbye: "Goodbye!"}},
|
209
|
+
es: {messages: {hello: "¡Hola!", goodbye: "¡Adios!"}}
|
210
|
+
)
|
211
|
+
|
212
|
+
glob << "{pt,en}.messages.{hello,goodbye}"
|
213
|
+
|
214
|
+
expected = [
|
215
|
+
"en.messages.goodbye",
|
216
|
+
"en.messages.hello",
|
217
|
+
"pt.messages.goodbye",
|
218
|
+
"pt.messages.hello"
|
219
|
+
]
|
220
|
+
|
221
|
+
assert_equal expected, glob.paths
|
222
|
+
end
|
223
|
+
|
224
|
+
test "with keys that contain dots" do
|
225
|
+
data = {
|
226
|
+
"keys.with.dots" => "dots",
|
227
|
+
node: {
|
228
|
+
"more.keys.with.dots" => "more dots"
|
229
|
+
}
|
230
|
+
}
|
231
|
+
|
232
|
+
glob = Glob.new(data)
|
233
|
+
|
234
|
+
glob << "*"
|
235
|
+
|
236
|
+
assert_equal %w[keys\\.with\\.dots node.more\\.keys\\.with\\.dots],
|
237
|
+
glob.paths
|
238
|
+
assert_equal Glob::SymbolizeKeys.call(data), glob.to_h
|
239
|
+
|
240
|
+
assert_equal ({node: {"more.keys.with.dots": "more dots"}}),
|
241
|
+
Glob.filter(data, ["node.more\\.keys\\.with\\.dots"])
|
242
|
+
end
|
192
243
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: glob
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nando Vieira
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-01
|
11
|
+
date: 2022-09-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -152,10 +152,10 @@ metadata:
|
|
152
152
|
rubygems_mfa_required: 'true'
|
153
153
|
homepage_uri: https://github.com/fnando/glob
|
154
154
|
bug_tracker_uri: https://github.com/fnando/glob/issues
|
155
|
-
source_code_uri: https://github.com/fnando/glob/tree/v0.
|
156
|
-
changelog_uri: https://github.com/fnando/glob/tree/v0.
|
157
|
-
documentation_uri: https://github.com/fnando/glob/tree/v0.
|
158
|
-
license_uri: https://github.com/fnando/glob/tree/v0.
|
155
|
+
source_code_uri: https://github.com/fnando/glob/tree/v0.3.1
|
156
|
+
changelog_uri: https://github.com/fnando/glob/tree/v0.3.1/CHANGELOG.md
|
157
|
+
documentation_uri: https://github.com/fnando/glob/tree/v0.3.1/README.md
|
158
|
+
license_uri: https://github.com/fnando/glob/tree/v0.3.1/LICENSE.md
|
159
159
|
post_install_message:
|
160
160
|
rdoc_options: []
|
161
161
|
require_paths:
|
@@ -171,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
171
171
|
- !ruby/object:Gem::Version
|
172
172
|
version: '0'
|
173
173
|
requirements: []
|
174
|
-
rubygems_version: 3.3.
|
174
|
+
rubygems_version: 3.3.7
|
175
175
|
signing_key:
|
176
176
|
specification_version: 4
|
177
177
|
summary: Create a list of hash paths that match a given pattern. You can also generate
|