Class: PostRunner::PagingButtons

Inherits:
Object
  • Object
show all
Defined in:
lib/postrunner/PagingButtons.rb

Overview

A class to generate a set of forward/backward buttons for an HTML page. It can also include jump to first/last buttons.

Instance Method Summary collapse

Constructor Details

#initialize(page_urls, end_buttons = true) ⇒ PagingButtons

Create a new PagingButtons object.

Parameters:

  • page_urls (Array of String)

    Sorted list of all possible pages

  • end_buttons (Boolean) (defaults to: true)

    If true jump to first/last buttons are included



25
26
27
28
29
30
31
32
# File 'lib/postrunner/PagingButtons.rb', line 25

def initialize(page_urls, end_buttons = true)
  if page_urls.empty?
    raise ArgumentError.new("'page_urls' must not be empty")
  end
  @pages = page_urls
  @current_page_index = 0
  @end_buttons = end_buttons
end

Instance Method Details

#current_pageObject

Return the URL of the current page



35
36
37
# File 'lib/postrunner/PagingButtons.rb', line 35

def current_page
  @pages[@current_page_index]
end

#current_page=(page_url) ⇒ Object

Set the URL for the current page. It must be included in the URL set passed at creation time. The forward/backward links will be derived from the setting of the current page.

Parameters:

  • page_url (String)

    URL of the page



43
44
45
46
47
# File 'lib/postrunner/PagingButtons.rb', line 43

def current_page=(page_url)
  unless (@current_page_index = @pages.index(page_url))
    raise ArgumentError.new("URL #{page_url} is not a known page URL")
  end
end

#eachObject

Iterate over all buttons. A NavButtonDef object is passed to the block that contains the icon and URL for the button. If no URL is set, the button is inactive.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/postrunner/PagingButtons.rb', line 52

def each
  %w( first back forward last ).each do |button_name|
    button = NavButtonDef.new
    button.icon = button_name + '.png'
    button.url =
      case button_name
      when 'first'
        @current_page_index == 0 || !@end_buttons ? nil : @pages.first
      when 'back'
        @current_page_index == 0 ? nil :
          @pages[@current_page_index - 1]
      when 'forward'
        @current_page_index == @pages.length - 1 ? nil :
          @pages[@current_page_index + 1]
      when 'last'
        @current_page_index == @pages.length - 1 ||
          !@end_buttons ? nil : @pages.last
      end

    yield(button)
  end
end