Class: Ccp::Persistent::Dir

Inherits:
Base
  • Object
show all
Defined in:
lib/ccp/persistent/dir.rb

Direct Known Subclasses

Tsv

Instance Attribute Summary

Attributes inherited from Base

#serializer, #source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#[], #initialize, #read, #read!, #save

Constructor Details

This class inherits a constructor from Ccp::Persistent::Base

Class Method Details

.extObject



4
5
6
# File 'lib/ccp/persistent/dir.rb', line 4

def self.ext
  ""
end

Instance Method Details

#[]=(key, val) ⇒ Object



27
28
29
# File 'lib/ccp/persistent/dir.rb', line 27

def []=(key, val)
  path_for(key).open("wb+"){|f| f.print encode(val)}
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/ccp/persistent/dir.rb', line 8

def exist?(key)
  path_for(key).exist?
end

#keysObject



31
32
33
# File 'lib/ccp/persistent/dir.rb', line 31

def keys
  Dir["#{path!}/*.#{ext}"].map{|i| File.basename(i, ".*")}.sort
end

#load(key) ⇒ Object



21
22
23
24
25
# File 'lib/ccp/persistent/dir.rb', line 21

def load(key)
  load!(key)
rescue Ccp::Persistent::NotFound
  nil
end

#load!(key) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/ccp/persistent/dir.rb', line 12

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

#pathObject



39
40
41
# File 'lib/ccp/persistent/dir.rb', line 39

def path
  @path ||= Pathname(@source)
end

#truncateObject



35
36
37
# File 'lib/ccp/persistent/dir.rb', line 35

def truncate
  Dir["#{path}/*.#{ext}"].each{|file| File.unlink(file)}
end