Class: Menu
- Inherits:
-
Object
- Object
- Menu
- Defined in:
- lib/emenu/menu.rb
Constant Summary collapse
- IDENTATION =
2
- SPACE =
" "
- DEFAULT_SPACE =
SPACE*IDENTATION
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #add(item_name, path = "#", &block) ⇒ Object
- #all_items ⇒ Object
- #collapsible_menu_haml ⇒ Object
- #humanized_title ⇒ Object
- #indent ⇒ Object
-
#initialize(title, parent, config, path = "#") ⇒ Menu
constructor
A new instance of Menu.
- #item(name, path = "#", &block) ⇒ Object
- #menu_haml ⇒ Object
- #names ⇒ Object
- #opened? ⇒ Boolean
- #parents ⇒ Object
- #selected? ⇒ Boolean
- #spacing ⇒ Object
- #spacing_subitem ⇒ Object
- #title_haml ⇒ Object
- #to_haml ⇒ Object
Constructor Details
#initialize(title, parent, config, path = "#") ⇒ Menu
Returns a new instance of Menu.
9 10 11 12 13 14 15 16 |
# File 'lib/emenu/menu.rb', line 9 def initialize(title, parent, config, path="#") @title = title @parent = parent @config = config @path = path @items = [] @type = @parent.nil? ? :title : :menu end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
7 8 9 |
# File 'lib/emenu/menu.rb', line 7 def items @items end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/emenu/menu.rb', line 7 def path @path end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
7 8 9 |
# File 'lib/emenu/menu.rb', line 7 def title @title end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/emenu/menu.rb', line 7 def type @type end |
Instance Method Details
#add(item_name, path = "#", &block) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/emenu/menu.rb', line 23 def add(item_name, path="#", &block) items << Menu.new(item_name, self, @config, path) @type = :collapsible_menu if @type == :menu # when we add subitems to menu it becomes collapsible menu instance_eval(&block) unless block.nil? MenuConfig.enforce_uniqueness names return items.last end |
#all_items ⇒ Object
80 81 82 |
# File 'lib/emenu/menu.rb', line 80 def all_items items.inject([self]) { |result, | result << .all_items } end |
#collapsible_menu_haml ⇒ Object
50 51 52 53 54 |
# File 'lib/emenu/menu.rb', line 50 def "#{spacing}%li.collapsible\n" << "#{spacing_subitem}%a{ :href => '#', :class => 'collapsible #{opened? ? "minus" : "plus"}'} #{humanized_title}\n" << "#{spacing_subitem}%ul##{@title}.#{opened? ? "expanded" : "collapsed"}\n" end |
#humanized_title ⇒ Object
56 57 58 |
# File 'lib/emenu/menu.rb', line 56 def humanized_title @title.to_s.humanize end |
#indent ⇒ Object
68 69 70 |
# File 'lib/emenu/menu.rb', line 68 def indent result = @parent.nil? ? 0 : @parent.indent + IDENTATION * (@parent.type == :collapsible_menu ? 2 : 1) end |
#item(name, path = "#", &block) ⇒ Object
18 19 20 21 |
# File 'lib/emenu/menu.rb', line 18 def item(name, path="#", &block) current = self.add(name,path) current.instance_eval(&block) unless block.nil? end |
#menu_haml ⇒ Object
46 47 48 |
# File 'lib/emenu/menu.rb', line 46 def "#{spacing}%li#{selected? ? '{ :class => "selected" }' : ""}= link_to '#{humanized_title}', '#{@path}'\n" end |
#names ⇒ Object
31 32 33 |
# File 'lib/emenu/menu.rb', line 31 def names items.inject([@title.to_s]) { |result, item| result << item.names } end |
#opened? ⇒ Boolean
64 65 66 |
# File 'lib/emenu/menu.rb', line 64 def opened? @config.opened? @title end |
#parents ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/emenu/menu.rb', line 84 def parents result = [] unless @parent.nil? result << @parent result << @parent.parents unless @parent.parents == [] end return result.flatten end |
#selected? ⇒ Boolean
60 61 62 |
# File 'lib/emenu/menu.rb', line 60 def selected? @config.selected == @title end |
#spacing_subitem ⇒ Object
76 77 78 |
# File 'lib/emenu/menu.rb', line 76 def spacing_subitem spacing + DEFAULT_SPACE end |
#title_haml ⇒ Object
39 40 41 42 43 44 |
# File 'lib/emenu/menu.rb', line 39 def title_haml "#{spacing}%h6#h-menu-#{@title}#{opened? ? '{ :class => "selected" }' : ""}\n" << "#{spacing_subitem}%a{ :href => '##{@title}'}\n" << "#{spacing_subitem*2}%span #{humanized_title}\n" << "%ul#menu-#{@title}.#{opened? ? "opened" : "closed"}\n" end |
#to_haml ⇒ Object
35 36 37 |
# File 'lib/emenu/menu.rb', line 35 def to_haml items.inject(eval "#{type}_haml") { |result, item| result << item.to_haml } end |