Class: Juno::Adapters::File
- Inherits:
-
Base
- Object
- Base
- Juno::Adapters::File
show all
- Defined in:
- lib/juno/adapters/file.rb
Overview
Instance Method Summary
collapse
Methods inherited from Base
#[], #[]=, #close, #fetch
Constructor Details
#initialize(options = {}) ⇒ File
Returns a new instance of File.
8
9
10
11
12
|
# File 'lib/juno/adapters/file.rb', line 8
def initialize(options = {})
raise ArgumentError, 'Option :dir is required' unless @dir = options[:dir]
FileUtils.mkpath(@dir)
raise "#{@dir} is not a directory" unless ::File.directory?(@dir)
end
|
Instance Method Details
#clear(options = {}) ⇒ Object
43
44
45
46
47
48
49
50
51
|
# File 'lib/juno/adapters/file.rb', line 43
def clear(options = {})
temp_dir = "#{@dir}-#{$$}-#{Thread.current.object_id}"
::File.rename(@dir, temp_dir)
FileUtils.mkpath(@dir)
FileUtils.rm_rf(temp_dir)
self
rescue Errno::ENOENT
self
end
|
#delete(key, options = {}) ⇒ Object
36
37
38
39
40
41
|
# File 'lib/juno/adapters/file.rb', line 36
def delete(key, options = {})
value = load(key, options)
::File.unlink(store_path(key))
value
rescue Errno::ENOENT
end
|
#key?(key, options = {}) ⇒ Boolean
14
15
16
|
# File 'lib/juno/adapters/file.rb', line 14
def key?(key, options = {})
::File.exist?(store_path(key))
end
|
#load(key, options = {}) ⇒ Object
18
19
20
21
|
# File 'lib/juno/adapters/file.rb', line 18
def load(key, options = {})
::File.read(store_path(key))
rescue Errno::ENOENT
end
|
#store(key, value, options = {}) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/juno/adapters/file.rb', line 23
def store(key, value, options = {})
path = store_path(key)
temp_file = ::File.join(@dir, "value-#{$$}-#{Thread.current.object_id}")
FileUtils.mkpath(::File.dirname(path))
::File.open(temp_file, 'wb') {|file| file.write(value) }
::File.unlink(path) if ::File.exist?(path)
::File.rename(temp_file, path)
value
rescue Errno::ENOENT
::File.unlink(temp_file) rescue nil
value
end
|