Class: ProjectEulerCli::ArchiveController

Inherits:
Object
  • Object
show all
Defined in:
lib/project_euler_cli/archive_controller.rb

Overview

Controller class that manages the archive system. It holds the archive data used by ArchiveViewer and ArchiveSearcher.

Instance Method Summary collapse

Constructor Details

#initializeArchiveController

Returns a new instance of ArchiveController.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/project_euler_cli/archive_controller.rb', line 7

def initialize
  @archive_data = {}

  lookup_totals

  @av = ArchiveViewer.new(@archive_data)
  @as = ArchiveSearcher.new(@archive_data)

  @archive_data[:visited_pages] = []
  @archive_data[:problems] = Array.new(@archive_data[:num_problems] + 1, "")
  @archive_data[:problem_details] = Array.new(@archive_data[:num_problems] + 1) { {} }
end

Instance Method Details

#display_page(page) ⇒ Object



80
81
82
# File 'lib/project_euler_cli/archive_controller.rb', line 80

def display_page(page)
  @av.display_page(page)
end

#display_problem(id) ⇒ Object



88
89
90
# File 'lib/project_euler_cli/archive_controller.rb', line 88

def display_problem(id)
  @av.display_problem(id)
end

#display_recentObject



76
77
78
# File 'lib/project_euler_cli/archive_controller.rb', line 76

def display_recent
  @av.display_recent
end

#display_resultsObject



84
85
86
# File 'lib/project_euler_cli/archive_controller.rb', line 84

def display_results
  @av.display_custom_page(@as.results)
end

#get_page(id) ⇒ Object

call-seq:

get_page(id) => page

Returns page number based on the ID of the problem. The recent page is considered page 0, invalid pages return -1.



25
26
27
28
29
30
31
32
33
# File 'lib/project_euler_cli/archive_controller.rb', line 25

def get_page(id)
  if id.between?(@archive_data[:num_problems] - 9, @archive_data[:num_problems])
    0
  elsif id.between?(1, @archive_data[:num_problems] - 10)
    (id - 1) / 50 + 1
  else
    -1
  end
end

#lookup_totalsObject

Pulls information from the recent page to determine the total number of problems and pages.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/project_euler_cli/archive_controller.rb', line 37

def lookup_totals
  html = open("https://projecteuler.net/recent")
  fragment = Nokogiri::HTML(html)

  id_col = fragment.css('#problems_table td.id_column')

  # The newest problem is the first one listed on the recent page. The ID of this
  # problem will always equal the total number of problems.
  @archive_data[:num_problems] = id_col.first.text.to_i
  # The last problem on the recent page has an ID that is one larger than the
  # last problem in the archive pages. The total number of pages can be calculated
  # from its ID.
  @archive_data[:num_pages] = get_page(id_col.last.text.to_i - 1)
end

#num_pagesObject



52
53
54
# File 'lib/project_euler_cli/archive_controller.rb', line 52

def num_pages
  @archive_data[:num_pages]
end

#num_problemsObject



56
57
58
# File 'lib/project_euler_cli/archive_controller.rb', line 56

def num_problems
  @archive_data[:num_problems]
end

#results_include?(id) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/project_euler_cli/archive_controller.rb', line 72

def results_include?(id)
  @as.results.include?(id)
end

#search(terms) ⇒ Object



68
69
70
# File 'lib/project_euler_cli/archive_controller.rb', line 68

def search(terms)
  @as.search(terms)
end

#searchingObject



64
65
66
# File 'lib/project_euler_cli/archive_controller.rb', line 64

def searching
  @as.searching
end

#searching=(searching) ⇒ Object



60
61
62
# File 'lib/project_euler_cli/archive_controller.rb', line 60

def searching=(searching)
  @as.searching = searching
end