Class: Moneta::Adapters::File

Inherits:
Object
  • Object
show all
Includes:
Defaults
Defined in:
lib/moneta/adapters/file.rb

Instance Method Summary collapse

Methods included from Defaults

#[]=, #fetch

Constructor Details

#initialize(options = {}) ⇒ File

Returns a new instance of File.



8
9
10
11
12
13
14
15
# File 'lib/moneta/adapters/file.rb', line 8

def initialize(options = {})
  @directory = options[:path]
  if ::File.file?(@directory)
    raise StandardError, "The path you supplied #{@directory} is a file"
  elsif !::File.exists?(@directory)
    FileUtils.mkdir_p(@directory)
  end
end

Instance Method Details

#[](key) ⇒ Object



21
22
23
24
25
# File 'lib/moneta/adapters/file.rb', line 21

def [](key)
  if ::File.exist?(path(key))
    Marshal.load(::File.read(path(key)))
  end
end

#clearObject



41
42
43
44
# File 'lib/moneta/adapters/file.rb', line 41

def clear(*)
  FileUtils.rm_rf(@directory)
  FileUtils.mkdir(@directory)
end

#delete(key) ⇒ Object



34
35
36
37
38
39
# File 'lib/moneta/adapters/file.rb', line 34

def delete(key, *)
  value = self[key]
  FileUtils.rm(path(key))
  value
rescue Errno::ENOENT
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/moneta/adapters/file.rb', line 17

def key?(key, *)
  ::File.exist?(path(key))
end

#store(key, value) ⇒ Object



27
28
29
30
31
32
# File 'lib/moneta/adapters/file.rb', line 27

def store(key, value, *)
  ::File.open(path(key), "w") do |file|
    contents = Marshal.dump(value)
    file.puts(contents)
  end
end