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(options) ⇒ 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(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() @serializer = [:serializer] || :json @progressmeter = [:progressmeter] || ProgressMeter.new @config = {} end |
Instance Method Details
#check_option(name) ⇒ Object
Check a config option and adjust it if needed.
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 |
#close ⇒ Object
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.
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. end end |
#open ⇒ Object
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.
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. end end |