Class: Search::CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(search, options) ⇒ CLI

Returns a new instance of CLI.



5
6
7
8
9
# File 'lib/search/cli.rb', line 5

def initialize(search, options)
  @search = search
  @glob = options[:files]
  @limit = options[:n]
end

Instance Attribute Details

#globObject (readonly)

Returns the value of attribute glob.



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

def glob
  @glob
end

#limitObject (readonly)

Returns the value of attribute limit.



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

def limit
  @limit
end

#searchObject (readonly)

Returns the value of attribute search.



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

def search
  @search
end

Instance Method Details

#filenamesObject



19
20
21
# File 'lib/search/cli.rb', line 19

def filenames
  Dir.glob(glob)
end

#filesObject

Raises:



11
12
13
14
15
16
17
# File 'lib/search/cli.rb', line 11

def files
  raise Error, 'You must provide files to search' if glob.nil?

  filenames.map do |filename|
    File.open(filename)
  end
end

#qualitiesObject



23
24
25
26
27
28
29
# File 'lib/search/cli.rb', line 23

def qualities
  filenames_with_quality = files.map do |file|
    [Search.new(search, haystack: file.read).quality, file.path]
  end

  filenames_with_quality.sort_by(&:first).reverse[0, limit]
end

#suggestionsObject



31
32
33
34
35
# File 'lib/search/cli.rb', line 31

def suggestions
  files.map do |file|
    Search.new(search, haystack: file.read).suggestions
  end[0, limit]
end