Class: Sass::Plugin::StalenessChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/sass/plugin/staleness_checker.rb

Overview

The class handles .s[ca]ss file staleness checks via their mtime timestamps.

To speed things up two level of caches are employed:

  • A class-level dependency cache which stores @import paths for each file. This is a long-lived cache that is reused by every StalenessChecker instance.
  • Two short-lived instance-level caches, one for file mtimes and one for whether a file is stale during this particular run. These are only used by a single StalenessChecker instance.

Usage:

  • For a one-off staleness check of a single .s[ca]ss file, the class-level StalenessChecker.stylesheet_needs_update? method should be used.
  • For a series of staleness checks (e.g. checking all files for staleness) a StalenessChecker instance should be created, and the instance-level #stylesheet_needs_update? method should be used. the caches should make the whole process significantly faster. WARNING: It is important not to retain the instance for too long, as its instance-level caches are never explicitly expired.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStalenessChecker

Creates a new StalenessChecker for checking the staleness of several stylesheets at once.



34
35
36
37
38
39
40
41
# File 'lib/sass/plugin/staleness_checker.rb', line 34

def initialize
  @dependencies = self.class.dependencies_cache

  # Entries in the following instance-level caches are never explicitly expired.
  # Instead they are supposed to automaticaly go out of scope when a series of staleness checks
  # (this instance of StalenessChecker was created for) is finished.
  @mtimes, @dependencies_stale = {}, {}
end

Class Method Details

.stylesheet_needs_update?(css_file, template_file) ⇒ Boolean

Returns whether or not a given CSS file is out of date and needs to be regenerated.

The distinction between this method and the instance-level #stylesheet_needs_update? is that the instance method preserves mtime and stale-dependency caches, so it's better to use when checking multiple stylesheets at once.

Parameters:

  • css_file (String)

    The location of the CSS file to check.

  • template_file (String)

    The location of the Sass or SCSS template that is compiled to css_file.

Returns:

  • (Boolean)


71
72
73
# File 'lib/sass/plugin/staleness_checker.rb', line 71

def self.stylesheet_needs_update?(css_file, template_file)
  new.stylesheet_needs_update?(css_file, template_file)
end

Instance Method Details

#stylesheet_needs_update?(css_file, template_file) ⇒ Boolean

Returns whether or not a given CSS file is out of date and needs to be regenerated.

Parameters:

  • css_file (String)

    The location of the CSS file to check.

  • template_file (String)

    The location of the Sass or SCSS template that is compiled to css_file.

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sass/plugin/staleness_checker.rb', line 49

def stylesheet_needs_update?(css_file, template_file)
  template_file = File.expand_path(template_file)

  unless File.exists?(css_file) && File.exists?(template_file)
    @dependencies.delete(template_file)
    true
  else
    css_mtime = mtime(css_file)
    mtime(template_file) > css_mtime || dependencies_stale?(template_file, css_mtime)
  end
end