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?

  # TODO: cache fast key lookup in a temporary sqlite db?
  query = parse_key(query_string)
  files = Dir.glob(File.join(dir, '**', '*.yml'))
  files.each do |file|
    FileExtractor.call(file) do |key|
      yield key if key_match?(key.path, query)
    end
  rescue YAML::SyntaxError
    # just skip files we can't parse
    # TODO: silence warnings?
    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

Raises:



227
228
229
# File 'lib/findyml.rb', line 227

def self.invalid_key!
  raise Error, "invalid key"
end

.key_match?(path, query) ⇒ Boolean

Returns:

  • (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

Raises:

  • (ArgumentError)


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\./ # start with a dot
    key = $' # everything after the dot
    pre << :splat
  end
  if key =~ /\.\z/ # end with a dot
    key = $` # everything before the dot
    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
  # starts with quote
  when /\A['"]/
     # invalid unless rest has matching quote
    invalid_key! unless $' =~ /#{$&}/

    # everything before the next matching quote (i.e. contents of quotes)
    quoted_key = $`

    case $'
    # quote was at end of string
    when ''     then [quoted_key]

    # dot follows quote, parse everything after the dot
    when /\A\./ then [quoted_key, *parse_key_parts($')]

    # anything else after the quote is not allowed
    else invalid_key!
    end

  # includes a dot
  when /\./
    # invalid unless something before the dot
    invalid_key! if $`.empty?

    k = $` == '*' ? :splat : $`

    # parse everything after the dot
    [k, *parse_key_parts($')]

  # splat at the end of the string
  when '*' then [:splat]

  # single key query
  else [key]
  end
end