Class: Lolita::Navigation::Tree

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Hooks
Defined in:
lib/lolita/navigation/tree.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hooks

included, method_missing

Constructor Details

#initialize(name) ⇒ Tree

Returns a new instance of Tree.



23
24
25
26
27
# File 'lib/lolita/navigation/tree.rb', line 23

def initialize(name)
  @name=name
  @default_possition=:append
  @branches=[]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



60
61
62
# File 'lib/lolita/navigation/tree.rb', line 60

def method_missing method_name, *args
  @branches.send(method_name.to_sym,*args)
end

Instance Attribute Details

#branchesObject (readonly)

Returns the value of attribute branches.



21
22
23
# File 'lib/lolita/navigation/tree.rb', line 21

def branches
  @branches
end

#default_positionObject (readonly)

Returns the value of attribute default_position.



21
22
23
# File 'lib/lolita/navigation/tree.rb', line 21

def default_position
  @default_position
end

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/lolita/navigation/tree.rb', line 21

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



21
22
23
# File 'lib/lolita/navigation/tree.rb', line 21

def parent
  @parent
end

#rootObject (readonly)

Returns the value of attribute root.



21
22
23
# File 'lib/lolita/navigation/tree.rb', line 21

def root
  @root
end

Class Method Details

.[](name) ⇒ Object



13
14
15
16
# File 'lib/lolita/navigation/tree.rb', line 13

def [](name)
  @@trees||={}
  @@trees[name]
end

.remember(tree) ⇒ Object



8
9
10
11
# File 'lib/lolita/navigation/tree.rb', line 8

def remember(tree)
  @@trees||={}
  @@trees[tree.name.to_sym]=tree
end

Instance Method Details

#after(given_branch, *other_branch) ⇒ Object



77
78
79
80
81
82
83
# 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) ⇒ Object



65
66
67
68
69
# 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) ⇒ Object



85
86
87
88
89
90
91
# 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

#eachObject



29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object

Raises:

  • (ArgumentError)


93
94
95
96
97
98
# 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

#populate_urls_in_branches(view) ⇒ Object



54
55
56
57
58
# 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) ⇒ Object



71
72
73
74
75
# File 'lib/lolita/navigation/tree.rb', line 71

def prepend(*branch)
  adding_branch(*branch) do |fixed_branch|
    @branches.unshift(fixed_branch)
  end
end

#root?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/lolita/navigation/tree.rb', line 40

def root?
  !parent
end

#set_parent(new_parent) ⇒ Object



100
101
102
# File 'lib/lolita/navigation/tree.rb', line 100

def set_parent(new_parent)
  @parent=new_parent
end

#visible?(view) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
# 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