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.



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

def initialize(options)
  @serializer = options[:serializer] || :json
  @progressmeter = options[:progressmeter] || ProgressMeter.new
  @config = {}
  @class_map = nil
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.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# 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

#closeObject

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



63
64
# File 'lib/perobs/DataBase.rb', line 63

def close
end

#deserialize(raw) ⇒ Hash

De-serialize the given String into a Ruby object.

Parameters:

  • raw (String)

Returns:

  • (Hash)

    Deserialized version



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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

#openObject

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



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

def open
end

#register_class_map(class_map) ⇒ Object

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



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

def register_class_map(class_map)
  @class_map = class_map
end

#serialize(obj) ⇒ String

Serialize the given object using the object serializer.

Parameters:

Returns:

  • (String)

    Serialized version



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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