Class: Middleman::Sitemap::Store
- Inherits:
-
Object
- Object
- Middleman::Sitemap::Store
- Includes:
- Contracts
- Defined in:
- 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
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#update_count ⇒ Object
readonly
Returns the value of attribute update_count.
Instance Method Summary collapse
-
#ensure_resource_list_updated! ⇒ Object
Actually update the resource list, assuming anything has called rebuild_resource_list! since the last time it was run.
- #extensionless_path(file) ⇒ Object
- #file_to_path(file) ⇒ Object
- #find_resource_by_destination_path(request_path) ⇒ Object
- #find_resource_by_page_id(page_id) ⇒ Object
- #find_resource_by_path(request_path) ⇒ Object
-
#initialize(app) ⇒ Store
constructor
A new instance of Store.
-
#invalidate_resources_not_ignored_cache! ⇒ Object
Invalidate our cached view of resource that are not ignored.
- #rebuild_resource_list!(name) ⇒ Object
- #register_resource_list_manipulator(name, manipulator, priority = 50, custom_name = nil) ⇒ Object
- #register_resource_list_manipulators(name, manipulator, priority = 50, custom_name = nil) ⇒ Object
- #resources(include_ignored = false) ⇒ Object
-
#Symbol
Register an object which can transform the sitemap resource list.
Methods included from Contracts
Constructor Details
#initialize(app) ⇒ Store
Returns a new instance of Store.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# 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 |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
67 68 69 |
# File 'lib/middleman-core/sitemap/store.rb', line 67 def app @app end |
#update_count ⇒ Object (readonly)
Returns the value of attribute update_count.
70 71 72 |
# File 'lib/middleman-core/sitemap/store.rb', line 70 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!
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# 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_sym] = resource end invalidate_resources_not_ignored_cache! end end @update_count += 1 @rebuild_reasons = [] end end end |
#extensionless_path(file) ⇒ Object
210 211 212 213 |
# File 'lib/middleman-core/sitemap/store.rb', line 210 def extensionless_path(file) path = file.dup ::Middleman::Util.remove_templating_extensions(path) end |
#file_to_path(file) ⇒ Object
195 196 197 198 199 200 201 202 203 204 |
# 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 |
#find_resource_by_destination_path(request_path) ⇒ Object
151 152 153 154 155 156 157 |
# 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 |
#find_resource_by_page_id(page_id) ⇒ Object
163 164 165 166 167 168 |
# 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_sym] end end |
#find_resource_by_path(request_path) ⇒ Object
139 140 141 142 143 144 145 |
# 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 |
#invalidate_resources_not_ignored_cache! ⇒ Object
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.
187 188 189 |
# File 'lib/middleman-core/sitemap/store.rb', line 187 def invalidate_resources_not_ignored_cache! @resources_not_ignored = nil end |
#rebuild_resource_list!(name) ⇒ Object
127 128 129 130 131 132 133 |
# 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 |
#register_resource_list_manipulator(name, manipulator, priority = 50, custom_name = nil) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# 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 |
#register_resource_list_manipulators(name, manipulator, priority = 50, custom_name = nil) ⇒ Object
91 92 93 94 95 |
# 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 |
#resources(include_ignored = false) ⇒ Object
174 175 176 177 178 179 180 181 182 183 |
# 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 |
#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.
105 |
# File 'lib/middleman-core/sitemap/store.rb', line 105 Contract Symbol, RespondTo[:manipulate_resource_list], Maybe[Num, Bool], Maybe[Symbol] => Any |