class Middleman::Sitemap::Store
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.
Attributes
Public Class Methods
Source
# File lib/middleman-core/sitemap/store.rb, line 75 def initialize(app) @app = app @resources = [] @rebuild_reasons = [:first_run] @update_count = 0 @resource_list_manipulators = ::Hamster::Vector.empty @needs_sitemap_rebuild = true @lock = Monitor.new reset_lookup_cache! @app.config_context.class.send :def_delegator, :app, :sitemap end
Public Instance Methods
Source
# File lib/middleman-core/sitemap/store.rb, line 218 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 = [] @resource_list_manipulators.each do |m| ::Middleman::Util.instrument 'sitemap.manipulator', name: m[:name] do @app.logger.debug "== Running manipulator: #{m[:name]} (#{m[:priority]})" @resources = m[:manipulator].send(m[:custom_name] || :manipulate_resource_list, @resources) # Reset lookup cache reset_lookup_cache! # Rebuild cache @resources.each do |resource| @_lookup_by_path[resource.path] = resource end @resources.each do |resource| @_lookup_by_destination_path[resource.destination_path] = resource end # NB: This needs to be done after the previous two steps, # since some proxy resources are looked up by path in order to # get their metadata and subsequently their page_id. @resources.each do |resource| @_lookup_by_page_id[resource.page_id.to_s.to_sym] = resource end invalidate_resources_not_ignored_cache! end end @update_count += 1 @rebuild_reasons = [] end end end
Actually update the resource list, assuming anything has called rebuild_resource_list! since the last time it was run. This is very expensive!
Source
# File lib/middleman-core/sitemap/store.rb, line 210 def extensionless_path(file) path = file.dup ::Middleman::Util.remove_templating_extensions(path) end
Source
# File lib/middleman-core/sitemap/store.rb, line 195 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 unless @app.config[:automatic_directory_matcher].nil? relative_path = relative_path.gsub(@app.config[:automatic_directory_matcher], '/') end extensionless_path(relative_path) end
Source
# File lib/middleman-core/sitemap/store.rb, line 151 def find_resource_by_destination_path(request_path) @lock.synchronize do request_path = ::Middleman::Util.normalize_path(request_path) ensure_resource_list_updated! @_lookup_by_destination_path[request_path] end end
Source
# File lib/middleman-core/sitemap/store.rb, line 163 def find_resource_by_page_id(page_id) @lock.synchronize do ensure_resource_list_updated! @_lookup_by_page_id[page_id.to_s.to_sym] end end
Source
# File lib/middleman-core/sitemap/store.rb, line 139 def find_resource_by_path(request_path) @lock.synchronize do request_path = ::Middleman::Util.normalize_path(request_path) ensure_resource_list_updated! @_lookup_by_path[request_path] end end
Source
# File lib/middleman-core/sitemap/store.rb, line 187 def invalidate_resources_not_ignored_cache! @resources_not_ignored = nil end
Invalidate our cached view of resource that are not ignored. If your extension adds ways to ignore files, you should call this to make sure resources
works right.
Source
# File lib/middleman-core/sitemap/store.rb, line 127 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
Source
# File lib/middleman-core/sitemap/store.rb, line 106 def register_resource_list_manipulator(name, manipulator, priority=50, custom_name=nil) # 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, custom_name) ) # 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
Source
# File lib/middleman-core/sitemap/store.rb, line 91 def register_resource_list_manipulators(name, manipulator, priority=50, custom_name=nil) Array(priority || 50).each do |p| register_resource_list_manipulator(name, manipulator, p, custom_name) end end
Source
# File lib/middleman-core/sitemap/store.rb, line 174 def resources(include_ignored=false) @lock.synchronize do ensure_resource_list_updated! if include_ignored @resources else @resources_not_ignored ||= @resources.reject(&:ignored?) end end end
Private Instance Methods
Source
# File lib/middleman-core/sitemap/store.rb, line 268 def reset_lookup_cache! @lock.synchronize do @_lookup_by_path = {} @_lookup_by_destination_path = {} @_lookup_by_page_id = {} end end
Source
# File lib/middleman-core/sitemap/store.rb, line 280 def strip_away_locale(path) if @app.extensions[:i18n] path_bits = path.split('.') lang = path_bits.last return path_bits[0..-2].join('.') if @app.extensions[:i18n].langs.include?(lang.to_sym) end path end