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 45
def initialize(options)
  @serializer = options[:serializer] || :json
  @progressmeter = options[:progressmeter] || ProgressMeter.new
  @config = {}
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 101
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 58
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 83
def deserialize(raw)
  begin
    case @serializer
    when :marshal
      Marshal.load(raw)
    when :json
      JSON.parse(raw, :create_additions => true)
    when :yaml
      YAML.load(raw)
    end
  rescue => e
    PEROBS.log.fatal "Cannot de-serialize object with #{@serializer} " +
      "parser: " + e.message
  end
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 53
def open
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 64
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 121
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