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

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


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

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

  add_cache resource
end

#by_binary(is_binary) ⇒ Object


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

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

#by_destination_path(request_path) ⇒ Object


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

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


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

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

#by_extensions(extensions) ⇒ Object


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

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

#by_page_id(page_id) ⇒ Object


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

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

#by_path(request_path) ⇒ Object


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

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


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

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

#by_source_extensions(extensions) ⇒ Object


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

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

#remove!(resource) ⇒ Object


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

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

  remove_cache resource
end

#remove_cache(resource, only = nil) ⇒ Object


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
162
163
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 118

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


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 = ::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)

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


245
246
247
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 245

def to_a
  @_set.to_a
end

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


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

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

  yield

  add_cache(resource, only)
end

#with_ignoredObject


234
235
236
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 234

def with_ignored
  @_set
end

#without_ignoredObject


240
241
242
# File 'middleman-core/lib/middleman-core/sitemap/resource_list_container.rb', line 240

def without_ignored
  @_set - @_lookup_by_ignored
end