Class: Amber::Menu

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent = nil) ⇒ 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

#childrenObject

Returns the value of attribute children.



8
9
10
# File 'lib/amber/menu.rb', line 8

def children
  @children
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/amber/menu.rb', line 9

def name
  @name
end

#parentObject

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(menu_file_path)
  File.open(menu_file_path) do |file|
    parse_menu(file)
  end
end

#pathObject

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_strObject



54
55
56
# File 'lib/amber/menu.rb', line 54

def path_str
  @path_str ||= path.join('/')
end

#sizeObject



62
63
64
# File 'lib/amber/menu.rb', line 62

def size
  children.size
end

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 submenu(item_name=nil)
  if item_name
    self.children.detect {|child| child.name == item_name}
  else
    self.children
  end
end