Class: Amber::Menu
- Inherits:
-
Object
- Object
- Amber::Menu
- Defined in:
- lib/amber/menu.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
- #each(&block) ⇒ Object
-
#initialize(name, parent = nil) ⇒ Menu
constructor
A new instance of Menu.
- #inspect(indent = 0) ⇒ Object
-
#leaf_for_path?(path) ⇒ Boolean
returns true if this menu item is the terminus menu item for path.
-
#load(menu_file_path) ⇒ Object
load the menu.txt file and build the in-memory menu array.
-
#path ⇒ Object
returns path from root to this leaf as an array.
- #path_prefix_of?(full_path) ⇒ Boolean
-
#path_starts_with?(path_prefix) ⇒ Boolean
returns true if menu’s path starts with
path_prefix
. - #path_str ⇒ Object
- #size ⇒ Object
-
#submenu(item_name = nil) ⇒ Object
returns the menu under the item that matches item_name.
Constructor Details
#initialize(name, parent = nil) ⇒ Menu
Returns a new instance of Menu.
20 21 22 23 24 |
# File 'lib/amber/menu.rb', line 20 def initialize(name, parent=nil) self.name = name self.parent = parent self.children = [] end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
8 9 10 |
# File 'lib/amber/menu.rb', line 8 def children @children end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/amber/menu.rb', line 9 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
7 8 9 |
# File 'lib/amber/menu.rb', line 7 def parent @parent end |
Instance Method Details
#each(&block) ⇒ Object
58 59 60 |
# File 'lib/amber/menu.rb', line 58 def each(&block) children.each(&block) end |
#inspect(indent = 0) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/amber/menu.rb', line 88 def inspect(indent=0) lines = [] lines << ' '*indent + '- ' + self.name self.children.each do |child| lines << child.inspect(indent+1) end lines.join("\n") end |
#leaf_for_path?(path) ⇒ Boolean
returns true if this menu item is the terminus menu item for path. (meaning that there are no children that match more path segments)
81 82 83 84 85 86 |
# File 'lib/amber/menu.rb', line 81 def leaf_for_path?(path) return false unless path_prefix_of?(path) next_path_segment = (path - self.path).first return false if next_path_segment.nil? return !children.detect {|i| i.name == next_path_segment} end |
#load(menu_file_path) ⇒ Object
load the menu.txt file and build the in-memory menu array
14 15 16 17 18 |
# File 'lib/amber/menu.rb', line 14 def load() File.open() do |file| (file) end end |
#path ⇒ Object
returns path from root to this leaf as an array
44 45 46 47 48 49 50 51 52 |
# File 'lib/amber/menu.rb', line 44 def path @path ||= begin if parent == nil [] else parent.path + [name] end end end |
#path_prefix_of?(full_path) ⇒ Boolean
73 74 75 |
# File 'lib/amber/menu.rb', line 73 def path_prefix_of?(full_path) array_starts_with?(full_path, path) end |
#path_starts_with?(path_prefix) ⇒ Boolean
returns true if menu’s path starts with path_prefix
69 70 71 |
# File 'lib/amber/menu.rb', line 69 def path_starts_with?(path_prefix) array_starts_with?(path, path_prefix) end |
#path_str ⇒ Object
54 55 56 |
# File 'lib/amber/menu.rb', line 54 def path_str @path_str ||= path.join('/') end |
#size ⇒ Object
62 63 64 |
# File 'lib/amber/menu.rb', line 62 def size children.size end |
#submenu(item_name = nil) ⇒ Object
returns the menu under the item that matches item_name.
33 34 35 36 37 38 39 |
# File 'lib/amber/menu.rb', line 33 def (item_name=nil) if item_name self.children.detect {|child| child.name == item_name} else self.children end end |