class Ccp::Persistent::Dir

Public Class Methods

ext() click to toggle source
# File lib/ccp/persistent/dir.rb, line 3
def self.ext
  ""
end

Public Instance Methods

[]=(key, val) click to toggle source
# File lib/ccp/persistent/dir.rb, line 26
def []=(key, val)
  path_for(key).open("wb+"){|f| f.print encode(val)}
end
exist?(key) click to toggle source
# File lib/ccp/persistent/dir.rb, line 7
def exist?(key)
  path_for(key).exist?
end
keys() click to toggle source
# File lib/ccp/persistent/dir.rb, line 30
def keys
  Dir["#{path!}/*.#{ext}"].map{|i| File.basename(i, ".*")}.sort
end
load(key) click to toggle source
# File lib/ccp/persistent/dir.rb, line 20
def load(key)
  load!(key)
rescue Ccp::Persistent::NotFound
  nil
end
load!(key) click to toggle source
# File lib/ccp/persistent/dir.rb, line 11
def load!(key)
  path = path_for(key)
  if path.exist?
    decode(path.open("rb").read{})
  else
    raise Ccp::Persistent::NotFound, key.to_s
  end
end
path() click to toggle source
# File lib/ccp/persistent/dir.rb, line 38
def path
  @path ||= Pathname(@source)
end
truncate() click to toggle source
# File lib/ccp/persistent/dir.rb, line 34
def truncate
  Dir["#{path}/*.#{ext}"].each{|file| File.unlink(file)}
end

Private Instance Methods

path!() click to toggle source
# File lib/ccp/persistent/dir.rb, line 43
def path!
  if path.exist?
    path
  else
    raise Ccp::Persistent::NotFound, path.to_s
  end
end
path_for(key, mkdir = true) click to toggle source
# File lib/ccp/persistent/dir.rb, line 51
def path_for(key, mkdir = true)
  path.mkpath if mkdir
  path + "#{key}.#{ext}"
end