Module: Yamlook::Search

Defined in:
lib/yamlook/search.rb

Overview

Searches for occurrences of dot-notated keys in all yaml files of current directory

Constant Summary collapse

NoArgumentsError =
Class.new(ArgumentError)
EXTENSIONS =
%w[yml yaml].freeze
PATTERN =
EXTENSIONS.map { |ext| ::File.join('**', "*.#{ext}") }.freeze

Class Method Summary collapse

Class Method Details

.perform(keys) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/yamlook/search.rb', line 14

def perform(keys)
  raise NoArgumentsError, "Nothing to search for.\n" if keys.empty?

  findings = Dir.glob(PATTERN).flat_map do |filename|
    result = File.new(filename).search(keys)
    print_progress(result)
    result
  end

  print_result(findings.compact)
rescue NoArgumentsError => e
  puts e.message
end


38
39
40
# File 'lib/yamlook/search.rb', line 38

def print_failure
  puts 'Nothing found.'
end


28
29
30
# File 'lib/yamlook/search.rb', line 28

def print_progress(result)
  print result ? "\033[32m*\033[0m" : "\033[33m.\033[0m"
end


32
33
34
35
36
# File 'lib/yamlook/search.rb', line 32

def print_result(findings)
  puts
  puts '-' * 10
  findings.any? ? print_success(findings) : print_failure
end


42
43
44
45
46
47
48
# File 'lib/yamlook/search.rb', line 42

def print_success(findings)
  print "Found #{findings.count} occurrences:"
  findings.each do |finding|
    puts
    puts finding
  end
end