class PEROBS::DataBase

Base class for all storage back-ends.

Public Class Methods

new(options) click to toggle source

Create a new DataBase object. This method must be overwritten by the deriving classes and then called via their constructor.

# File lib/perobs/DataBase.rb, line 44
def initialize(options)
  @serializer = options[:serializer] || :json
  @progressmeter = options[:progressmeter] || ProgressMeter.new
  @config = {}
  @class_map = nil
end

Public Instance Methods

check_option(name) click to toggle source

Check a config option and adjust it if needed. @param name [String] Name of the config option.

# File lib/perobs/DataBase.rb, line 108
def check_option(name)
  value = instance_variable_get('@' + name)

  if @config.include?(name)
    # The database already existed and has a setting for this config
    # option. If it does not match the instance variable, adjust the
    # instance variable accordingly.
    unless @config[name] == value
      instance_variable_set('@' + name, @config[name])
    end
  else
    # There is no such config option yet. Create it with the value of the
    # corresponding instance variable.
    @config[name] = value
  end
end
close() click to toggle source

A dummy close method. Deriving classes must overload them to insert their open/close semantics.

# File lib/perobs/DataBase.rb, line 63
def close
end
deserialize(raw) click to toggle source

De-serialize the given String into a Ruby object. @param raw [String] @return [Hash] Deserialized version

# File lib/perobs/DataBase.rb, line 88
def deserialize(raw)
  case @serializer
  when :marshal
    Marshal.load(raw)
  when :json
    JSON.parse(raw, create_additions: true)
  when :yaml
    if RUBY_VERSION < '3.2'
      YAML.load(raw)
    else
      YAML.load(raw, permitted_classes: [Symbol, POReference] + @class_map.classes)
    end
  end
rescue => e
  PEROBS.log.fatal "Cannot de-serialize object with #{@serializer} " +
    "parser: " + e.message
end
open() click to toggle source

A dummy open method. Deriving classes must overload them to insert their open/close semantics.

# File lib/perobs/DataBase.rb, line 58
def open
end
register_class_map(class_map) click to toggle source

Register the class map object needed to de-serialize objects.

# File lib/perobs/DataBase.rb, line 52
def register_class_map(class_map)
  @class_map = class_map
end
serialize(obj) click to toggle source

Serialize the given object using the object serializer. @param obj [ObjectBase] Object to serialize @return [String] Serialized version

# File lib/perobs/DataBase.rb, line 69
def serialize(obj)
  begin
    case @serializer
    when :marshal
      Marshal.dump(obj)
    when :json
      obj.to_json
    when :yaml
      YAML.dump(obj)
    end
  rescue => e
    PEROBS.log.fatal "Cannot serialize object as #{@serializer}: " +
      e.message
  end
end

Private Instance Methods

ensure_dir_exists(dir) click to toggle source

Ensure that we have a directory to store the DB items.

# File lib/perobs/DataBase.rb, line 128
def ensure_dir_exists(dir)
  unless Dir.exist?(dir)
    begin
      Dir.mkdir(dir)
    rescue IOError => e
      PEROBS.log.fatal "Cannote create DB directory '#{dir}': #{e.message}"
    end
  end
end