Class: Menu

Inherits:
Object
  • Object
show all
Defined in:
lib/emenu/menu.rb

Constant Summary collapse

IDENTATION =
2
SPACE =
" "
DEFAULT_SPACE =
SPACE*IDENTATION

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#itemsObject (readonly)

Returns the value of attribute items.



7
8
9
# File 'lib/emenu/menu.rb', line 7

def items
  @items
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/emenu/menu.rb', line 7

def path
  @path
end

#titleObject (readonly)

Returns the value of attribute title.



7
8
9
# File 'lib/emenu/menu.rb', line 7

def title
  @title
end

#typeObject (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_itemsObject



80
81
82
# File 'lib/emenu/menu.rb', line 80

def all_items
  items.inject([self]) { |result, menu| result << menu.all_items }
end

#collapsible_menu_hamlObject



50
51
52
53
54
# File 'lib/emenu/menu.rb', line 50

def collapsible_menu_haml
  "#{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_titleObject



56
57
58
# File 'lib/emenu/menu.rb', line 56

def humanized_title
  @title.to_s.humanize
end

#indentObject



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


46
47
48
# File 'lib/emenu/menu.rb', line 46

def menu_haml
  "#{spacing}%li#{selected? ? '{ :class => "selected" }' : ""}= link_to '#{humanized_title}', '#{@path}'\n"
end

#namesObject



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

Returns:

  • (Boolean)


64
65
66
# File 'lib/emenu/menu.rb', line 64

def opened?
  @config.opened? @title
end

#parentsObject



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

Returns:

  • (Boolean)


60
61
62
# File 'lib/emenu/menu.rb', line 60

def selected?
  @config.selected == @title
end

#spacingObject



72
73
74
# File 'lib/emenu/menu.rb', line 72

def spacing
  SPACE*indent
end

#spacing_subitemObject



76
77
78
# File 'lib/emenu/menu.rb', line 76

def spacing_subitem
  spacing + DEFAULT_SPACE
end

#title_hamlObject



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_hamlObject



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