Class: Moneta::Adapters::YAML

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

Instance Method Summary collapse

Methods included from Defaults

#[]=, #fetch

Constructor Details

#initialize(options = {}) ⇒ YAML

Returns a new instance of YAML.



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

def initialize(options = {})
  @file = ::File.expand_path(options[:path])
  unless ::File.exists?(@file)
    ::File.open(@file, "w") { |file| file << {}.to_yaml }
  end
end

Instance Method Details

#[](key) ⇒ Object



20
21
22
23
# File 'lib/moneta/adapters/yaml.rb', line 20

def [](key)
  string_key = key_for(key)
  yaml[string_key]['value'] if yaml.key?(string_key)
end

#clearObject



39
40
41
# File 'lib/moneta/adapters/yaml.rb', line 39

def clear(*)
  save
end

#delete(key) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/moneta/adapters/yaml.rb', line 31

def delete(key, *)
  hash = yaml
  value = self[key_for(key)]
  hash.delete(key_for(key))
  save(hash)
  value
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/moneta/adapters/yaml.rb', line 16

def key?(key, *)
  yaml.has_key?(key_for(key))
end

#store(key, value) ⇒ Object



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

def store(key, value, *)
  hash = yaml
  (hash[key_for(key)] ||= {})['value'] = value
  save(hash)
end