Class: Navigator::Menu
- Inherits:
-
Object
- Object
- Navigator::Menu
- Defined in:
- lib/navigator/menu.rb
Overview
Renders a HTML menu.
Constant Summary collapse
- ALLOWED_ELEMENTS =
/ ^( div| section| header| h[1-6]| nav| ul| li| a| img| b| em| s| small| span| strong| sub| sup| form| label| select| option| input| button )$ /x
Instance Method Summary collapse
- #add(name, content = nil, attributes: {}, activator: menu_activator, &block) ⇒ Object
- #image(url, alt = nil, attributes: {}, activator: menu_activator) ⇒ Object
-
#initialize(template, tag: "ul", attributes: {}, activator: Navigator::TagActivator.new, &block) ⇒ Menu
constructor
A new instance of Menu.
- #item(content = nil, url, item_attributes: {}, link_attributes: {}, activator: menu_activator, &block) ⇒ Object
- #link(content = nil, url, attributes: {}, activator: menu_activator, &block) ⇒ Object
- #method_missing(name, *positionals, **keywords) ⇒ Object
- #render ⇒ Object
Constructor Details
#initialize(template, tag: "ul", attributes: {}, activator: Navigator::TagActivator.new, &block) ⇒ Menu
Returns a new instance of Menu.
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/navigator/menu.rb', line 34 def initialize template, tag: "ul", attributes: {}, activator: Navigator::TagActivator.new, &block @template = template @tag = Navigator::Tag.new(tag, attributes:, activator:) @menu_activator = activator @items = [] instance_eval(&block) if block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *positionals, **keywords) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/navigator/menu.rb', line 85 def method_missing(name, *positionals, **keywords, &) if respond_to_missing? name add(name, *positionals, **keywords, &) else template.public_send(name, *positionals, **keywords) || super end end |
Instance Method Details
#add(name, content = nil, attributes: {}, activator: menu_activator, &block) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/navigator/menu.rb', line 46 def add name, content = nil, attributes: {}, activator: , &block tag = Navigator::Tag.new(name, content, attributes:, activator:) return items << tag.render unless block items << tag.prefix items << tag.content instance_eval(&block) items << tag.suffix end |
#image(url, alt = nil, attributes: {}, activator: menu_activator) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/navigator/menu.rb', line 60 def image url, alt = nil, attributes: {}, activator: modified_attributes = attributes.merge(src: url, alt:) modified_attributes = modified_attributes.delete_if { |_, value| !value.present? } add "img", attributes: modified_attributes, activator: end |
#item(content = nil, url, item_attributes: {}, link_attributes: {}, activator: menu_activator, &block) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/navigator/menu.rb', line 67 def item content = nil, url, item_attributes: {}, link_attributes: {}, activator: , &block modified_item_attributes = item_attributes.clone activate_item_attributes! modified_item_attributes, url, activator add "li", attributes: modified_item_attributes, activator: do link content, url, attributes: link_attributes, activator: Navigator::TagActivator.new, &block end end |
#link(content = nil, url, attributes: {}, activator: menu_activator, &block) ⇒ Object
56 57 58 |
# File 'lib/navigator/menu.rb', line 56 def link content = nil, url, attributes: {}, activator: , &block add "a", content, attributes: attributes.merge(href: url), activator:, &block end |
#render ⇒ Object
93 |
# File 'lib/navigator/menu.rb', line 93 def render = [tag.prefix, tag.content, items.compact.join, tag.suffix].compact.join |