Class: PEROBS::DataBase
- Inherits:
-
Object
- Object
- PEROBS::DataBase
- Defined in:
- lib/perobs/DataBase.rb
Overview
Base class for all storage back-ends.
Direct Known Subclasses
Instance Method Summary collapse
-
#check_option(name) ⇒ Object
Check a config option and adjust it if needed.
-
#close ⇒ Object
A dummy close method.
-
#deserialize(raw) ⇒ Hash
De-serialize the given String into a Ruby object.
-
#initialize(serializer = :json) ⇒ DataBase
constructor
Create a new DataBase object.
-
#open ⇒ Object
A dummy open method.
-
#serialize(obj) ⇒ String
Serialize the given object using the object serializer.
Constructor Details
#initialize(serializer = :json) ⇒ 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 |
# File 'lib/perobs/DataBase.rb', line 45 def initialize(serializer = :json) @serializer = serializer @config = {} end |
Instance Method Details
#check_option(name) ⇒ Object
Check a config option and adjust it if needed.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/perobs/DataBase.rb', line 100 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 ⇒ Object
A dummy close method. Deriving classes must overload them to insert their open/close semantics.
57 58 |
# File 'lib/perobs/DataBase.rb', line 57 def close end |
#deserialize(raw) ⇒ Hash
De-serialize the given String into a Ruby object.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/perobs/DataBase.rb', line 82 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. end end |
#open ⇒ Object
A dummy open method. Deriving classes must overload them to insert their open/close semantics.
52 53 |
# File 'lib/perobs/DataBase.rb', line 52 def open end |
#serialize(obj) ⇒ String
Serialize the given object using the object serializer.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/perobs/DataBase.rb', line 63 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. end end |