Class: Mariner::UnorderedListRenderer
- Inherits:
-
Object
- Object
- Mariner::UnorderedListRenderer
- Defined in:
- lib/mariner/unordered_list_renderer.rb
Overview
Public: The default renderer used. Renders the nav config to an unordered list.
Examples:
Mariner.configure do
a_group do
root_path 'A link'
end
end
Mariner.configuration.render #=> returns:
<ul class=''>
<li>
<a class="root_path" href="/">A Link</a>
</li>
</ul>
If you want to implement your own rendering strategy, read on for a good starting point.
class FakeRenderingStrategy
# Called from Store and Url instances. Must return something
# that responds to #render
#
def factory(type, subject)
# type - Can be `:group` or `:item`
# subject - The Store or Url instance to render
case type
when :group then FakeRenderingStrategy::GroupRenderer.new(subject, self)
when :item then FakeRenderingStrategy::ItemRenderer.new(subject, self)
end
end
class GroupRenderer < RenderingStrategy::Base
# A group renderer must iterate its #configurations and call
# #render on each, then return the joined result.
#
def render
subject.configurations.map do |config|
_, entity = config
entity.render(rendering_strategy) # entity can be a Store or a Url
end.join
end
end
class ItemRenderer < RenderingStrategy::Base
# An item renderer just needs to return some string representation
# of the Url it's rendering.
#
def render
# render something based on #subject (e.g. markup for a link)
end
end
end
Defined Under Namespace
Classes: GroupRenderer, ItemRenderer
Instance Attribute Summary collapse
-
#group_classname ⇒ Object
Public: The classname that will be added to the rendered UL elements.
-
#item_classname ⇒ Object
Public: The classname that will be added to the rendered links (A elements).
-
#render_titles ⇒ Object
(also: #render_titles?)
Public: If true, will render a title list element based on the name of the group.
-
#title_classname ⇒ Object
Public: The classname that will be added to the rendered title LI elements.
Instance Method Summary collapse
-
#factory(type, subject) ⇒ Object
Public: Called by Store and Url instances to get a renderer.
-
#initialize(options = {}) ⇒ UnorderedListRenderer
constructor
Public: Initialize a new renderer with options.
Constructor Details
#initialize(options = {}) ⇒ UnorderedListRenderer
Public: Initialize a new renderer with options.
options - The hash of options used to determine how to render:
:render_titles - Turn title rendering for Stores on or off (optional, default: false)
100 101 102 |
# File 'lib/mariner/unordered_list_renderer.rb', line 100 def initialize(={}) defaults.merge().each { |k,v| send("#{k}=", v) } end |
Instance Attribute Details
#group_classname ⇒ Object
Public: The classname that will be added to the rendered UL elements.
83 84 85 |
# File 'lib/mariner/unordered_list_renderer.rb', line 83 def group_classname @group_classname end |
#item_classname ⇒ Object
Public: The classname that will be added to the rendered links (A elements)
93 94 95 |
# File 'lib/mariner/unordered_list_renderer.rb', line 93 def item_classname @item_classname end |
#render_titles ⇒ Object Also known as: render_titles?
Public: If true, will render a title list element based on the name of the group.
77 78 79 |
# File 'lib/mariner/unordered_list_renderer.rb', line 77 def render_titles @render_titles end |
#title_classname ⇒ Object
Public: The classname that will be added to the rendered title LI elements
88 89 90 |
# File 'lib/mariner/unordered_list_renderer.rb', line 88 def title_classname @title_classname end |
Instance Method Details
#factory(type, subject) ⇒ Object
Public: Called by Store and Url instances to get a renderer.
Examples:
s = UnorderedListRenderer.new
s.factory(:group, self) #=> instance of UnorderedListRenderer::GroupRenderer
s.factory(:item, self) #=> instance of UnorderedListRenderer::ItemRenderer
112 113 114 115 116 117 |
# File 'lib/mariner/unordered_list_renderer.rb', line 112 def factory(type, subject) case type when :group then GroupRenderer.new(subject, self) when :item then ItemRenderer.new(subject, self) end end |