Class: ProjectEulerCli::ArchiveSearcher

Inherits:
Object
  • Object
show all
Includes:
Scraper
Defined in:
lib/project_euler_cli/archive_searcher.rb

Overview

Handles searching the problems

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#resultsObject (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

#searchingObject

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_termsObject

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 all of the search terms.

  • terms - String of search terms



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/project_euler_cli/archive_searcher.rb', line 31

def search(terms)
  load_terms if Page.visited != (0..Page.total).to_a

  puts "searching..."
  @searching = true

  @results = (1..Problem.total).select do |i|
    terms.downcase.split(' ').all? do |term|
      @problems[i].title.downcase.include?(term)
    end
  end
end