Class: Anemone::Storage::PStore

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/anemone/storage/pstore.rb

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ PStore

Returns a new instance of PStore.



11
12
13
14
15
# File 'lib/anemone/storage/pstore.rb', line 11

def initialize(file)
  File.delete(file) if File.exists?(file)
  @store = ::PStore.new(file)
  @keys = {}
end

Instance Method Details

#[](key) ⇒ Object



17
18
19
# File 'lib/anemone/storage/pstore.rb', line 17

def [](key)
  @store.transaction { |s| s[key] }
end

#[]=(key, value) ⇒ Object



21
22
23
24
# File 'lib/anemone/storage/pstore.rb', line 21

def []=(key,value)
  @keys[key] = nil
  @store.transaction { |s| s[key] = value }
end

#delete(key) ⇒ Object



26
27
28
29
# File 'lib/anemone/storage/pstore.rb', line 26

def delete(key)
  @keys.delete(key)
  @store.transaction { |s| s.delete key}
end

#eachObject



31
32
33
34
35
36
37
# File 'lib/anemone/storage/pstore.rb', line 31

def each
  @keys.each_key do |key|
    value = nil
    @store.transaction { |s| value = s[key] }
    yield key, value
  end
end

#merge!(hash) ⇒ Object



39
40
41
42
43
44
# File 'lib/anemone/storage/pstore.rb', line 39

def merge!(hash)
  @store.transaction do |s|
    hash.each { |key, value| s[key] = value; @keys[key] = nil }
  end
  self
end