hiiro 0.1.57 → 0.1.58
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 +4 -4
- data/lib/hiiro/prefix_matcher.rb +107 -107
- data/lib/hiiro/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0f183baf860848362b475e46afc9f6519e244e341cc2c63e07a5917dee51603a
|
|
4
|
+
data.tar.gz: de0fde039502136565942f159e499f0bb99eb76cb6a4bc25990174131b0b8779
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8871dc00189d5e8a640ac91b0b7ac28759ead113adf8675e848fffa79282524547140b93f2ad51a9b2b7ecea69a6dd78b290306288440303129bdb8ae03817f9
|
|
7
|
+
data.tar.gz: 3b2a9793aea6c1d54d52141c98549f7fc01d017d4e11c5fecca424383430f917252b62b8aa62545cd4aced9771c3429d306fdbf6c7a4b1c1e590b829f9285736
|
data/lib/hiiro/prefix_matcher.rb
CHANGED
|
@@ -1,5 +1,112 @@
|
|
|
1
1
|
class Hiiro
|
|
2
2
|
class PrefixMatcher
|
|
3
|
+
class << self
|
|
4
|
+
def find(items, prefix, key: nil, &block)
|
|
5
|
+
new(items, key, &block).find(prefix)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def find_all(items, prefix, key: nil, &block)
|
|
9
|
+
new(items, key, &block).find_all(prefix)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def resolve(items, prefix, key: nil, &block)
|
|
13
|
+
new(items, key, &block).resolve(prefix)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def find_path(items, prefix, key: nil, &block)
|
|
17
|
+
new(items, key, &block).find_path(prefix)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def find_all_paths(items, prefix, key: nil, &block)
|
|
21
|
+
new(items, key, &block).find_all_paths(prefix)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def resolve_path(items, prefix, key: nil, &block)
|
|
25
|
+
new(items, key, &block).resolve_path(prefix)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
attr_reader :original_items, :key, :block
|
|
30
|
+
|
|
31
|
+
def initialize(items, key = nil, &block)
|
|
32
|
+
@original_items = items
|
|
33
|
+
@key = key
|
|
34
|
+
@block = block
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def all_items(key = nil, &block)
|
|
38
|
+
use_key = key.nil? && !block_given? ? @key : key
|
|
39
|
+
use_block = key.nil? && !block_given? ? @block : block
|
|
40
|
+
|
|
41
|
+
@all_items_cache ||= {}
|
|
42
|
+
cache_key = [use_key, use_block].hash
|
|
43
|
+
|
|
44
|
+
@all_items_cache[cache_key] ||= original_items.map { |item|
|
|
45
|
+
Item.new(
|
|
46
|
+
item: item,
|
|
47
|
+
extracted_item: extract(item, use_key, &use_block),
|
|
48
|
+
key: use_key,
|
|
49
|
+
block: use_block
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def search(prefix, key = nil, &block)
|
|
55
|
+
Result.new(
|
|
56
|
+
matcher: self,
|
|
57
|
+
all_items: all_items(key, &block),
|
|
58
|
+
prefix: prefix,
|
|
59
|
+
key: key || @key,
|
|
60
|
+
block: block || @block
|
|
61
|
+
)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def find(prefix, key = nil, &block)
|
|
65
|
+
search(prefix, key, &block)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def find_all(prefix, key = nil, &block)
|
|
69
|
+
search(prefix, key, &block)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def resolve(prefix, key = nil, &block)
|
|
73
|
+
search(prefix, key, &block)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def find_path(prefix, key = nil, &block)
|
|
77
|
+
search_path(prefix, key, &block)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def find_all_paths(prefix, key = nil, &block)
|
|
81
|
+
search_path(prefix, key, &block)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def resolve_path(prefix, key = nil, &block)
|
|
85
|
+
search_path(prefix, key, &block)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def search_path(prefix, key = nil, &block)
|
|
89
|
+
PathResult.new(
|
|
90
|
+
matcher: self,
|
|
91
|
+
all_items: all_items(key, &block),
|
|
92
|
+
prefix: prefix,
|
|
93
|
+
key: key || @key,
|
|
94
|
+
block: block || @block
|
|
95
|
+
)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
private
|
|
99
|
+
|
|
100
|
+
def matches?(item, prefix)
|
|
101
|
+
item.to_s.start_with?(prefix.to_s)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def extract(item, key = nil, &block)
|
|
105
|
+
return block.call(item) if block
|
|
106
|
+
return item.send(key) if key
|
|
107
|
+
item
|
|
108
|
+
end
|
|
109
|
+
|
|
3
110
|
class Item
|
|
4
111
|
attr_reader :item, :extracted_item, :key, :block
|
|
5
112
|
|
|
@@ -130,112 +237,5 @@ class Hiiro
|
|
|
130
237
|
matches.first
|
|
131
238
|
end
|
|
132
239
|
end
|
|
133
|
-
|
|
134
|
-
class << self
|
|
135
|
-
def find(items, prefix, key: nil, &block)
|
|
136
|
-
new(items, key, &block).find(prefix)
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
def find_all(items, prefix, key: nil, &block)
|
|
140
|
-
new(items, key, &block).find_all(prefix)
|
|
141
|
-
end
|
|
142
|
-
|
|
143
|
-
def resolve(items, prefix, key: nil, &block)
|
|
144
|
-
new(items, key, &block).resolve(prefix)
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
def find_path(items, prefix, key: nil, &block)
|
|
148
|
-
new(items, key, &block).find_path(prefix)
|
|
149
|
-
end
|
|
150
|
-
|
|
151
|
-
def find_all_paths(items, prefix, key: nil, &block)
|
|
152
|
-
new(items, key, &block).find_all_paths(prefix)
|
|
153
|
-
end
|
|
154
|
-
|
|
155
|
-
def resolve_path(items, prefix, key: nil, &block)
|
|
156
|
-
new(items, key, &block).resolve_path(prefix)
|
|
157
|
-
end
|
|
158
|
-
end
|
|
159
|
-
|
|
160
|
-
attr_reader :original_items, :key, :block
|
|
161
|
-
|
|
162
|
-
def initialize(items, key = nil, &block)
|
|
163
|
-
@original_items = items
|
|
164
|
-
@key = key
|
|
165
|
-
@block = block
|
|
166
|
-
end
|
|
167
|
-
|
|
168
|
-
def all_items(key = nil, &block)
|
|
169
|
-
use_key = key.nil? && !block_given? ? @key : key
|
|
170
|
-
use_block = key.nil? && !block_given? ? @block : block
|
|
171
|
-
|
|
172
|
-
@all_items_cache ||= {}
|
|
173
|
-
cache_key = [use_key, use_block].hash
|
|
174
|
-
|
|
175
|
-
@all_items_cache[cache_key] ||= original_items.map { |item|
|
|
176
|
-
Item.new(
|
|
177
|
-
item: item,
|
|
178
|
-
extracted_item: extract(item, use_key, &use_block),
|
|
179
|
-
key: use_key,
|
|
180
|
-
block: use_block
|
|
181
|
-
)
|
|
182
|
-
}
|
|
183
|
-
end
|
|
184
|
-
|
|
185
|
-
def search(prefix, key = nil, &block)
|
|
186
|
-
Result.new(
|
|
187
|
-
matcher: self,
|
|
188
|
-
all_items: all_items(key, &block),
|
|
189
|
-
prefix: prefix,
|
|
190
|
-
key: key || @key,
|
|
191
|
-
block: block || @block
|
|
192
|
-
)
|
|
193
|
-
end
|
|
194
|
-
|
|
195
|
-
def find(prefix, key = nil, &block)
|
|
196
|
-
search(prefix, key, &block)
|
|
197
|
-
end
|
|
198
|
-
|
|
199
|
-
def find_all(prefix, key = nil, &block)
|
|
200
|
-
search(prefix, key, &block)
|
|
201
|
-
end
|
|
202
|
-
|
|
203
|
-
def resolve(prefix, key = nil, &block)
|
|
204
|
-
search(prefix, key, &block)
|
|
205
|
-
end
|
|
206
|
-
|
|
207
|
-
def find_path(prefix, key = nil, &block)
|
|
208
|
-
search_path(prefix, key, &block)
|
|
209
|
-
end
|
|
210
|
-
|
|
211
|
-
def find_all_paths(prefix, key = nil, &block)
|
|
212
|
-
search_path(prefix, key, &block)
|
|
213
|
-
end
|
|
214
|
-
|
|
215
|
-
def resolve_path(prefix, key = nil, &block)
|
|
216
|
-
search_path(prefix, key, &block)
|
|
217
|
-
end
|
|
218
|
-
|
|
219
|
-
def search_path(prefix, key = nil, &block)
|
|
220
|
-
PathResult.new(
|
|
221
|
-
matcher: self,
|
|
222
|
-
all_items: all_items(key, &block),
|
|
223
|
-
prefix: prefix,
|
|
224
|
-
key: key || @key,
|
|
225
|
-
block: block || @block
|
|
226
|
-
)
|
|
227
|
-
end
|
|
228
|
-
|
|
229
|
-
private
|
|
230
|
-
|
|
231
|
-
def matches?(item, prefix)
|
|
232
|
-
item.to_s.start_with?(prefix.to_s)
|
|
233
|
-
end
|
|
234
|
-
|
|
235
|
-
def extract(item, key = nil, &block)
|
|
236
|
-
return block.call(item) if block
|
|
237
|
-
return item.send(key) if key
|
|
238
|
-
item
|
|
239
|
-
end
|
|
240
240
|
end
|
|
241
241
|
end
|
data/lib/hiiro/version.rb
CHANGED