class Amber::Menu
Attributes
children[RW]
name[RW]
parent[RW]
Public Class Methods
new(name, parent=nil)
click to toggle source
# File lib/amber/menu.rb, line 20 def initialize(name, parent=nil) self.name = name self.parent = parent self.children = [] end
Public Instance Methods
each(&block)
click to toggle source
# File lib/amber/menu.rb, line 58 def each(&block) children.each(&block) end
inspect(indent=0)
click to toggle source
# 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)
click to toggle source
returns true if this menu item is the terminus menu item for path. (meaning that there are no children that match more path segments)
# 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)
click to toggle source
load the menu.txt file and build the in-memory menu array
# File lib/amber/menu.rb, line 14 def load(menu_file_path) File.open(menu_file_path) do |file| parse_menu(file) end end
path()
click to toggle source
returns path from root to this leaf as an array
# 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)
click to toggle source
# 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)
click to toggle source
returns true if menu's path starts with path_prefix
# File lib/amber/menu.rb, line 69 def path_starts_with?(path_prefix) array_starts_with?(path, path_prefix) end
path_str()
click to toggle source
# File lib/amber/menu.rb, line 54 def path_str @path_str ||= path.join('/') end
size()
click to toggle source
# File lib/amber/menu.rb, line 62 def size children.size end
Protected Instance Methods
add_child(name)
click to toggle source
private & protected methods
# File lib/amber/menu.rb, line 103 def add_child(name) self.children << Menu.new(name, self) end
Private Instance Methods
array_starts_with?(big_array, small_array)
click to toggle source
# File lib/amber/menu.rb, line 109 def array_starts_with?(big_array, small_array) small_array.length.times do |i| if small_array[i] != big_array[i] return false end end return true end