Class: Arli::Commands::Search

Inherits:
Base
  • Object
show all
Defined in:
lib/arli/commands/search.rb

Constant Summary

Constants included from Helpers::Output

Helpers::Output::CHAR_FAILURE, Helpers::Output::CHAR_SUCCESS

Instance Attribute Summary collapse

Attributes inherited from Base

#config, #name

Instance Method Summary collapse

Methods inherited from Base

#library_path, #runtime, #setup, #temp_path

Methods included from Helpers::Output

#___, #__p, #__pf, #__pt, #abort?, #action_fail, #action_ok, #backup?, #cursor, #debug, #debug?, disable!, enable!, enabled?, #error, #fuck, #header, #hr, #indent_cursor, #info, #ok, #overwrite?, #print_action_failure, #print_action_starting, #print_action_success, #print_target_dir, #quiet?, #raise_invalid_arli_command!, #report_exception, #verbose?

Constructor Details

#initialize(*args) ⇒ Search

Returns a new instance of Search.



26
27
28
29
30
31
32
# File 'lib/arli/commands/search.rb', line 26

def initialize(*args)
  super(*args)
  self.format   = config.search.results.output_format
  valid_methods = Arli::Library::MultiVersion.format_methods
  raise Arli::Errors::InvalidSearchSyntaxError,
        "invalid format #{format}" unless valid_methods.include?(format)
end

Instance Attribute Details

#databaseObject

Returns the value of attribute database.



17
18
19
# File 'lib/arli/commands/search.rb', line 17

def database
  @database
end

#formatObject

Returns the value of attribute format.



17
18
19
# File 'lib/arli/commands/search.rb', line 17

def format
  @format
end

#limitObject

Returns the value of attribute limit.



17
18
19
# File 'lib/arli/commands/search.rb', line 17

def limit
  @limit
end

#resultsObject

Returns the value of attribute results.



17
18
19
# File 'lib/arli/commands/search.rb', line 17

def results
  @results
end

#search_methodObject

Returns the value of attribute search_method.



17
18
19
# File 'lib/arli/commands/search.rb', line 17

def search_method
  @search_method
end

#search_optsObject

Returns the value of attribute search_opts.



17
18
19
# File 'lib/arli/commands/search.rb', line 17

def search_opts
  @search_opts
end

#search_stringObject

Returns the value of attribute search_string.



17
18
19
# File 'lib/arli/commands/search.rb', line 17

def search_string
  @search_string
end

#unique_librariesObject

Returns the value of attribute unique_libraries.



17
18
19
# File 'lib/arli/commands/search.rb', line 17

def unique_libraries
  @unique_libraries
end

Instance Method Details

#add_lib_or_version(lib) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/arli/commands/search.rb', line 54

def add_lib_or_version(lib)
  a_version = Arli::Library::MultiVersion.new(lib)
  if unique_libraries.include?(a_version)
    unique_libraries.find { |l| l.name == a_version.name }&.add_version(library: lib)
  else
    unique_libraries << a_version
  end
end

#additional_infoObject



34
35
36
# File 'lib/arli/commands/search.rb', line 34

def additional_info
  "\nSearching For: #{runtime.argv.join(' ').bold.green}\n"
end

#extract_search_argument!Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/arli/commands/search.rb', line 90

def extract_search_argument!
  search = runtime.argv.first
  if search =~ /:/
    self.search_method = :ruby
    search
  elsif search.start_with?('/')
    self.search_method = :regex_name_and_url
    # exact match
    "#{config.search.default_field}: #{search}"
  elsif search.start_with?('=')
    self.search_method = :equals
    # exact match
    "#{config.search.default_field}: '#{search[1..-1]}'"
  elsif search
    self.search_method = :regex
    "#{config.search.default_field}: /#{search.downcase}/i"
  end
end

#handle_and_raise_error(e) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/arli/commands/search.rb', line 116

def handle_and_raise_error(e)
  message = e.message
  if message =~ /undefined method.*Arduino::Library::Model/
    message = 'Invalid attributed search. Possible values are:' +
        "\n#{Arduino::Library::Types::LIBRARY_PROPERTIES.keys}"
  end
  raise Arli::Errors::InvalidSearchSyntaxError,
        "Search string '#{search_string}' is invalid.\n" +
            message.red
end

#paramsObject



137
138
139
# File 'lib/arli/commands/search.rb', line 137

def params
  search_opts
end


127
128
129
130
131
132
133
134
135
# File 'lib/arli/commands/search.rb', line 127

def print_total_with_help
  hr_short
  puts "  Total Versions : #{results.size.to_s.bold.magenta}\n"
  puts "Unique Libraries : #{unique_libraries.size.to_s.bold.magenta}\n"
  hr_short
  if results.size == Arli::Configuration::DEFAULT_RESULTS_LIMIT
    puts "Hint: use #{'-m 5'.bold.green} to limit the result set."
  end
end

#process_search_options!Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/arli/commands/search.rb', line 63

def process_search_options!
  self.search_string = extract_search_argument!
  raise_error_unless_search_string!

  self.limit  = config.search.results.limit
  search_opts = {}
  begin
    params_code = "{ #{search_string} }"
    puts "Evaluating: [#{params_code.blue}]\nSearch Method: [#{search_method.to_s.green}]" if config.trace
    search_opts = eval(params_code)

  rescue => e
    handle_and_raise_error(e)
  end

  unless search_opts.is_a?(::Hash) && search_opts.size > 0
    raise Arli::Errors::InvalidSearchSyntaxError,
          "Search string '#{search_string}' did not eval to Hash.\n"
  end

  self.database = db_default

  search_opts.merge!(limit: limit) if limit && limit > 0
  search_opts.delete(:limit) if limit == 0
  search_opts
end

#raise_error_unless_search_string!Object



109
110
111
112
113
114
# File 'lib/arli/commands/search.rb', line 109

def raise_error_unless_search_string!
  unless search_string
    raise Arli::Errors::InvalidSyntaxError,
          'Expected an argument or a flag to follow the command ' + 'search'.bold.green
  end
end

#runObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/arli/commands/search.rb', line 38

def run
  self.search_opts      = process_search_options!
  self.results          = search(database, **search_opts).sort
  self.unique_libraries = Set.new

  results.map { |lib| add_lib_or_version(lib) }

  unique_libraries.each do |multi_version|
    puts multi_version.send("to_s_#{format}".to_sym)
  end
  print_total_with_help
rescue Exception => e
  error e
  puts e.backtrace.join("\n") if ENV['DEBUG']
end