Class: ProjectEulerCli::ArchiveSearcher
- Inherits:
-
Object
- Object
- ProjectEulerCli::ArchiveSearcher
- Includes:
- Scraper
- Defined in:
- lib/project_euler_cli/archive_searcher.rb
Overview
Handles searching the problems
Instance Attribute Summary collapse
-
#results ⇒ Object
readonly
Array of IDs corresponding to the problems found in last search.
-
#searching ⇒ Object
Tracks whether there is an active search.
Instance Method Summary collapse
-
#initialize(problems) ⇒ ArchiveSearcher
constructor
A new instance of ArchiveSearcher.
-
#load_terms ⇒ Object
Loads the problem numbers and titles for every page that is not loaded.
-
#search(terms) ⇒ Object
Performs a simple search of the problems.
Methods included from Scraper
#load_page, #load_problem_details, #load_recent, #lookup_totals
Constructor Details
#initialize(problems) ⇒ ArchiveSearcher
Returns a new instance of ArchiveSearcher.
12 13 14 15 16 17 18 |
# File 'lib/project_euler_cli/archive_searcher.rb', line 12 def initialize(problems) @problems = problems @results = [] @searching = false @initial_search = true end |
Instance Attribute Details
#results ⇒ Object (readonly)
Array of IDs corresponding to the problems found in last search
8 9 10 |
# File 'lib/project_euler_cli/archive_searcher.rb', line 8 def results @results end |
#searching ⇒ Object
Tracks whether there is an active search
10 11 12 |
# File 'lib/project_euler_cli/archive_searcher.rb', line 10 def searching @searching end |
Instance Method Details
#load_terms ⇒ Object
Loads the problem numbers and titles for every page that is not loaded.
21 22 23 24 25 |
# File 'lib/project_euler_cli/archive_searcher.rb', line 21 def load_terms puts "updating keywords..." 0.upto(Page.total) { |page| load_page(page, @problems) } end |
#search(terms) ⇒ Object
Performs a simple search of the problems. It accepts multiple terms. Results will contain any of the terms
-
terms- String of search terms
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/project_euler_cli/archive_searcher.rb', line 31 def search(terms) if @initial_search @initial_search = false load_terms end puts "searching..." @results.clear @searching = true terms.downcase.split(' ').each do |term| for i in 1..Problem.total if @problems[i].title.downcase.include?(term.downcase) @results << i end end end end |