Class: PostRunner::PagingButtons
- Inherits:
-
Object
- Object
- PostRunner::PagingButtons
- 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
-
#current_page ⇒ Object
Return the URL of the current page.
-
#current_page=(page_url) ⇒ Object
Set the URL for the current page.
-
#each ⇒ Object
Iterate over all buttons.
-
#initialize(page_urls, end_buttons = true) ⇒ PagingButtons
constructor
Create a new PagingButtons object.
Constructor Details
#initialize(page_urls, end_buttons = true) ⇒ PagingButtons
Create a new PagingButtons object.
25 26 27 28 29 30 31 32 |
# File 'lib/postrunner/PagingButtons.rb', line 25 def initialize(page_urls, = 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 |
Instance Method Details
#current_page ⇒ Object
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.
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 |
#each ⇒ Object
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 || = NavButtonDef.new .icon = + '.png' .url = case 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() end end |