Class: PostRunner::ViewButtons

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

Overview

This class generates a simple icon menue to select from a set of HTML pages (called views). The current page is represented as an inactive icon. All other icons are buttons that contain links to other pages.

Instance Method Summary collapse

Constructor Details

#initialize(views) ⇒ ViewButtons

Create a ViewButtons object.

Parameters:

  • views (Array of NavButtonDef)

    icons and URLs for all pages.



22
23
24
25
26
27
28
# File 'lib/postrunner/ViewButtons.rb', line 22

def initialize(views)
  if views.empty?
    raise ArgumentError.new("'views' must not be empty")
  end
  @views = views
  self.current_page = views[0].url
end

Instance Method Details

#current_pageString

Get the URL of the current page

Returns:

  • (String)


32
33
34
# File 'lib/postrunner/ViewButtons.rb', line 32

def current_page
  @current_view_url
end

#current_page=(page_url) ⇒ Object

Set the the current page. nil or a URL in the predefined set.

Parameters:

  • page_url (String)

    URL of the current page. This must either be



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/postrunner/ViewButtons.rb', line 39

def current_page=(page_url)
  unless page_url
    @current_view_url = nil
    return
  end

  if (current = @views.find { |v| v.url == page_url })
    @current_view_url = current.url
  else
    raise ArgumentError.new("#{page_url} is not a URL of a known view")
  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.



55
56
57
58
59
60
61
62
63
64
# File 'lib/postrunner/ViewButtons.rb', line 55

def each
  @views.each do |view|
    view = view.clone
    if @current_view_url == view.url
      view.url = nil
    end
    yield(view)
  end

end