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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sitemap) ⇒ OnDisk

Returns a new instance of OnDisk.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/middleman-core/sitemap/extensions/on_disk.rb', line 14

def initialize(sitemap)
  @sitemap = sitemap
  @app     = @sitemap.app
  @file_paths_on_disk = Set.new

  scoped_self = self
  @waiting_for_ready = true

  # Register file change callback
  @app.files.changed do |file|
    scoped_self.touch_file(file)
  end

  # Register file delete callback
  @app.files.deleted do |file|
    scoped_self.remove_file(file)
  end

  @app.ready do
    scoped_self.waiting_for_ready = false
    # Make sure the sitemap is ready for the first request
    sitemap.ensure_resource_list_updated!
  end
end

Instance Attribute Details

#sitemapObject

Returns the value of attribute sitemap.



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

def sitemap
  @sitemap
end

#waiting_for_readyObject

Returns the value of attribute waiting_for_ready.



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

def waiting_for_ready
  @waiting_for_ready
end

Instance Method Details

#manipulate_resource_list(resources) ⇒ void

This method returns an undefined value.

Update the main sitemap resource list



83
84
85
86
87
88
89
90
91
# File 'lib/middleman-core/sitemap/extensions/on_disk.rb', line 83

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

#remove_file(file, rebuild = true) ⇒ void

This method returns an undefined value.

Remove a file from the store

Parameters:

  • file (String)


70
71
72
73
74
75
76
77
78
79
# File 'lib/middleman-core/sitemap/extensions/on_disk.rb', line 70

def remove_file(file, rebuild=true)
  if @file_paths_on_disk.delete?(file)
    @sitemap.rebuild_resource_list!(:removed_file)
    unless waiting_for_ready || @app.build?
      # Force sitemap rebuild so the next request is ready to go.
      # Skip this during build because the builder will control sitemap refresh.
      @sitemap.ensure_resource_list_updated!
    end
  end
end

#touch_file(file, rebuild = true) ⇒ Boolean

Update or add an on-disk file path

Parameters:

  • file (String)

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/middleman-core/sitemap/extensions/on_disk.rb', line 42

def touch_file(file, rebuild=true)
  return false if File.directory?(file)

  path = @sitemap.file_to_path(file)
  return false unless path

  ignored = @app.ignored_sitemap_matchers.any? do |name, callback|
    callback.call(file)
  end

  @file_paths_on_disk << file unless 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)
  @sitemap.rebuild_resource_list!(:touched_file)

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