Class: Nestene::DiskStorage
- Inherits:
-
Object
- Object
- Nestene::DiskStorage
- Defined in:
- lib/nestene/storage.rb
Instance Method Summary collapse
- #delete(key) ⇒ Object
-
#initialize(dir) ⇒ DiskStorage
constructor
A new instance of DiskStorage.
- #list ⇒ Object
- #load(key) ⇒ Object
- #store(key, value) ⇒ Object
Constructor Details
#initialize(dir) ⇒ DiskStorage
Returns a new instance of DiskStorage.
20 21 22 23 |
# File 'lib/nestene/storage.rb', line 20 def initialize(dir) @dir=dir @converter = FileNameConverter.new end |
Instance Method Details
#delete(key) ⇒ Object
56 57 58 59 |
# File 'lib/nestene/storage.rb', line 56 def delete(key) file_name = "%s/%s" % [@dir,@converter.key_to_file_name(key)] File.unlink(file_name) end |
#list ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/nestene/storage.rb', line 25 def list entries = Dir.entries(@dir) entries.delete('.') entries.delete('..') entries.map!{|fn| @converter.file_name_to_key(fn)} entries.delete('__credentials__') entries end |
#load(key) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/nestene/storage.rb', line 47 def load(key) file_name = "%s/%s" % [@dir,@converter.key_to_file_name(key)] if File.exist?(file_name) File.open(file_name,"r"){|f| JSON.parse(f.read)} else nil end end |
#store(key, value) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/nestene/storage.rb', line 34 def store(key, value) if value != nil final_name = "%s/%s" % [@dir,@converter.key_to_file_name(key)] tmp_name = "%s.temp" % final_name File.open(tmp_name,"w") do |f| f.write(JSON.pretty_generate(value)) end File.rename(tmp_name, final_name) else delete(key) end end |