Class: N::SiteMap

Inherits:
SafeHash show all
Defined in:
lib/n/sitemap.rb

Overview

SiteMap

TODO:

Use a second class in the UI namespace for rendering related stuff?

Instance Attribute Summary collapse

Attributes inherited from SafeHash

#sync

Instance Method Summary collapse

Methods inherited from SafeHash

#[], #[]=, #clear, #delete, #keys, #size, #values

Constructor Details

#initialize(separator = " > ") ⇒ SiteMap

Returns a new instance of SiteMap.



93
94
95
96
# File 'lib/n/sitemap.rb', line 93

def initialize(separator = " > ")
	super
	@separator = separator
end

Instance Attribute Details

#rootObject

The root page for this sitemap



88
89
90
# File 'lib/n/sitemap.rb', line 88

def root
  @root
end

#separatorObject

The separator used when creating paths



91
92
93
# File 'lib/n/sitemap.rb', line 91

def separator
  @separator
end

Instance Method Details

#<<(page) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/n/sitemap.rb', line 98

def << (page)
	self[page.uri] = page
	unless page.parent
		@root = page
		page.realm = :root
	end
end

#linked_path(uri, lc = nil, args = nil) ⇒ Object

Calculates the linked path to the given uri.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/n/sitemap.rb', line 129

def linked_path(uri, lc = nil, args = nil)
  if the_path = path(uri)
		i = -1
		j, s = 0, the_path.size()
		return the_path.collect { |p|
			j += 1
			if p.title.is_a?(String)
				j == s ? p.title : %|<a href="#{p.uri}">#{p.title}</a>|					
			elsif p.title.is_a?(Symbol)
				j == s ? lc[p.title] : %|<a href="#{p.uri}">#{lc[p.title]}</a>|
			else
				i += 1
				title, qs = p.title.call(args[i])
				j == s ? title : %|<a href="#{p.uri}#{qs}">#{title}</a>|				
			end
		}.join(@separator)
		
	else
		return nil
	end
end

#path(uri) ⇒ Object

Path as array of pages



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/n/sitemap.rb', line 108

def path(uri)
	return nil unless uri
	
	if page = self[uri]
		res = Array.new;
		
		res << page
		while page = page.parent
			res.unshift(page)
		end
		
		return res
	else
		$log.warn "The uri #{uri} is not registered in the SiteMap!"
	end
	
	return nil
end

#str_path(uri, lc = nil, args = nil) ⇒ Object

Calculates the path to the given uri.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/n/sitemap.rb', line 153

def str_path(uri, lc = nil, args = nil)
  if the_path = path(uri)
		i = -1
		return the_path.collect { |p| 
			if p.title.is_a?(String)
				p.title
			elsif p.title.is_a?(Symbol)
				lc[p.title]
			else
				i += 1
				p.title.call(args[i])[0]
			end
		}.join(@separator)
	else
		return nil
	end
end

#to_sObject

Returns a String representation of the Sitemap.



173
174
175
176
177
178
179
180
# File 'lib/n/sitemap.rb', line 173

def to_s
	str = "#{root}"
	arr = []
	self.each_pair { |page, title|
			arr << "#{title}: #{page}"
	}
	return "#{str} {" + arr.join(",") + "}"
end