Class: Frontman::SitemapTree

Inherits:
Object
  • Object
show all
Defined in:
lib/frontman/sitemap_tree.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url_part) ⇒ SitemapTree

Returns a new instance of SitemapTree.



65
66
67
68
69
70
71
72
73
# File 'lib/frontman/sitemap_tree.rb', line 65

def initialize(url_part)
  @parent = nil
  @children = []
  @resource = nil
  @url_part = url_part
  @@urls ||= {}
  @position = nil
  @url = nil
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



8
9
10
# File 'lib/frontman/sitemap_tree.rb', line 8

def children
  @children
end

#parentObject

Returns the value of attribute parent.



9
10
11
# File 'lib/frontman/sitemap_tree.rb', line 9

def parent
  @parent
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/frontman/sitemap_tree.rb', line 8

def path
  @path
end

#positionObject

Returns the value of attribute position.



9
10
11
# File 'lib/frontman/sitemap_tree.rb', line 9

def position
  @position
end

#resourceObject

Returns the value of attribute resource.



9
10
11
# File 'lib/frontman/sitemap_tree.rb', line 9

def resource
  @resource
end

#urlObject

Returns the value of attribute url.



9
10
11
# File 'lib/frontman/sitemap_tree.rb', line 9

def url
  @url
end

#url_partObject (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_resourcesObject



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

.resourcesObject



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
50
51
# 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

  if Frontman::App.instance.get_redirect(url)
    raise ExistingRedirectError.create(resource, url)
  end

  SitemapTree.resources.push(resource)
  parts = url.split('/')
  used_parts = []
  add_parts(parts, used_parts, resource)
end

#add_parts(parts, used_parts, resource) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/frontman/sitemap_tree.rb', line 130

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



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/frontman/sitemap_tree.rb', line 53

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_urlsObject



191
192
193
# File 'lib/frontman/sitemap_tree.rb', line 191

def all_urls
  @@urls
end

#ancestor(part, level = 0) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/frontman/sitemap_tree.rb', line 93

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_urlObject



79
80
81
# File 'lib/frontman/sitemap_tree.rb', line 79

def final_url
  "/#{format_url(@url)}/"
end

#find(parts) ⇒ Object



154
155
156
157
158
159
160
161
# File 'lib/frontman/sitemap_tree.rb', line 154

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



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/frontman/sitemap_tree.rb', line 105

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

Returns:

  • (Boolean)


171
172
173
# File 'lib/frontman/sitemap_tree.rb', line 171

def folder?
  !children.empty?
end

#format_url(url) ⇒ Object



75
76
77
# File 'lib/frontman/sitemap_tree.rb', line 75

def format_url(url)
  url.chomp('index.html').gsub('.html', '')
end

#from_resource(resource) ⇒ Object



83
84
85
# File 'lib/frontman/sitemap_tree.rb', line 83

def from_resource(resource)
  from_url(format_url(resource.destination_path))
end

#from_url(url) ⇒ Object



87
88
89
90
91
# File 'lib/frontman/sitemap_tree.rb', line 87

def from_url(url)
  return self if url == '/'

  @@urls[url.gsub(%r{^/}, '').gsub(%r{/$}, '').to_sym]
end

#get_all_pages(options = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/frontman/sitemap_tree.rb', line 117

def get_all_pages(options = {})
  resources = []
  resources.push(self) if @resource

  @children.each do |child|
    resources.push(child) unless child.resource.nil?

    resources += child.get_all_pages(options)
  end

  resources.uniq
end

#inspectObject



195
196
197
# File 'lib/frontman/sitemap_tree.rb', line 195

def inspect
  "SitemapTree: #{@resource ? @resource.destination_path : @url_part}"
end

#languagesObject



175
176
177
# File 'lib/frontman/sitemap_tree.rb', line 175

def languages
  children.select { |c| @@languages.include?(c.url_part) }
end

#pagesObject



167
168
169
# File 'lib/frontman/sitemap_tree.rb', line 167

def pages
  children.select(&:resource)
end

#pretty_print(space) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/frontman/sitemap_tree.rb', line 179

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

#resourcesObject



29
30
31
# File 'lib/frontman/sitemap_tree.rb', line 29

def resources
  @@resources ||= []
end

#siblingsObject



163
164
165
# File 'lib/frontman/sitemap_tree.rb', line 163

def siblings
  @parent.children
end