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::ImmutableSetOf, Contracts::ImmutableSortedSetOf, Contracts::OldResourceList, Contracts::PATH_MATCHER, Contracts::ResourceList, Contracts::VectorOf

Instance Method Summary collapse

Methods included from Contracts

#Contract

Constructor Details

#initialize(initial = nil) ⇒ ResourceListContainer

Returns a new instance of ResourceListContainer



14
15
16
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 14

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

Instance Method Details

#add!(*resources) ⇒ Object



35
36
37
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 35

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

#add_cache(resource, only = nil) ⇒ Object



54
55
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 54

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

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

  if should_run? :binary, only
    if resource.binary?
      @_lookup_by_binary = @_lookup_by_binary.add(resource)
    else
      @_lookup_by_non_binary = @_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 = @_lookup_by_source_extension.put(source_ext) do |v|
        v ||= ::Hamster::SortedSet.empty
        v.add(resource)
      end
    end
  end

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

    @_lookup_by_destination_extension = @_lookup_by_destination_extension.put(dest_ext) do |v|
      v ||= ::Hamster::SortedSet.empty
      v.add(resource)
    end
  end

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

  if should_run? :ignored, only
    @_lookup_by_ignored = @_lookup_by_ignored << resource if resource.ignored?
  end
end

#add_one!(resource) ⇒ Object



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

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

  add_cache resource
end

#by_binary(is_binary) ⇒ Object



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

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

#by_destination_path(request_path) ⇒ Object



209
210
211
212
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 209

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

#by_extension(extension) ⇒ Object



188
189
190
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 188

def by_extension(extension)
  @_lookup_by_destination_extension.fetch(extension, ::Hamster::SortedSet.empty)
end

#by_extensions(extensions) ⇒ Object



194
195
196
197
198
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 194

def by_extensions(extensions)
  extensions.reduce(::Hamster::SortedSet.empty) do |sum, ext|
    sum | by_extension(ext)
  end
end

#by_page_id(page_id) ⇒ Object



216
217
218
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 216

def by_page_id(page_id)
  @_lookup_by_page_id.get(page_id.to_s.to_sym)
end

#by_path(request_path) ⇒ Object



202
203
204
205
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 202

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

#by_source_extension(extension) ⇒ Object



174
175
176
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 174

def by_source_extension(extension)
  @_lookup_by_source_extension.fetch(extension, ::Hamster::SortedSet.empty)
end

#by_source_extensions(extensions) ⇒ Object



180
181
182
183
184
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 180

def by_source_extensions(extensions)
  extensions.reduce(::Hamster::SortedSet.empty) do |sum, ext|
    sum | by_source_extension(ext)
  end
end

#remove!(resource) ⇒ Object



109
110
111
112
113
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 109

def remove!(resource)
  @_set = @_set.delete resource

  remove_cache resource
end

#remove_cache(resource, only = nil) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 116

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

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

  if should_run? :binary, only
    if resource.binary?
      @_lookup_by_binary = @_lookup_by_binary.delete(resource)
    else
      @_lookup_by_non_binary = @_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])
    if source_ext
      @_lookup_by_source_extension = @_lookup_by_source_extension.put(source_ext) do |v|
        v.delete(resource)
      end
    end
  end

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

    @_lookup_by_destination_extension = @_lookup_by_destination_extension.put(dest_ext) do |v|
      v.delete(resource)
    end
  end

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

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

#reset!(initial = nil) ⇒ Object



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

def reset!(initial = nil)
  @_set = ::Hamster::SortedSet.empty
  @_lookup_by_binary = ::Hamster::SortedSet.empty
  @_lookup_by_non_binary = ::Hamster::SortedSet.empty
  @_lookup_by_ignored = ::Hamster::SortedSet.empty

  @_lookup_by_path = ::Hamster::Hash.empty
  @_lookup_by_destination_path = ::Hamster::Hash.empty
  @_lookup_by_source_extension = ::Hamster::Hash.empty
  @_lookup_by_destination_extension = ::Hamster::Hash.empty
  @_lookup_by_page_id = ::Hamster::Hash.empty

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

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

Returns:

  • (Boolean)


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

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

  key == only
end

#to_aObject



243
244
245
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 243

def to_a
  @_set.to_a
end

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



164
165
166
167
168
169
170
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 164

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

  yield

  add_cache(resource, only)
end

#with_ignoredObject



232
233
234
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 232

def with_ignored
  @_set
end

#without_ignoredObject



238
239
240
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 238

def without_ignored
  @_set - @_lookup_by_ignored
end