Class: SiteMap::ViewNode

Inherits:
Object
  • Object
show all
Includes:
Helpers::Mapping
Defined in:
lib/site_map/view_node.rb

Constant Summary collapse

ATTRIBUTES =
[ :map, :index, :label, :url, :visible, :resource, :parent, :node_type ]
TYPES =
[ :view, :group, :member, :collection ]
BASE_LABEL_TEMPLATE =
{
  :collection => ":action :resource",
  :member => ":action ::resource_name"
}
LABEL_ACTION_TEMPLATES =
{
  :index => BASE_LABEL_TEMPLATE[:collection].gsub(':action','').strip,
  :show => BASE_LABEL_TEMPLATE[:member].gsub(':action','').strip
}
BASE_URL_TEMPLATE =
{
  :collection => [':resource', 'path'],
  :member => [':resource', 'path(@:resource)']
}
URL_ACTION_TEMPLATES =
{
  :index => BASE_URL_TEMPLATE[:collection],
  :show => BASE_URL_TEMPLATE[:member]
}

Constants included from Helpers::Mapping

Helpers::Mapping::DEFAULT_ALIAS

Instance Method Summary collapse

Methods included from Helpers::Mapping

#alias, #collection, #collection_view, #group, #member, #member_view, #view

Constructor Details

#initialize(index, map, node_type, options = {}) ⇒ ViewNode

Returns a new instance of ViewNode.

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
# File 'lib/site_map/view_node.rb', line 27

def initialize(index, map, node_type, options={})
  raise(ArgumentError, "index, map and node_type arguements required") unless index && map && node_type
  @index = index
  @map = map
  @node_type = node_type
  options.each do |method, value|
    instance_variable_set("@#{method}", value)
  end
  @view_nodes = []
end

Instance Method Details

#[](attribute_sym) ⇒ Object



38
39
40
# File 'lib/site_map/view_node.rb', line 38

def [](attribute_sym)
  instance_variable_get("@#{attribute_sym}")
end

#add_to_children(view_node) ⇒ Object



65
66
67
# File 'lib/site_map/view_node.rb', line 65

def add_to_children(view_node)
  @view_nodes.push(view_node)
end

#aliasesObject



59
60
61
62
63
# File 'lib/site_map/view_node.rb', line 59

def aliases
  @aliases ||= self.map.index_of_nodes.collect do |key, view_node|
    key if view_node.index == self.index && key != self.index
  end.compact
end

#ancestorsObject



73
74
75
76
77
78
79
80
# File 'lib/site_map/view_node.rb', line 73

def ancestors
  unless @ancestors
    node, @ancestors = self, []
    @ancestors << node = node.parent while node.parent
    @ancestors.reverse!
  end
  @ancestors
end

#childrenObject Also known as: views



69
70
71
# File 'lib/site_map/view_node.rb', line 69

def children
  @view_nodes
end

#inspectObject



107
108
109
110
111
112
# File 'lib/site_map/view_node.rb', line 107

def inspect
  attributes_string = [:index, :node_type, :label, :url, :visible].collect do |attribute|
    "#{attribute}: #{self.send(attribute).inspect}"
  end.join(", ")
  "#<#{self.class} #{attributes_string}>"
end

#labelObject



42
43
44
# File 'lib/site_map/view_node.rb', line 42

def label
  @label ? @label : self.default_label
end

#next_siblingObject Also known as: next_view, next



97
98
99
# File 'lib/site_map/view_node.rb', line 97

def next_sibling
  sns[(idx = sns.index(self)+1) == sns.length ? 0 : idx]
end

#previous_siblingObject Also known as: previous_view, prev



92
93
94
# File 'lib/site_map/view_node.rb', line 92

def previous_sibling
  sns[sns.index(self)-1]
end

#self_and_ancestorsObject Also known as: sna



81
82
83
# File 'lib/site_map/view_node.rb', line 81

def self_and_ancestors
  @with_ancestors ||= self.ancestors.dup.push(self)
end

#self_and_siblingsObject Also known as: sns



88
89
90
# File 'lib/site_map/view_node.rb', line 88

def self_and_siblings
  @with_siblings ||= self.parent.children
end

#siblingsObject



85
86
87
# File 'lib/site_map/view_node.rb', line 85

def siblings
  @siblings ||= (self.self_and_siblings - [self])
end

#urlObject



45
46
47
# File 'lib/site_map/view_node.rb', line 45

def url
  @url ? @url : self.default_url
end

#visibleObject Also known as: visible?



48
49
50
51
52
53
54
55
56
# File 'lib/site_map/view_node.rb', line 48

def visible
  if @visible.nil?
    true
  elsif @visible.kind_of?(::String)
    @visible
  else
    !!@visible
  end
end