Class: Middleman::Sitemap::Store

Inherits:
Object
  • Object
show all
Includes:
Queryable::API
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.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Queryable::API

#limit, #offset, #order_by, #select, #where

Constructor Details

- (Store) initialize(app)

Initialize with parent app

Parameters:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 33

def initialize(app)
  @app = app
  @resources = []
  @_cached_metadata = {}
  @resource_list_manipulators = []
  @needs_sitemap_rebuild = true
  
  @lock = Monitor.new
  reset_lookup_cache!

  # Handle ignore commands
  Middleman::Sitemap::Extensions::Ignores.new(self)

  # Extensions
  {
    # Register classes which can manipulate the main site map list
    on_disk: Middleman::Sitemap::Extensions::OnDisk,

    # Request Endpoints
    request_endpoints: Middleman::Sitemap::Extensions::RequestEndpoints,

    # Proxies
    proxies: Middleman::Sitemap::Extensions::Proxies,

    # Redirects
    redirects: Middleman::Sitemap::Extensions::Redirects
  }.each do |k, m|
    register_resource_list_manipulator(k, m.new(self))
  end

  app.config_context.class.send :delegate, :sitemap, :to => :app
end

Instance Attribute Details

- (Middleman::Application) app



27
28
29
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 27

def app
  @app
end

Instance Method Details

- (Object) ensure_resource_list_updated!

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



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 214

def ensure_resource_list_updated!
  @lock.synchronize do
    return unless @needs_sitemap_rebuild
    @needs_sitemap_rebuild = false

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

    @resources = @resource_list_manipulators.inject([]) do |result, (_, inst)|
      newres = inst.manipulate_resource_list(result)

      # Reset lookup cache
      reset_lookup_cache!
      newres.each do |resource|
        @_lookup_by_path[resource.path] = resource
        @_lookup_by_destination_path[resource.destination_path] = resource
      end

      newres
    end

    invalidate_resources_not_ignored_cache!
  end
end

- (String) extensionless_path(file)

Get a path without templating extensions

Parameters:

  • file (String)

Returns:

  • (String)


206
207
208
209
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 206

def extensionless_path(file)
  path = file.dup
  remove_templating_extensions(path)
end

- (String) file_to_path(file)

Get the URL path for an on-disk file

Parameters:

  • file (String)

Returns:

  • (String)


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 187

def file_to_path(file)
  file = File.join(@app.root, file)

  prefix = @app.source_dir.sub(/\/$/, '') + '/'
  return false unless file.start_with?(prefix)

  path = file.sub(prefix, '')

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

  extensionless_path(path)
end

- (Middleman::Sitemap::Resource) find_resource_by_destination_path(request_path)

Find a resource given its destination path

Parameters:

  • request_path (String)

    The destination (output) path of a resource.

Returns:



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

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

- (Middleman::Sitemap::Resource) find_resource_by_path(request_path)

Find a resource given its original path

Parameters:

  • request_path (String)

    The original path of a resource.

Returns:



88
89
90
91
92
93
94
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 88

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

- (Object) invalidate_resources_not_ignored_cache!

Invalidate our cached view of resource that are not ingnored. If your extension adds ways to ignore files, you should call this to make sure #resources works right.



123
124
125
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 123

def invalidate_resources_not_ignored_cache!
  @resources_not_ignored = nil
end

- (Hash) metadata_for_file(source_file)

Get the metadata for a specific file

Parameters:

  • source_file (String)

Returns:

  • (Hash)


139
140
141
142
143
144
145
146
147
148
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 139

def (source_file)
   = { :options => {}, :locals => {} }

  .inject() do |result, (callback, matcher)|
    next result if matcher && !source_file.match(matcher)

     = callback.call(source_file).dup
    result.deep_merge()
  end
end

- (Hash) metadata_for_path(request_path)

Get the metadata for a specific URL

Parameters:

  • request_path (String)

Returns:

  • (Hash)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 165

def (request_path)
  return @_cached_metadata[request_path] if @_cached_metadata[request_path]

   = { :options => {}, :locals => {} }

  @_cached_metadata[request_path] = .inject() do |result, (callback, matcher)|
    case matcher
    when Regexp
      next result unless request_path =~ matcher
    when String
      next result unless File.fnmatch('/' + Util.strip_leading_slash(matcher), "/#{request_path}")
    end

     = callback.call(request_path).dup

    result.deep_merge()
  end
end

- (Array<Array<Proc, Regexp>>) provides_metadata(matcher = nil, &block)

Register a handler to provide metadata on a file path

Parameters:

  • matcher (Regexp) (defaults to: nil)

Returns:

  • (Array<Array<Proc, Regexp>>)


130
131
132
133
134
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 130

def (matcher=nil, &block)
  @_provides_metadata ||= []
  @_provides_metadata << [block, matcher] if block_given?
  @_provides_metadata
end

- (Array<Array<Proc, Regexp>>) provides_metadata_for_path(matcher = nil, &block)

Register a handler to provide metadata on a url path

Parameters:

  • matcher (Regexp) (defaults to: nil)

Returns:

  • (Array<Array<Proc, Regexp>>)


153
154
155
156
157
158
159
160
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 153

def (matcher=nil, &block)
  @_provides_metadata_for_path ||= []
  if block_given?
    @_provides_metadata_for_path << [block, matcher]
    @_cached_metadata = {}
  end
  @_provides_metadata_for_path
end

- rebuild_resource_list!(reason = nil)

This method returns an undefined value.

Rebuild the list of resources from scratch, using registed manipulators



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

def rebuild_resource_list!(reason=nil)
  @lock.synchronize do
    @needs_sitemap_rebuild = true
  end
end

- register_resource_list_manipulator(name, inst, unused = true)

This method returns an undefined value.

Register a klass which can manipulate the main site map list. Best to register these in a before_configuration or after_configuration hook.

Parameters:

  • name (Symbol)

    Name of the manipulator for debugging

  • inst (Class, Module)

    Abstract namespace which can update the resource list



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

def register_resource_list_manipulator(name, inst, unused=true)
  @resource_list_manipulators << [name, inst]
  rebuild_resource_list!(:registered_new)
end

- (Array<Middleman::Sitemap::Resource>) resources(include_ignored = false)

Get the array of all resources

Parameters:

  • include_ignored (Boolean) (defaults to: false)

    Whether to include ignored resources

Returns:



110
111
112
113
114
115
116
117
118
119
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 110

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