Class: Moneta::Adapters::BasicFile

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

Instance Method Summary collapse

Methods included from Defaults

#[]=, #fetch

Constructor Details

#initialize(options = {}) ⇒ BasicFile

Returns a new instance of BasicFile.



25
26
27
28
29
30
# File 'lib/moneta/adapters/basic_file.rb', line 25

def initialize(options = {})
  @namespace = options[:namespace]
  @directory = ::File.join(options[:path], @namespace.to_s)

  ensure_directory_created(@directory)
end

Instance Method Details

#[](key) ⇒ Object



36
37
38
39
40
# File 'lib/moneta/adapters/basic_file.rb', line 36

def [](key)
  if ::File.exist?(path(key))
    raw_get(key)
  end
end

#clearObject



55
56
57
58
# File 'lib/moneta/adapters/basic_file.rb', line 55

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

#delete(key) ⇒ Object



49
50
51
52
53
# File 'lib/moneta/adapters/basic_file.rb', line 49

def delete(key, *)
  value = self[key]
  delete!(key)
  value
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/moneta/adapters/basic_file.rb', line 32

def key?(key, *)
  !self[key].nil?
end

#store(key, value) ⇒ Object



42
43
44
45
46
47
# File 'lib/moneta/adapters/basic_file.rb', line 42

def store(key, value, *)
  ensure_directory_created(::File.dirname(path(key)))
  ::File.open(path(key), "w") do |file|
    file.puts(serialize(value))
  end
end