Class: Middleman::Sitemap::ResourceListContainer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Contracts
Defined in:
middleman-core/lib/middleman-core/sitemap/resource_list_container.rb

Constant Summary

Constants included from Contracts

Contracts::PATH_MATCHER

Instance Method Summary collapse

Methods included from Contracts

#Contract

Constructor Details

#initialize(initial = nil) ⇒ ResourceListContainer

Returns a new instance of ResourceListContainer.


16
17
18
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 16

def initialize(initial = nil)
  reset!(initial)
end

Instance Method Details

#add!(*resources) ⇒ Object


37
38
39
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 37

def add!(*resources)
  resources.each(&method(:add_one!))
end

#add_cache(resource, only = nil) ⇒ Object


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 56

def add_cache(resource, only = nil)
  @_lookup_by_path[::Middleman::Util.normalize_path(resource.path)] = resource if should_run? :path, only

  @_lookup_by_destination_path[::Middleman::Util.normalize_path(resource.destination_path)] = resource if should_run? :destination_path, only

  if should_run? :binary, only
    if resource.binary?
      @_lookup_by_binary.add(resource)
    else
      @_lookup_by_non_binary.add(resource)
    end
  end

  if should_run? :source_extension, only
    source_ext = resource.file_descriptor && resource.file_descriptor[:full_path] && ::File.extname(resource.file_descriptor[:full_path])
    if source_ext
      @_lookup_by_source_extension[source_ext] ||= Set.new
      @_lookup_by_source_extension[source_ext].add(resource)
    end
  end

  if should_run? :destination_extension, only
    dest_ext = ::File.extname(resource.destination_path)

    v = @_lookup_by_destination_extension[dest_ext] ||= Set.new
    v.add(resource)
  end

  @_lookup_by_page_id[resource.page_id.to_s.to_sym] = resource if should_run? :page_id, only

  @_lookup_by_ignored.add(resource) if should_run?(:ignored, only) && resource.ignored?
end

#add_one!(resource) ⇒ Object


42
43
44
45
46
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 42

def add_one!(resource)
  @_set.add(resource)

  add_cache resource
end

#by_binary(is_binary) ⇒ Object


197
198
199
200
201
202
203
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 197

def by_binary(is_binary)
  if is_binary
    @_lookup_by_binary
  else
    @_lookup_by_non_binary
  end
end

#by_destination_path(request_path) ⇒ Object


184
185
186
187
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 184

def by_destination_path(request_path)
  request_path = ::Middleman::Util.normalize_path(request_path)
  @_lookup_by_destination_path[request_path]
end

#by_extension(extension) ⇒ Object


163
164
165
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 163

def by_extension(extension)
  @_lookup_by_destination_extension[extension] || Set.new
end

#by_extensions(extensions) ⇒ Object


169
170
171
172
173
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 169

def by_extensions(extensions)
  extensions.reduce(Set.new) do |sum, ext|
    sum | by_extension(ext)
  end
end

#by_page_id(page_id) ⇒ Object


191
192
193
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 191

def by_page_id(page_id)
  @_lookup_by_page_id[page_id.to_s.to_sym]
end

#by_path(request_path) ⇒ Object


177
178
179
180
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 177

def by_path(request_path)
  request_path = ::Middleman::Util.normalize_path(request_path)
  @_lookup_by_path[request_path]
end

#by_priorityObject


218
219
220
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 218

def by_priority
  without_ignored.sort
end

#by_source_extension(extension) ⇒ Object


149
150
151
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 149

def by_source_extension(extension)
  @_lookup_by_source_extension[extension] || Set.new
end

#by_source_extensions(extensions) ⇒ Object


155
156
157
158
159
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 155

def by_source_extensions(extensions)
  extensions.reduce(Set.new) do |sum, ext|
    sum | by_source_extension(ext)
  end
end

#remove!(resource) ⇒ Object


90
91
92
93
94
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 90

def remove!(resource)
  @_set.delete resource

  remove_cache resource
end

#remove_cache(resource, only = nil) ⇒ Object


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 97

def remove_cache(resource, only = nil)
  if should_run? :path, only
    @_lookup_by_path.delete(
      ::Middleman::Util.normalize_path(resource.path)
    )
  end

  if should_run? :destination_path, only
    @_lookup_by_destination_path.delete(
      ::Middleman::Util.normalize_path(resource.destination_path)
    )
  end

  if should_run? :binary, only
    if resource.binary?
      @_lookup_by_binary.delete(resource)
    else
      @_lookup_by_non_binary.delete(resource)
    end
  end

  if should_run? :source_extension, only
    source_ext = resource.file_descriptor && resource.file_descriptor[:full_path] && ::File.extname(resource.file_descriptor[:full_path])
    @_lookup_by_source_extension[source_ext]&.delete(resource) if source_ext
  end

  if should_run? :destination_extension, only
    dest_ext = ::File.extname(resource.destination_path)

    @_lookup_by_destination_extension[dest_ext]&.delete(resource)
  end

  if should_run? :page_id, only
    @_lookup_by_page_id.delete(
      resource.page_id.to_s.to_sym
    )
  end

  @_lookup_by_ignored.delete(resource) if should_run? :ignored, only
end

#reset!(initial = nil) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 21

def reset!(initial = nil)
  @_set = Set.new
  @_lookup_by_binary = Set.new
  @_lookup_by_non_binary = Set.new
  @_lookup_by_ignored = Set.new

  @_lookup_by_path = {}
  @_lookup_by_destination_path = {}
  @_lookup_by_source_extension = {}
  @_lookup_by_destination_extension = {}
  @_lookup_by_page_id = {}

  add!(*initial.sort_by(&:priority)) unless initial.nil?
end

#should_run?(key, only = nil) ⇒ Boolean

Returns:

  • (Boolean)

49
50
51
52
53
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 49

def should_run?(key, only = nil)
  return true if only.nil?

  key == only
end

#to_aObject


223
224
225
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 223

def to_a
  @_set.to_a
end

#update!(resource, only = nil) ⇒ Object


139
140
141
142
143
144
145
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 139

def update!(resource, only = nil)
  remove_cache(resource, only)

  yield

  add_cache(resource, only)
end

#with_ignoredObject


207
208
209
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 207

def with_ignored
  @_set
end

#without_ignoredObject


213
214
215
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 213

def without_ignored
  @_set - @_lookup_by_ignored
end