Class: Alfi::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/alfi/search.rb

Instance Method Summary collapse

Constructor Details

#initializeSearch

Returns a new instance of Search.



2
3
4
5
# File 'lib/alfi/search.rb', line 2

def initialize
  $result_list = []
  $suggestions = []
end

Instance Method Details

#call(search_param, search_type) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/alfi/search.rb', line 12

def call(search_param, search_type)
  return puts 'The search needs 3+ characters'.red if search_param.size < 3
  puts "Searching...\n"

  Alfi::Providers.all.each { |cc| cc.new(search_param, search_type).call }

  exit_with('No results'.red) if $result_list.empty? && $suggestions.empty?
  num_results = total_results_count

  if num_results > 0
    puts "\ndependencies {\n"
    $result_list.each { |result| puts result }
    puts "}\n"
  end

  puts "\nFound: #{num_results} result#{num_results > 1?'s':''} for '#{search_param.yellow}'"

  return if $suggestions.empty?
  puts "Did you mean: #{$suggestions.join(', ').yellow}"
end

#exit_with(message) ⇒ Object



7
8
9
10
# File 'lib/alfi/search.rb', line 7

def exit_with(message)
  puts message
  exit 1
end

#total_results_countObject



33
34
35
# File 'lib/alfi/search.rb', line 33

def total_results_count
  return $result_list.count { |r| r.strip[0] != '#' }
end