Class: Search::CLI
- Inherits:
-
Object
- Object
- Search::CLI
- Defined in:
- lib/search/cli.rb
Instance Attribute Summary collapse
-
#glob ⇒ Object
readonly
Returns the value of attribute glob.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#search ⇒ Object
readonly
Returns the value of attribute search.
Instance Method Summary collapse
- #filenames ⇒ Object
- #files ⇒ Object
-
#initialize(search, options) ⇒ CLI
constructor
A new instance of CLI.
- #qualities ⇒ Object
- #suggestions ⇒ Object
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, ) @search = search @glob = [:files] @limit = [:n] end |
Instance Attribute Details
#glob ⇒ Object (readonly)
Returns the value of attribute glob.
3 4 5 |
# File 'lib/search/cli.rb', line 3 def glob @glob end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
3 4 5 |
# File 'lib/search/cli.rb', line 3 def limit @limit end |
#search ⇒ Object (readonly)
Returns the value of attribute search.
3 4 5 |
# File 'lib/search/cli.rb', line 3 def search @search end |
Instance Method Details
#filenames ⇒ Object
19 20 21 |
# File 'lib/search/cli.rb', line 19 def filenames Dir.glob(glob) end |
#files ⇒ Object
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 |
#qualities ⇒ Object
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 |
#suggestions ⇒ Object
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 |