Class: Navigator::Menu
- Inherits:
-
Object
- Object
- Navigator::Menu
- Defined in:
- lib/navigator/menu.rb
Overview
Renders a HTML menu.
Class Method Summary collapse
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, *args, &block) ⇒ Object
rubocop:disable Style/MethodMissing.
- #render ⇒ Object
- #respond_to?(name) ⇒ Boolean
Constructor Details
#initialize(template, tag: "ul", attributes: {}, activator: Navigator::TagActivator.new, &block) ⇒ Menu
Returns a new instance of Menu.
10 11 12 13 14 15 16 |
# File 'lib/navigator/menu.rb', line 10 def initialize template, tag: "ul", attributes: {}, activator: Navigator::TagActivator.new, &block @template = template @tag = Navigator::Tag.new tag, attributes: attributes, activator: activator @menu_activator = activator @items = [] instance_eval(&block) if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
rubocop:disable Style/MethodMissing
53 54 55 56 57 58 59 |
# File 'lib/navigator/menu.rb', line 53 def method_missing name, *args, &block if method_allowed?(name.to_s) add(name, *args, &block) else template.public_send name, *args end end |
Class Method Details
.allowed_methods ⇒ Object
6 7 8 |
# File 'lib/navigator/menu.rb', line 6 def self.allowed_methods /^(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)$/ end |
Instance Method Details
#add(name, content = nil, attributes: {}, activator: menu_activator, &block) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/navigator/menu.rb', line 18 def add name, content = nil, attributes: {}, activator: , &block tag = Navigator::Tag.new name, content, attributes: attributes, activator: activator return items << tag.render unless block_given? items << tag.prefix items << tag.content instance_eval(&block) items << tag.suffix end |
#image(url, alt = nil, attributes: {}, activator: menu_activator) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/navigator/menu.rb', line 32 def image url, alt = nil, attributes: {}, activator: modified_attributes = attributes.merge src: url, alt: alt modified_attributes = modified_attributes.delete_if { |_, value| !value.present? } add "img", attributes: modified_attributes, activator: activator end |
#item(content = nil, url, item_attributes: {}, link_attributes: {}, activator: menu_activator, &block) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/navigator/menu.rb', line 39 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: 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
28 29 30 |
# File 'lib/navigator/menu.rb', line 28 def link content = nil, url, attributes: {}, activator: , &block add "a", content, attributes: attributes.merge(href: url), activator: activator, &block end |
#render ⇒ Object
61 62 63 |
# File 'lib/navigator/menu.rb', line 61 def render [tag.prefix, tag.content, items.compact.join(""), tag.suffix].compact * "" end |
#respond_to?(name) ⇒ Boolean
48 49 50 |
# File 'lib/navigator/menu.rb', line 48 def respond_to? name method_allowed?(name) || super(name) end |