Class: Docks::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/docks/cache.rb

Constant Summary collapse

DIR =
"docks"
META_FILE =
"docks_meta"
PATTERN_LIBRARY_FILE =
"docks_pattern_library"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



65
66
67
68
69
70
# File 'lib/docks/cache.rb', line 65

def initialize
  FileUtils.mkdir_p(Docks.config.cache_location)
  @patterns_needing_caches = []
  
  load_pattern_library
end

Class Method Details

.cached?(group) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# 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

.pattern_for(pattern) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# 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) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
# 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_libraryObject



26
27
28
29
30
31
32
# 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

Instance Method Details

#<<(pattern) ⇒ Object



76
77
78
79
80
81
# File 'lib/docks/cache.rb', line 76

def <<(pattern)
  return unless pattern.valid?

  @patterns_needing_caches << pattern.name
  @pattern_library << pattern
end

#clearObject



72
73
74
# File 'lib/docks/cache.rb', line 72

def clear
  FileUtils.rm_rf Dir[Docks.config.cache_location + "*"]
end

#dumpObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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) ⇒ Object

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.



85
86
87
# File 'lib/docks/cache.rb', line 85

def no_update(pattern)
  @pattern_library << @old_pattern_library[pattern]
end