Module: Octopress::Ink::Cache

Extended by:
Cache
Included in:
Cache
Defined in:
lib/octopress-ink/cache.rb

Constant Summary collapse

INK_CACHE_DIR =
'.ink-cache'

Instance Method Summary collapse

Instance Method Details

#cache_label(asset) ⇒ Object



26
27
28
# File 'lib/octopress-ink/cache.rb', line 26

def cache_label(asset)
  "#{asset.plugin.slug}-#{File.basename(asset.file, '.*').downcase}"
end

#cleanObject



43
44
45
46
47
48
49
50
51
# File 'lib/octopress-ink/cache.rb', line 43

def clean
  if File.directory?(INK_CACHE_DIR)
    remove = Find.find(INK_CACHE_DIR).to_a.reject do |file|
      @cache_files.include?(file) || File.directory?(file)
    end

    FileUtils.rm(remove)
  end
end

#get_cache_path(dir, label, str) ⇒ Object



30
31
32
# File 'lib/octopress-ink/cache.rb', line 30

def get_cache_path(dir, label, str)
  File.join(dir, ".#{label}#{Digest::MD5.hexdigest(str)}.js")
end

#read_cache(asset, options) ⇒ Object



13
14
15
16
17
# File 'lib/octopress-ink/cache.rb', line 13

def read_cache(asset, options)
  path = get_cache_path(INK_CACHE_DIR, cache_label(asset), options.to_s << asset.content)
  @cache_files << path
  File.exist?(path) ? File.read(path) : nil unless path.nil?
end

#resetObject



8
9
10
11
# File 'lib/octopress-ink/cache.rb', line 8

def reset
  @cache_files = []
  @write_cache = {}
end

#writeObject



34
35
36
37
38
39
40
41
# File 'lib/octopress-ink/cache.rb', line 34

def write
  @write_cache.each do |path, contents|
    File.open(path, 'w') do |f|
      f.print(contents)
    end
  end
  @write_cache = {}
end

#write_to_cache(asset, content, options) ⇒ Object



19
20
21
22
23
24
# File 'lib/octopress-ink/cache.rb', line 19

def write_to_cache(asset, content, options)
  FileUtils.mkdir_p(INK_CACHE_DIR) unless File.directory?(INK_CACHE_DIR)
  path = get_cache_path(INK_CACHE_DIR, cache_label(asset), options.to_s << asset.content)
  @write_cache[path] = content
  content
end