Class: Frontman::SitemapTree
- Inherits:
-
Object
- Object
- Frontman::SitemapTree
- Defined in:
- lib/frontman/sitemap_tree.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#position ⇒ Object
Returns the value of attribute position.
-
#resource ⇒ Object
Returns the value of attribute resource.
-
#url ⇒ Object
Returns the value of attribute url.
-
#url_part ⇒ Object
readonly
Returns the value of attribute url_part.
Class Method Summary collapse
- .format_url(url) ⇒ Object
- .proxy_resources ⇒ Object
- .proxy_resources_from_template(template) ⇒ Object
- .resources ⇒ Object
Instance Method Summary collapse
- #add(resource) ⇒ Object
- #add_parts(parts, used_parts, resource) ⇒ Object
- #add_proxy(destination, template, data = {}) ⇒ Object
- #all_urls ⇒ Object
- #ancestor(part, level = 0) ⇒ Object
- #final_url ⇒ Object
- #find(parts) ⇒ Object
- #flatten(take = false) ⇒ Object
- #folder? ⇒ Boolean
- #format_url(url) ⇒ Object
- #from_resource(resource) ⇒ Object
- #from_url(url) ⇒ Object
- #get_all_pages(options = {}) ⇒ Object
-
#initialize(url_part) ⇒ SitemapTree
constructor
A new instance of SitemapTree.
- #inspect ⇒ Object
- #languages ⇒ Object
- #pages ⇒ Object
- #pretty_print(space) ⇒ Object
- #resources ⇒ Object
- #siblings ⇒ Object
Constructor Details
#initialize(url_part) ⇒ SitemapTree
Returns a new instance of SitemapTree.
63 64 65 66 67 68 69 70 71 |
# File 'lib/frontman/sitemap_tree.rb', line 63 def initialize(url_part) @parent = nil @children = [] @resource = nil @url_part = url_part @@urls ||= {} @position = nil @url = nil end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
8 9 10 |
# File 'lib/frontman/sitemap_tree.rb', line 8 def children @children end |
#parent ⇒ Object
Returns the value of attribute parent.
9 10 11 |
# File 'lib/frontman/sitemap_tree.rb', line 9 def parent @parent end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
8 9 10 |
# File 'lib/frontman/sitemap_tree.rb', line 8 def path @path end |
#position ⇒ Object
Returns the value of attribute position.
9 10 11 |
# File 'lib/frontman/sitemap_tree.rb', line 9 def position @position end |
#resource ⇒ Object
Returns the value of attribute resource.
9 10 11 |
# File 'lib/frontman/sitemap_tree.rb', line 9 def resource @resource end |
#url ⇒ Object
Returns the value of attribute url.
9 10 11 |
# File 'lib/frontman/sitemap_tree.rb', line 9 def url @url end |
#url_part ⇒ Object (readonly)
Returns the value of attribute url_part.
8 9 10 |
# File 'lib/frontman/sitemap_tree.rb', line 8 def url_part @url_part end |
Class Method Details
.format_url(url) ⇒ Object
24 25 26 |
# File 'lib/frontman/sitemap_tree.rb', line 24 def format_url(url) url.chomp('index.html').gsub('.html', '') end |
.proxy_resources ⇒ Object
16 17 18 |
# File 'lib/frontman/sitemap_tree.rb', line 16 def proxy_resources @@proxy_resources ||= {} end |
.proxy_resources_from_template(template) ⇒ Object
20 21 22 |
# File 'lib/frontman/sitemap_tree.rb', line 20 def proxy_resources_from_template(template) proxy_resources[format_url(template)] || [] end |
.resources ⇒ Object
12 13 14 |
# File 'lib/frontman/sitemap_tree.rb', line 12 def resources @@resources ||= [] end |
Instance Method Details
#add(resource) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/frontman/sitemap_tree.rb', line 33 def add(resource) return if resource.destination_path.end_with?('.js', '.css', '.yml') url = format_url(resource.destination_path) existing_resource = from_url(url)&.resource unless existing_resource.nil? raise DuplicateResourceError.create(resource, url, existing_resource) end raise ExistingRedirectError.create(resource, url) if Frontman::App.instance.get_redirect(url) SitemapTree.resources.push(resource) parts = url.split('/') used_parts = [] add_parts(parts, used_parts, resource) end |
#add_parts(parts, used_parts, resource) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/frontman/sitemap_tree.rb', line 128 def add_parts(parts, used_parts, resource) if parts.length <= 0 @resource = resource return end current_part = parts.shift used_parts.push(current_part) child = @children.select { |c| c.url_part == current_part } child = !child.empty? ? child[0] : nil if child.nil? child = SitemapTree.new(current_part) child.parent = self url = used_parts.join('/') child.url = url @@urls[url.to_sym] = child child.position = @children.length @children.push(child) end child.add_parts(parts, used_parts, resource) end |
#add_proxy(destination, template, data = {}) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/frontman/sitemap_tree.rb', line 51 def add_proxy(destination, template, data = {}) resource = Resource.from_path(template, destination, true) resource.data = resource.data.to_h.merge(data.to_h).to_ostruct key = format_url(template) SitemapTree.proxy_resources[key] ||= [] T.must(SitemapTree.proxy_resources[key]).push(resource) add(resource) end |
#all_urls ⇒ Object
189 190 191 |
# File 'lib/frontman/sitemap_tree.rb', line 189 def all_urls @@urls end |
#ancestor(part, level = 0) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/frontman/sitemap_tree.rb', line 91 def ancestor(part, level = 0) ancestors = [self] ancestor = self while !ancestor.nil? && (ancestor.url_part != part) ancestor = ancestor.parent ancestors.unshift(ancestor) end ancestors[level] if !ancestor.nil? && (ancestor.url_part == part) end |
#final_url ⇒ Object
77 78 79 |
# File 'lib/frontman/sitemap_tree.rb', line 77 def final_url "/#{format_url(@url)}/" end |
#find(parts) ⇒ Object
152 153 154 155 156 157 158 159 |
# File 'lib/frontman/sitemap_tree.rb', line 152 def find(parts) return self if parts.length <= 0 current_part = parts.shift child = @children.select { |c| c.url_part == current_part } child[0].find(parts) unless child.empty? end |
#flatten(take = false) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/frontman/sitemap_tree.rb', line 103 def flatten(take = false) resources = [] resources.push(self) if take && @resource && @children.empty? children.each do |child| resources += child.flatten(true) end resources end |
#folder? ⇒ Boolean
169 170 171 |
# File 'lib/frontman/sitemap_tree.rb', line 169 def folder? !children.empty? end |
#format_url(url) ⇒ Object
73 74 75 |
# File 'lib/frontman/sitemap_tree.rb', line 73 def format_url(url) url.chomp('index.html').gsub('.html', '') end |
#from_resource(resource) ⇒ Object
81 82 83 |
# File 'lib/frontman/sitemap_tree.rb', line 81 def from_resource(resource) from_url(format_url(resource.destination_path)) end |
#from_url(url) ⇒ Object
85 86 87 88 89 |
# File 'lib/frontman/sitemap_tree.rb', line 85 def from_url(url) return self if url == '/' @@urls[url.gsub(%r{^/}, '').gsub(%r{/$}, '').to_sym] end |
#get_all_pages(options = {}) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/frontman/sitemap_tree.rb', line 115 def get_all_pages( = {}) resources = [] resources.push(self) if @resource @children.each do |child| resources.push(child) unless child.resource.nil? resources += child.get_all_pages() end resources.uniq end |
#inspect ⇒ Object
193 194 195 |
# File 'lib/frontman/sitemap_tree.rb', line 193 def inspect "SitemapTree: #{@resource ? @resource.destination_path : @url_part}" end |
#languages ⇒ Object
173 174 175 |
# File 'lib/frontman/sitemap_tree.rb', line 173 def languages children.select { |c| @@languages.include?(c.url_part) } end |
#pages ⇒ Object
165 166 167 |
# File 'lib/frontman/sitemap_tree.rb', line 165 def pages children.select(&:resource) end |
#pretty_print(space) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/frontman/sitemap_tree.rb', line 177 def pretty_print(space) spaces = space >= 0 ? ' ' * space : '' print(spaces + @url_part + "\n") if @url_part print(spaces + " -> r\n") if @resource @children.each do |child| child.pretty_print(space + 1) end end |
#resources ⇒ Object
29 30 31 |
# File 'lib/frontman/sitemap_tree.rb', line 29 def resources @@resources ||= [] end |
#siblings ⇒ Object
161 162 163 |
# File 'lib/frontman/sitemap_tree.rb', line 161 def siblings @parent.children end |