Class: RST::Persistent::DiskStore

Inherits:
Store
  • Object
show all
Defined in:
lib/modules/persistent/disk_store.rb

Overview

#DiskStore is responsible to save an Array of objects persistently to the disk - PStore is used to effort this.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Store

#-, #<<, #create, #find, #first

Constructor Details

#initialize(_filename = DEFAULT_STORE_FILENAME, args = {}) ⇒ DiskStore

The first argument is the filename Following args are passed to base-class

Examples:


DiskStore.new('myfile')
DiskStore.new('myfile', other_option: '...')
DiskStore.new( nil, other_option: '...') # use default filename

Parameters:

  • _filename (String) (defaults to: DEFAULT_STORE_FILENAME)
  • args (Array) (defaults to: {})
    • arguments passed to the super-class

See Also:

  • DEFAULT_STORE_FILENAME


25
26
27
28
# File 'lib/modules/persistent/disk_store.rb', line 25

def initialize(_filename=DEFAULT_STORE_FILENAME,args={})
  @filename = _filename
  super(args)
end

Instance Attribute Details

#filenameObject (readonly)

String

filename - used for PStore (no path! filename only)

See Also:

  • #store_path


13
14
15
# File 'lib/modules/persistent/disk_store.rb', line 13

def filename
  @filename
end

Instance Method Details

#allArray

TODO:

This is bad for performance and memory - refactor!

Returns:

  • (Array)


37
38
39
40
41
42
43
44
45
# File 'lib/modules/persistent/disk_store.rb', line 37

def all
  _all = []
  @store.transaction(true) do |s|
    s.roots.each do |_r|
      _all << s[_r]
    end
  end
  _all
end

#delete!Object

Delete the store



48
49
50
# File 'lib/modules/persistent/disk_store.rb', line 48

def delete!
  File.unlink(store_path)
end

#pathString

Returns full path of PStore-file.

Returns:

  • (String)

    full path of PStore-file



31
32
33
# File 'lib/modules/persistent/disk_store.rb', line 31

def path
  @store.path
end

#update(object) ⇒ Object

Find and update or add object to the store.

Parameters:

  • object (Object)


54
55
56
57
58
# File 'lib/modules/persistent/disk_store.rb', line 54

def update(object)
  @store.transaction do |s|
    s[object.id] = object
  end
end