Class: Ccp::Persistent::Dir
- Inherits:
-
Base
- Object
- Base
- Ccp::Persistent::Dir
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
Class Method Details
.ext ⇒ Object
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
8
9
10
|
# File 'lib/ccp/persistent/dir.rb', line 8
def exist?(key)
path_for(key).exist?
end
|
#keys ⇒ Object
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
|
#path ⇒ Object
39
40
41
|
# File 'lib/ccp/persistent/dir.rb', line 39
def path
@path ||= Pathname(@source)
end
|
#truncate ⇒ Object
35
36
37
|
# File 'lib/ccp/persistent/dir.rb', line 35
def truncate
Dir["#{path}/*.#{ext}"].each{|file| File.unlink(file)}
end
|