class Lolita::Navigation::Tree

Attributes

branches[R]
default_position[R]
name[R]
parent[R]
root[R]

Public Class Methods

[](name) click to toggle source
# File lib/lolita/navigation/tree.rb, line 13
def [](name)
  @@trees||={}
  @@trees[name]
end
new(name) click to toggle source
# File lib/lolita/navigation/tree.rb, line 23
def initialize(name)
  @name=name
  @default_possition=:append
  @branches=[]
end
remember(tree) click to toggle source
# File lib/lolita/navigation/tree.rb, line 8
def remember(tree)
  @@trees||={}
  @@trees[tree.name.to_sym]=tree
end

Public Instance Methods

after(given_branch,*other_branch) click to toggle source
# File lib/lolita/navigation/tree.rb, line 77
def after(given_branch,*other_branch) 
  index=get_branch_index(given_branch)

  adding_branch(*other_branch) do |fixed_branch|
    put_in_branches(fixed_branch,index)
  end
end
append(*branch) click to toggle source
# File lib/lolita/navigation/tree.rb, line 65
def append(*branch)
  adding_branch(*branch) do |fixed_branch|
    @branches<<fixed_branch
  end
end
before(given_branch,*other_branch) click to toggle source
# File lib/lolita/navigation/tree.rb, line 85
def before(given_branch,*other_branch)
  index=get_branch_index(given_branch)

  adding_branch(*other_branch) do |fixed_branch|
    put_in_branches(fixed_branch,index-1)
  end
end
each() { |branch| ... } click to toggle source
# File lib/lolita/navigation/tree.rb, line 29
def each 
  @branches.each do |branch|
    yield branch
    if branch.children.any?
      branch.children.each do |child|
        yield child
      end
    end
  end
end
get_branch_index(given_branch) click to toggle source
# File lib/lolita/navigation/tree.rb, line 93
def get_branch_index(given_branch)
  @branches.each_with_index{|branch,index|
    return index if given_branch==branch
  }
  raise ArgumentError, "Branch #{given_branch.inspect} not exists in #{self.inspect}"
end
method_missing(method_name, *args) click to toggle source
# File lib/lolita/navigation/tree.rb, line 60
def method_missing method_name, *args
  @branches.send(method_name.to_sym,*args)
end
populate_urls_in_branches(view) click to toggle source
# File lib/lolita/navigation/tree.rb, line 54
def populate_urls_in_branches(view)
  self.each do |branch|
    branch.populate_url(view)
  end
end
prepend(*branch) click to toggle source
# File lib/lolita/navigation/tree.rb, line 71
def prepend(*branch)
  adding_branch(*branch) do |fixed_branch|
    @branches.unshift(fixed_branch)
  end
end
root?() click to toggle source
# File lib/lolita/navigation/tree.rb, line 40
def root?
  !parent
end
set_parent(new_parent) click to toggle source
# File lib/lolita/navigation/tree.rb, line 100
def set_parent(new_parent)
  @parent=new_parent
end
visible?(view) click to toggle source
# File lib/lolita/navigation/tree.rb, line 44
def visible?(view)
  self.branches.inject([]){|result,branch| 
    if branch.visible?(view)
      result << true
    else
      result
    end
  }.any?
end

Private Instance Methods

adding_branch(*branch) { |fixed_branch| ... } click to toggle source
# File lib/lolita/navigation/tree.rb, line 106
def adding_branch *branch
  self.run(:before_branch_added,*branch)
  fixed_branch=fix_branch(*branch)
  yield fixed_branch
  @last_branch=fixed_branch
  self.run(:after_branch_added,fixed_branch)
  fixed_branch
end
fix_branch(*branch) click to toggle source
# File lib/lolita/navigation/tree.rb, line 115
def fix_branch(*branch)
  unless branch[0].is_a?(Lolita::Navigation::Branch)
    options=branch.extract_options!
    Lolita::Navigation::Branch.get_or_create(*branch,options.merge(:tree=>self))
  else
    branch[0].tree=self
    branch[0]
  end
end
put_in_branches(branch,index) click to toggle source
# File lib/lolita/navigation/tree.rb, line 125
def put_in_branches branch,index
  before_part=@branches.slice(0,index+1) || []
  after_part=@branches.slice(index+1,@branches.size-index) || []
  @branches=before_part+[branch]+after_part
end