Class: PEROBS::DataBase

Inherits:
Object
  • Object
show all
Defined in:
lib/perobs/DataBase.rb

Overview

Base class for all storage back-ends.

Direct Known Subclasses

BTreeDB, DynamoDB, FlatFileDB

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DataBase

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



45
46
47
48
49
# File 'lib/perobs/DataBase.rb', line 45

def initialize(options)
  @serializer = options[:serializer] || :json
  @progressmeter = options[:progressmeter] || ProgressMeter.new
  @config = {}
end

Instance Method Details

#check_option(name) ⇒ Object

Check a config option and adjust it if needed.

Parameters:

  • name (String)

    Name of the config option.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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

#closeObject

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



58
59
# File 'lib/perobs/DataBase.rb', line 58

def close
end

#deserialize(raw) ⇒ Hash

De-serialize the given String into a Ruby object.

Parameters:

  • raw (String)

Returns:

  • (Hash)

    Deserialized version



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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

#openObject

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



53
54
# File 'lib/perobs/DataBase.rb', line 53

def open
end

#serialize(obj) ⇒ String

Serialize the given object using the object serializer.

Parameters:

Returns:

  • (String)

    Serialized version



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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