Class: N::SiteMap
Overview
SiteMap
Encapsulates the page structure and additional metadata.
TODO:
move out of the UI namespace. include url remapping functionality.
Design
Unlike the original version, this is presentation agnostic, and greatly simplified.
Todo:
-
support strings as titles.
-
add support to read the map from a config file.
-
rename to appmap ?
-
Use a second class in the UI namespace for rendering
related stuff?
Instance Attribute Summary collapse
-
#root ⇒ Object
The root page for this sitemap.
-
#separator ⇒ Object
The separator used when creating paths.
Attributes inherited from SafeHash
Instance Method Summary collapse
- #<<(page) ⇒ Object
-
#initialize(separator = " > ") ⇒ SiteMap
constructor
A new instance of SiteMap.
-
#linked_path(uri, lc = nil, args = nil) ⇒ Object
Calculates the linked path to the given uri.
-
#path(uri) ⇒ Object
Path as array of pages.
-
#str_path(uri, lc = nil, args = nil) ⇒ Object
Calculates the path to the given uri.
-
#to_s ⇒ Object
Returns a String representation of the Sitemap.
Methods inherited from SafeHash
#[], #[]=, #clear, #delete, #keys, #size, #values
Constructor Details
#initialize(separator = " > ") ⇒ SiteMap
Returns a new instance of SiteMap.
88 89 90 91 |
# File 'lib/nitro/ui/sitemap.rb', line 88 def initialize(separator = " > ") super @separator = separator end |
Instance Attribute Details
#root ⇒ Object
The root page for this sitemap
83 84 85 |
# File 'lib/nitro/ui/sitemap.rb', line 83 def root @root end |
#separator ⇒ Object
The separator used when creating paths
86 87 88 |
# File 'lib/nitro/ui/sitemap.rb', line 86 def separator @separator end |
Instance Method Details
#<<(page) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/nitro/ui/sitemap.rb', line 93 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.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/nitro/ui/sitemap.rb', line 124 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
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/nitro/ui/sitemap.rb', line 103 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 Logger.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.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/nitro/ui/sitemap.rb', line 148 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 |