Module: Findyml
- Defined in:
- lib/findyml.rb,
lib/findyml/version.rb
Defined Under Namespace
Classes: Error, FileExtractor, IndexNode, Key, KeyNode
Constant Summary
collapse
- VERSION =
"0.1.0"
Class Method Summary
collapse
Class Method Details
.find(query_string, dir = Dir.pwd) ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/findyml.rb', line 149
def self.find(query_string, dir = Dir.pwd)
return to_enum(:find, query_string, dir) unless block_given?
query = parse_key(query_string)
files = Dir.glob(File.join(dir, '**', '*.yml'))
files.each do |file|
.call(file) do |key|
yield key if key_match?(key.path, query)
end
rescue YAML::SyntaxError
warn "Skipping #{file} due to parse error"
end
end
|
.find_and_print(*args) ⇒ Object
166
167
168
169
170
|
# File 'lib/findyml.rb', line 166
def self.find_and_print(*args)
find(*args) do |key|
puts "#{key.file}:#{key.line}:#{key.col}#{key.alias_path.map{"(#{_1.start_line+1})"}.join('')}"
end
end
|
.invalid_key! ⇒ Object
227
228
229
|
# File 'lib/findyml.rb', line 227
def self.invalid_key!
raise Error, "invalid key"
end
|
.key_match?(path, query) ⇒ Boolean
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
# File 'lib/findyml.rb', line 231
def self.key_match?(path, query)
path == query unless query.include? :splat
query_parts = query.slice_before(:splat)
query_parts.each do |q|
case q
in [:splat]
return false if path.empty?
path = []
in [:splat, *partial]
return false if path.empty?
rest = munch(path[1..], partial)
return false unless rest
path = rest
else
return false unless path[0...q.size] == q
path = path.drop(q.size)
end
end
path.empty?
end
|
.munch(arr, part) ⇒ Object
255
256
257
258
259
260
261
262
|
# File 'lib/findyml.rb', line 255
def self.munch(arr, part)
raise ArgumentError, "part must not be empty" if part.empty?
return if arr.empty?
return if arr.size < part.size
return arr[part.size..] if arr[0...part.size] == part
munch(arr[1..], part)
end
|
.parse_key(key) ⇒ Object
172
173
174
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/findyml.rb', line 172
def self.parse_key(key)
pre = []
post = []
if key =~ /\A\./
key = $'
pre << :splat
end
if key =~ /\.\z/
key = $`
post << :splat
end
[*pre, *parse_key_parts(key), *post]
end
|
.parse_key_parts(key) ⇒ Object
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
# File 'lib/findyml.rb', line 186
def self.parse_key_parts(key)
invalid_key! if key.empty?
case key
when /\A['"]/
invalid_key! unless $' =~ /#{$&}/
quoted_key = $`
case $'
when '' then [quoted_key]
when /\A\./ then [quoted_key, *parse_key_parts($')]
else invalid_key!
end
when /\./
invalid_key! if $`.empty?
k = $` == '*' ? :splat : $`
[k, *parse_key_parts($')]
when '*' then [:splat]
else [key]
end
end
|