Class: Middleman::Sitemap::Store

Inherits:
Object
  • Object
show all
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

Constant Summary

Constants included from Contracts

Contracts::PATH_MATCHER, Contracts::ResourceList

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Contracts

#Contract

Constructor Details

- (Store) initialize(app)

Initialize with parent app

Parameters:



54
55
56
57
58
59
60
61
62
63
64
65
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 54

def initialize(app)
  @app = app
  @resources = []
  # TODO: Should this be a set or hash?
  @resource_list_manipulators = []
  @needs_sitemap_rebuild = true

  @lock = Monitor.new
  reset_lookup_cache!

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

Instance Attribute Details

- (Middleman::Application) app (readonly)



50
51
52
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 50

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!



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 169

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.reduce([]) do |result, (_, manipulator, _)|
      newres = manipulator.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

- (Object) extensionless_path(file)



161
162
163
164
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 161

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

- (Object) file_to_path(file)



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

def file_to_path(file)
  relative_path = 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

- (Object) find_resource_by_destination_path(request_path)



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

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

- (Object) find_resource_by_path(request_path)



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

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.



138
139
140
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 138

def invalidate_resources_not_ignored_cache!
  @resources_not_ignored = nil
end

- rebuild_resource_list!(_ = nil)

This method returns an undefined value.

Rebuild the list of resources from scratch, using registed manipulators



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

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

- (Object) register_resource_list_manipulator(name, manipulator, priority = 50)



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 75

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 << [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[2], n]
  end
  rebuild_resource_list!(:registered_new)
end

- (Object) resources(include_ignored = false)



125
126
127
128
129
130
131
132
133
134
# File 'middleman-core/lib/middleman-core/sitemap/store.rb', line 125

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.

Parameters:

  • name (Symbol)

    Name of the manipulator for debugging

  • manipulator (#manipulate_resource_list)

    Resource list manipulator

  • priority (Numeric)

    Sets the order of this resource list manipulator relative to the rest. By default this is 50, and manipulators run in the order they are registered, but if a priority is provided then this will run ahead of or behind other manipulators.



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

Contract Symbol, RespondTo['manipulate_resource_list'], Maybe[Num] => Any