Class: Middleman::Sitemap::Extensions::OnDisk

Inherits:
Extension
  • Object
show all
Defined in:
lib/middleman-core/sitemap/extensions/on_disk.rb

Constant Summary

Constants included from Contracts

Contracts::PATH_MATCHER

Instance Attribute Summary

Attributes inherited from Extension

#app, #options

Instance Method Summary collapse

Methods inherited from Extension

activated_extension, #add_exposed_to_context, #after_build, #after_configuration, #after_extension_activated, after_extension_activated, #before_build, clear_after_extension_callbacks, config, define_setting, expose_to_application, expose_to_config, expose_to_template, global_config, helpers, option, resources

Methods included from Contracts

#Contract

Constructor Details

#initialize(app, config = {}, &block) ⇒ OnDisk

Returns a new instance of OnDisk.



10
11
12
13
14
15
# File 'lib/middleman-core/sitemap/extensions/on_disk.rb', line 10

def initialize(app, config={}, &block)
  super

  @file_paths_on_disk = Set.new
  @waiting_for_ready = true
end

Instance Method Details

#before_configurationObject



25
26
27
# File 'lib/middleman-core/sitemap/extensions/on_disk.rb', line 25

def before_configuration
  app.files.on_change(:source, &method(:update_files))
end

#files_for_sitemapObject



55
56
57
# File 'lib/middleman-core/sitemap/extensions/on_disk.rb', line 55

def files_for_sitemap
  @app.files.by_type(:source).files.reject(&method(:ignored?))
end

#ignored?(file) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/middleman-core/sitemap/extensions/on_disk.rb', line 30

def ignored?(file)
  @app.config[:ignored_sitemap_matchers].any? do |_, callback|
    callback.call(file, @app)
  end
end

#manipulate_resource_list(resources) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/middleman-core/sitemap/extensions/on_disk.rb', line 62

def manipulate_resource_list(resources)
  resources + files_for_sitemap.map do |file|
    ::Middleman::Sitemap::Resource.new(
      @app.sitemap,
      @app.sitemap.file_to_path(file),
      file
    )
  end
end

#readyObject



17
18
19
20
21
22
# File 'lib/middleman-core/sitemap/extensions/on_disk.rb', line 17

def ready
  @waiting_for_ready = false

  # Make sure the sitemap is ready for the first request
  app.sitemap.ensure_resource_list_updated!
end

#update_files(updated_files, removed_files) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/middleman-core/sitemap/extensions/on_disk.rb', line 40

def update_files(updated_files, removed_files)
  return if (updated_files + removed_files).all?(&method(:ignored?))

  # Rebuild the sitemap any time a file is touched
  # in case one of the other manipulators
  # (like asset_hash) cares about the contents of this file,
  # whether or not it belongs in the sitemap (like a partial)
  @app.sitemap.rebuild_resource_list!(:touched_file)

  # Force sitemap rebuild so the next request is ready to go.
  # Skip this during build because the builder will control sitemap refresh.
  @app.sitemap.ensure_resource_list_updated! unless @waiting_for_ready || @app.build?
end