Class: Middleman::Sitemap::Store

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

Overview

The Store class

The Store manages a collection of Resource objects, which represent individual items in the sitemap. Resources are indexed by "source path", which is the path relative to the source directory, minus any template extensions. All "path" parameters used in this class are source paths.

Constant Summary

Constants included from Contracts

Contracts::ImmutableSetOf, Contracts::ImmutableSortedSetOf, Contracts::OldResourceList, Contracts::PATH_MATCHER, Contracts::ResourceList, Contracts::VectorOf

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Contracts

#Contract

Constructor Details

#initialize(app) ⇒ Store


83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 83

def initialize(app)
  @app = app
  @resources = ResourceListContainer.new
  @rebuild_reasons = [:first_run]
  @update_count = 0

  @resource_list_manipulators = ::Hamster::Vector.empty
  @needs_sitemap_rebuild = true

  @lock = Monitor.new

  @app.config_context.class.send :def_delegator, :app, :sitemap
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app


72
73
74
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 72

def app
  @app
end

#resourcesObject (readonly)

Returns the value of attribute resources


78
79
80
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 78

def resources
  @resources
end

#update_countObject (readonly)

Returns the value of attribute update_count


75
76
77
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 75

def update_count
  @update_count
end

Instance Method Details

#ensure_resource_list_updated!Object

Actually update the resource list, assuming anything has called rebuild_resource_list! since the last time it was run. This is very expensive!


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 166

def ensure_resource_list_updated!
  return if @app.config[:disable_sitemap]

  @lock.synchronize do
    return unless @needs_sitemap_rebuild

    ::Middleman::Util.instrument 'sitemap.update', reasons: @rebuild_reasons.uniq do
      @needs_sitemap_rebuild = false

      @app.logger.debug '== Rebuilding resource list'

      @resources.reset!

      @resource_list_manipulators.each do |m|
        ::Middleman::Util.instrument 'sitemap.manipulator', name: m[:name] do
          @app.logger.debug "== Running manipulator: #{m[:name]} (#{m[:priority]})"

          if m[:manipulator].respond_to?(:manipulate_resource_list_container!)
            m[:manipulator].send(:manipulate_resource_list_container!, @resources)
          elsif m[:manipulator].respond_to?(:manipulate_resource_list)
            m[:manipulator].send(:manipulate_resource_list, resources.to_a).tap do |result|
              @resources.reset!(result)
            end
          end
        end
      end

      @update_count += 1

      @rebuild_reasons = []
    end
  end
end

#extensionless_path(file) ⇒ Object


158
159
160
161
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 158

def extensionless_path(file)
  path = file.dup
  ::Middleman::Util.remove_templating_extensions(path)
end

#file_to_path(file) ⇒ Object


145
146
147
148
149
150
151
152
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 145

def file_to_path(file)
  relative_path = file.is_a?(Pathname) ? file.to_s : file[:relative_path].to_s

  # Replace a file name containing automatic_directory_matcher with a folder
  relative_path = relative_path.gsub(@app.config[:automatic_directory_matcher], '/') unless @app.config[:automatic_directory_matcher].nil?

  extensionless_path(relative_path)
end

#rebuild_resource_list!(name) ⇒ Object


133
134
135
136
137
138
139
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 133

def rebuild_resource_list!(name)
  @lock.synchronize do
    @rebuild_reasons << name
    @app.logger.debug "== Requesting resource list rebuilding: #{name}"
    @needs_sitemap_rebuild = true
  end
end

#register_resource_list_manipulator(name, manipulator, priority = 50) ⇒ Object


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 112

def register_resource_list_manipulator(name, manipulator, priority = 50)
  # The third argument used to be a boolean - handle those who still pass one
  priority = 50 unless priority.is_a? Numeric
  @resource_list_manipulators = @resource_list_manipulators.push(
    ManipulatorDescriptor.new(name, manipulator, priority)
  )

  # The index trick is used so that the sort is stable - manipulators with the same priority
  # will always be ordered in the same order as they were registered.
  n = 0
  @resource_list_manipulators = @resource_list_manipulators.sort_by do |m|
    n += 1
    [m[:priority], n]
  end

  rebuild_resource_list!(:"registered_new_manipulator_#{name}")
end

#register_resource_list_manipulators(name, manipulator, priority = 50) ⇒ Object


98
99
100
101
102
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 98

def register_resource_list_manipulators(name, manipulator, priority = 50)
  Array(priority || 50).each do |p|
    register_resource_list_manipulator(name, manipulator, p)
  end
end

#Symbol

This method returns an undefined value.

Register an object which can transform the sitemap resource list. Best to register these in a before_configuration or after_configuration hook.


111
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 111

Contract Symbol, Or[RespondTo[:manipulate_resource_list], RespondTo[:manipulate_resource_list_container!]], Maybe[Num, Bool] => Any