class Docks::Cache

Constants

DIR
META_FILE
PATTERN_LIBRARY_FILE

Public Class Methods

cached?(group) click to toggle source
# File lib/docks/cache.rb, line 34
def self.cached?(group)
  group = Array(group)
  cache_modified = last_modified(Docks.pattern_id(group.first))
  !cache_modified.nil? && cache_modified > most_recent_modified_date(group)
end
new() click to toggle source
# File lib/docks/cache.rb, line 65
def initialize
  FileUtils.mkdir_p(Docks.config.cache_location)
  @patterns_needing_caches = []
  load_metadata
  load_pattern_library
end
pattern_for(pattern) click to toggle source
# File lib/docks/cache.rb, line 15
def self.pattern_for(pattern)
  pattern = pattern.to_s
  cache_file = File.join(Docks.config.cache_location, pattern)

  unless File.exists?(cache_file)
    raise Docks::NoPatternError, "No pattern by the name of '#{pattern}' exists. Make sure you have a script, markup, or style file with that filename that is included in your 'docks_config' source directories."
  end

  Marshal::load(File.binread(cache_file))
end
pattern_for?(pattern) click to toggle source
# File lib/docks/cache.rb, line 10
def self.pattern_for?(pattern)
  cache_file = File.join(Docks.config.cache_location, pattern.to_s)
  File.exist?(cache_file)
end
pattern_library() click to toggle source
# File lib/docks/cache.rb, line 26
def self.pattern_library
  if File.exists?(pattern_library_cache_file)
    Marshal::load(File.binread(pattern_library_cache_file)) || Containers::PatternLibrary.new
  else
    Containers::PatternLibrary.new
  end
end

Private Class Methods

last_modified(pattern) click to toggle source
# File lib/docks/cache.rb, line 42
def self.last_modified(pattern)
  cache_file = Docks.config.cache_location + pattern.to_s
  return nil unless File.exist?(cache_file)
  File.mtime(cache_file)
end
load_pattern_cache(&block) click to toggle source
# File lib/docks/cache.rb, line 114
def self.load_pattern_cache(&block)
  patterns = {}

  return unless File.exists?(group_cache_file)
  pattern_cache = Marshal::load(File.binread(group_cache_file))

  if block_given?
    pattern_cache.each_value(&block)
  end

  nil
end
meta_file() click to toggle source
# File lib/docks/cache.rb, line 59
def self.meta_file
  Docks.config.cache_location + META_FILE
end
most_recent_modified_date(files) click to toggle source
# File lib/docks/cache.rb, line 48
def self.most_recent_modified_date(files)
  Array(files).select { |file| File.exist?(file) }
              .map { |file| File.mtime(file) }
              .sort
              .last
end
pattern_library_cache_file() click to toggle source
# File lib/docks/cache.rb, line 55
def self.pattern_library_cache_file
  Docks.config.cache_location + PATTERN_LIBRARY_FILE
end

Public Instance Methods

<<(pattern) click to toggle source
# File lib/docks/cache.rb, line 76
def <<(pattern)
  return unless pattern.valid?

  @patterns_needing_caches << pattern.name
  @pattern_library << pattern
end
clear() click to toggle source
# File lib/docks/cache.rb, line 72
def clear
  FileUtils.rm_rf Dir[Docks.config.cache_location + "*"]
end
dump() click to toggle source
# File lib/docks/cache.rb, line 89
def dump
  Process.process(@pattern_library)

  File.open(self.class.pattern_library_cache_file, "wb") do |file|
    file.write Marshal::dump(@pattern_library)
  end

  File.open(self.class.meta_file, "wb") do |file|
    file.write Marshal::dump(@metadata)
  end

  @patterns_needing_caches.each do |pattern|
    File.open(Docks.config.cache_location + pattern, "wb") do |file|
      file.write Marshal::dump(@pattern_library[pattern])
    end
  end

  # Clear out anything that didn't get written to the new cache
  @old_pattern_library.patterns.each do |name, pattern|
    FileUtils.rm_rf(Docks.config.cache_location + name) unless @pattern_library.has_pattern?(name)
  end
end
no_update(pattern) click to toggle source

When a pattern exists but did not need to be re-parsed, this needs to be communicated so that we can keep track of what patterns still exist.

# File lib/docks/cache.rb, line 85
def no_update(pattern)
  @pattern_library << @old_pattern_library[pattern]
end

Private Instance Methods

load_metadata() click to toggle source
# File lib/docks/cache.rb, line 127
def load_metadata
  if File.exists?(self.class.meta_file)
    @metadata = Marshal::load(File.binread(self.class.meta_file)) || Hash.new
  else
    @metadata = Hash.new
  end

  clear if !@metadata[:version].nil? && @metadata[:version] != VERSION
  @metadata[:version] = VERSION
end
load_pattern_library() click to toggle source
# File lib/docks/cache.rb, line 138
def load_pattern_library
  @old_pattern_library = self.class.pattern_library
  @pattern_library = Containers::PatternLibrary.new
end