class Ccp::Persistent::File

Public Class Methods

ext() click to toggle source
# File lib/ccp/persistent/file.rb, line 3
def self.ext
  ""
end
new(source, serializer) click to toggle source
# File lib/ccp/persistent/file.rb, line 7
def initialize(source, serializer)
  @serializer = Ccp::Serializers.lookup(serializer)
  @source     = File.extname(source) == ".#{ext}" ? source : "#{source}.#{ext}"
end

Public Instance Methods

[]=(key, val) click to toggle source
# File lib/ccp/persistent/file.rb, line 31
def []=(key, val)
  hash = read
  hash[key.to_s] = val
  raw_write(encode(hash))
end
exist?(key) click to toggle source
# File lib/ccp/persistent/file.rb, line 12
def exist?(key)
  read.has_key?(key.to_s)
end
keys() click to toggle source
# File lib/ccp/persistent/file.rb, line 37
def keys
  read!.keys.sort
end
load(key) click to toggle source
# File lib/ccp/persistent/file.rb, line 25
def load(key)
  load!(key)
rescue Ccp::Persistent::NotFound
  nil
end
load!(key) click to toggle source
# File lib/ccp/persistent/file.rb, line 16
def load!(key)
  hash = read
  if hash.has_key?(key.to_s)
    hash[key.to_s]
  else
    raise Ccp::Persistent::NotFound, key.to_s
  end
end
path() click to toggle source
# File lib/ccp/persistent/file.rb, line 45
def path
  @path ||= Pathname(@source)
end
read() click to toggle source
# File lib/ccp/persistent/file.rb, line 49
def read
  read!
rescue Ccp::Persistent::NotFound
  {}
end
read!() click to toggle source
# File lib/ccp/persistent/file.rb, line 55
def read!
  path.exist? or raise Ccp::Persistent::NotFound, path.to_s
  decode(path.open("rb").read{}).must(Hash)
end
truncate() click to toggle source
# File lib/ccp/persistent/file.rb, line 41
def truncate
  File.unlink(path.to_s)
end

Private Instance Methods

raw_write(buf) click to toggle source
# File lib/ccp/persistent/file.rb, line 61
def raw_write(buf)
  path.parent.mkpath
  path.open("wb+"){|f| f.print buf}
end