class PostRunner::PagingButtons
A class to generate a set of forward/backward buttons for an HTML page. It can also include jump to first/last buttons.
Public Class Methods
new(page_urls, end_buttons = true)
click to toggle source
Create a new PagingButtons
object. @param page_urls [Array of String] Sorted list of all possible pages @param end_buttons [Boolean] If true jump to first/last buttons are
included
# 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
Public Instance Methods
current_page()
click to toggle source
Return the URL of the current page
# File lib/postrunner/PagingButtons.rb, line 35 def current_page @pages[@current_page_index] end
current_page=(page_url)
click to toggle source
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. @param page_url [String] URL of the page
# 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
each() { |button| ... }
click to toggle source
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.
# 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