Class: PEROBS::Object
- Inherits:
-
ObjectBase
- Object
- ObjectBase
- PEROBS::Object
- Defined in:
- lib/perobs/Object.rb
Overview
The PEROBS::Object class is the base class for user-defined objects to be stored in the Store. It provides all the plumbing to define the class attributes and to transparently load and store the instances of the class in the database. You can use instance variables like normal instance variables unless they refer to other PEROBS objects. In these cases you must use the accessor methods for these instance variables. You must use accessor methods for any read and write operation to instance variables that hold or should hold PEROBS objects.
Class Attribute Summary collapse
-
.attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Attributes inherited from ObjectBase
Class Method Summary collapse
-
.po_attr(*attributes) ⇒ Object
This method can be used to define instance variable for PEROBS::Object derived classes.
Instance Method Summary collapse
-
#_delete_reference_to_id(id) ⇒ Object
This method should only be used during store repair operations.
-
#_deserialize(data) ⇒ Object
Restore the persistent data from a single data structure.
-
#_referenced_object_ids ⇒ Array of Fixnum or Bignum
Return a list of all object IDs that the attributes of this instance are referencing.
-
#init_attr(attr, val) ⇒ true|false
Initialize the specified attribute attr with the value val unless the attribute has been initialized already.
-
#initialize(store) ⇒ Object
constructor
New PEROBS objects must always be created by calling # Store.new().
-
#inspect ⇒ String
Textual dump for debugging purposes.
-
#mark_as_modified ⇒ Object
Call this method to manually mark the object as modified.
Methods inherited from ObjectBase
#==, #_change_id, _finalize, #_restore, #_stash, #_sync, #post_restore, read
Constructor Details
#initialize(store) ⇒ Object
New PEROBS objects must always be created by calling # Store.new(). PEROBS users should never call this method or equivalents of derived methods directly.
82 83 84 |
# File 'lib/perobs/Object.rb', line 82 def initialize(store) super end |
Class Attribute Details
.attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
48 49 50 |
# File 'lib/perobs/Object.rb', line 48 def attributes @attributes end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
77 78 79 |
# File 'lib/perobs/Object.rb', line 77 def attributes @attributes end |
Class Method Details
.po_attr(*attributes) ⇒ Object
This method can be used to define instance variable for PEROBS::Object derived classes.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/perobs/Object.rb', line 53 def po_attr(*attributes) attributes.each do |attr_name| unless attr_name.is_a?(Symbol) raise ArgumentError, "name must be a symbol but is a " + "#{attr_name.class}" end # Create the attribute reader method with name of attr_name. define_method(attr_name.to_s) do _get(attr_name) end # Create the attribute writer method with name of attr_name. define_method(attr_name.to_s + '=') do |val| _set(attr_name, val) end # Store a list of the attribute names @attributes ||= [] @attributes << attr_name unless @attributes.include?(attr_name) end end |
Instance Method Details
#_delete_reference_to_id(id) ⇒ Object
This method should only be used during store repair operations. It will delete all referenced to the given object ID.
132 133 134 135 136 137 138 139 140 |
# File 'lib/perobs/Object.rb', line 132 def _delete_reference_to_id(id) _all_attributes.each do |attr| ivar = ('@' + attr.to_s).to_sym value = instance_variable_get(ivar) if value && value.respond_to?(:is_poxreference?) && value.id == id instance_variable_set(ivar, nil) end end end |
#_deserialize(data) ⇒ Object
Restore the persistent data from a single data structure. This is a library internal method. Do not use outside of this library.
146 147 148 149 150 151 152 |
# File 'lib/perobs/Object.rb', line 146 def _deserialize(data) # Initialize all attributes with the provided values. data.each do |attr_name, value| value = POXReference.new(@store, value.id) if value.is_a?(POReference) instance_variable_set(('@' + attr_name).to_sym, value) end end |
#_referenced_object_ids ⇒ Array of Fixnum or Bignum
Return a list of all object IDs that the attributes of this instance are referencing.
120 121 122 123 124 125 126 127 |
# File 'lib/perobs/Object.rb', line 120 def _referenced_object_ids ids = [] _all_attributes.each do |attr| value = instance_variable_get(('@' + attr.to_s).to_sym) ids << value.id if value && value.respond_to?(:is_poxreference?) end ids end |
#init_attr(attr, val) ⇒ true|false
Initialize the specified attribute attr with the value val unless the attribute has been initialized already. Use this method in the class constructor to avoid overwriting values that have been set when the object was reconstructed from the store.
93 94 95 96 97 98 99 100 |
# File 'lib/perobs/Object.rb', line 93 def init_attr(attr, val) if _all_attributes.include?(attr) _set(attr, val) return true end false end |
#inspect ⇒ String
Textual dump for debugging purposes
156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/perobs/Object.rb', line 156 def inspect "{\n" + _all_attributes.map do |attr| ivar = ('@' + attr.to_s).to_sym if (value = instance_variable_get(ivar)).respond_to?('is_poxreference?') " #{attr}=>#{value.class}:#{value._id}" else " #{attr}=>#{value}" end end.join(",\n") + "\n}\n" end |
#mark_as_modified ⇒ Object
Call this method to manually mark the object as modified. This is necessary if you are using the ‘@’ notation to access instance variables during assignment operations (=, +=, -=, etc.). To avoid having to call this method you can use the self. notation.
@foo = 42 # faster but requires call to mark_as_modified()
self.foo = 42 # somewhat slower
IMPORTANT: If you use @foo = … and forget to call mark_as_modified() your data will only be modified in memory but might not be persisted into the database!
113 114 115 |
# File 'lib/perobs/Object.rb', line 113 def mark_as_modified @store.cache.cache_write(self) end |