class MotionBlender::Analyzer::Cache

Attributes

file[R]
hit[R]
hit?[R]

Public Class Methods

new(file) click to toggle source
# File lib/motion_blender/analyzer/cache.rb, line 7
def initialize file
  @file = Pathname.new(file)
end

Public Instance Methods

cache_file() click to toggle source
# File lib/motion_blender/analyzer/cache.rb, line 52
def cache_file
  @cache_file ||=
    begin
      path = @file
      path = path.relative_path_from(Pathname.new('/')) if path.absolute?
      dir.join(path.to_s + '.yml')
    end
end
delete() click to toggle source
# File lib/motion_blender/analyzer/cache.rb, line 44
def delete
  cache_file.exist? && cache_file.delete
end
dir() click to toggle source
# File lib/motion_blender/analyzer/cache.rb, line 11
def dir
  MotionBlender.config.cache_dir
end
fetch() { || ... } click to toggle source
# File lib/motion_blender/analyzer/cache.rb, line 15
def fetch
  return yield unless dir

  @hit = valid?
  if @hit
    read
  else
    content = yield
    write content
  end
rescue YAML::Exception
  retry if delete
  raise
end
read() click to toggle source
# File lib/motion_blender/analyzer/cache.rb, line 30
def read
  YAML.load_file cache_file
end
valid?() click to toggle source
# File lib/motion_blender/analyzer/cache.rb, line 48
def valid?
  cache_file.file? && @file.mtime < cache_file.mtime
end
write(content) click to toggle source
# File lib/motion_blender/analyzer/cache.rb, line 34
def write content
  if content.nil?
    delete
  else
    cache_file.dirname.mkpath
    cache_file.write YAML.dump(content)
  end
  content
end