Sha256: 7ea37f4a1df41d7f2a65b2bb515805c3b3f03dd28300e1c111d19a055a63e4fb

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

module ProjectEulerCli

# Handles searching the problems
class ArchiveSearcher
  include Scraper

  # Array of IDs corresponding to the problems found in last search
  attr_reader :results
  # Tracks whether there is an active search
  attr_accessor :searching

  def initialize(problems)
    @problems = problems

    @results = []
    @searching = false
    @initial_search = true
  end

  # Loads the problem numbers and titles for every page that is not loaded.
  def load_terms
    puts "updating keywords..."

    0.upto(Page.total) { |page| load_page(page, @problems) }
  end

  # Performs a simple search of the problems. It accepts multiple terms. Results
  # will contain *any* of the terms
  #
  # * +terms+ - String of search terms
  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

end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
project_euler_cli-1.1.2 lib/project_euler_cli/archive_searcher.rb
project_euler_cli-1.1.1 lib/project_euler_cli/archive_searcher.rb