class RST::Persistent::DiskStore

DiskStore is responsible to save an Array of objects persistently to the disk - PStore is used to effort this. @see RST::Persistent::Store @see Mutex @api persistent

Attributes

filename[R]
String

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

@see store_path

Public Class Methods

new(_filename=DEFAULT_STORE_FILENAME,args={}) click to toggle source

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

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

@see DEFAULT_STORE_FILENAME @param [String] _filename @param [Array] args - arguments passed to the super-class

Calls superclass method
# File lib/modules/persistent/disk_store.rb, line 25
def initialize(_filename=DEFAULT_STORE_FILENAME,args={})
  @filename = _filename
  super(args)
end

Public Instance Methods

all() click to toggle source

@todo This is bad for performance and memory - refactor! @return [Array]

# 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!() click to toggle source

Delete the store

# File lib/modules/persistent/disk_store.rb, line 48
def delete!
  File.unlink(store_path)
end
path() click to toggle source

@return [String] full path of PStore-file

# File lib/modules/persistent/disk_store.rb, line 31
def path
  @store.path
end
update(object) click to toggle source

Find and update or add object to the store. @param [Object] object

# File lib/modules/persistent/disk_store.rb, line 54
def update(object)
  @store.transaction do |s|
    s[object.id] = object
  end
end

Private Instance Methods

build_store_path() click to toggle source

build the path from ENV-vars and create the directory if necessary @return [String] full path of data-file for current environment

# File lib/modules/persistent/disk_store.rb, line 95
def build_store_path
  prefix = ENV['RST_DATA'] || RST::STOREPATH
  env = ENV['RST_ENV'] || 'development'
  _dir = File.join( prefix, env )
  FileUtils.mkdir_p(_dir)
  File.join(_dir, filename)
end
remove_object(object) click to toggle source

@param [Object] object - the object to be removed.

# File lib/modules/persistent/disk_store.rb, line 63
def remove_object(object)
  @store.transaction do |s|
    s.delete(object.id)
  end
end
setup_backend() click to toggle source

Initialize a PStore-instance @see store_path

# File lib/modules/persistent/disk_store.rb, line 71
def setup_backend
  @store = PStore.new(store_path)
end
store_path() click to toggle source

Determine the store-path from RST::STOREPATH and environment. Creates the path if not exists.

@example

RST_ENV=development
RST_DATA=$HOME/.rst-data

~/.rst-data/development/store.data

if no environment-variables defined, the defaults will be STOREPATH and ‘development’ @see STOREPATH @return [String]

# File lib/modules/persistent/disk_store.rb, line 88
def store_path
  @store_path ||= build_store_path
end