class Imagecache::Backend::Filesystem

Public Class Methods

new(root = nil) click to toggle source
# File lib/imagecache/backend/filesystem.rb, line 9
def initialize(root = nil)
  @root = root || "#{Rails.root}/public"
end

Public Instance Methods

delete(key) click to toggle source
# File lib/imagecache/backend/filesystem.rb, line 23
def delete(key)
  keypath = path(key)
  File.unlink(keypath)
  rmdir(keypath)
end
exists?(key) click to toggle source
# File lib/imagecache/backend/filesystem.rb, line 29
def exists?(key)
  File.exist?(path(key))
end
get(key) click to toggle source
# File lib/imagecache/backend/filesystem.rb, line 13
def get(key)
  File.open(path(key)).read
end
set(key, value) click to toggle source
# File lib/imagecache/backend/filesystem.rb, line 17
def set(key, value)
  keypath = path(key)
  mkdir(keypath)
  File.open(keypath, 'wb') { |file| file.write(value) }
end

Private Instance Methods

mkdir(filepath) click to toggle source
# File lib/imagecache/backend/filesystem.rb, line 39
def mkdir(filepath)
  directories = File.dirname(filepath).split("/").drop(1)
  fullpath = @root
  directories.each do |directory|
    fullpath += "/#{directory}"
    if !Dir.exist?(fullpath)
      Dir.mkdir(fullpath)
    end
  end
end
path(key) click to toggle source
# File lib/imagecache/backend/filesystem.rb, line 35
def path(key)
  "#{@root}/#{key}"
end
rmdir(filepath) click to toggle source
# File lib/imagecache/backend/filesystem.rb, line 50
def rmdir(filepath)
  directories = File.dirname(filepath).split("/")
  while(directories.any?)
    fullpath = directories.join("/")
    if Dir["#{fullpath}/*"].empty?
      Dir.delete(fullpath)
    end
    directories.pop
  end
end