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
submenu(item_name=nil) click to toggle source

returns the menu under the item that matches item_name.

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
last_menu_at_depth(depth) click to toggle source

returns the last list of children at the specified depth

# File lib/amber/menu.rb, line 134
def last_menu_at_depth(depth)
  menu = self
  depth.times { menu = menu.children.last }
  menu
end
parse_menu(file) click to toggle source
# File lib/amber/menu.rb, line 119
def parse_menu(file)
  while true
    item = file.readline
    if item.strip.chars.any? && item !~ /^\s*#/
      depth = item.scan("  ").size
      last_menu_at_depth(depth).add_child(item.strip)
    end
  end
rescue EOFError
  # done loading
end