Class: Findex::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/findex/cli.rb

Constant Summary collapse

USAGE =
"Usage: #{$PROGRAM_NAME} (index|search) [path] [-- query]".freeze

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Cli

Returns a new instance of Cli.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/findex/cli.rb', line 8

def initialize(args)
  query_separator_index = args.find_index('--') || -1
  action, path = args[0..query_separator_index]

  case action
  when 'index' then index(path || '.')
  when 'search'
    abort(USAGE) if query_separator_index == -1
    search(path, args[query_separator_index..-1])
  else abort("Unsupported action '#{action}'\n#{USAGE}")
  end
end

Instance Method Details

#index(path) ⇒ Object



21
22
23
# File 'lib/findex/cli.rb', line 21

def index(path)
  Indexer.new(path || '.').start
end

#search(path, query_terms) ⇒ Object



25
26
27
28
29
# File 'lib/findex/cli.rb', line 25

def search(path, query_terms)
  Search.new(path || '.').query(query_terms).each do |document|
    puts document.full_path
  end
end