class PEROBS::Object
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.
Attributes
Public Class Methods
This method can be used to define instance variable for PEROBS::Object
derived classes. Persistent attributes always have getter and setter methods defined. So it’s essentially equivalent to attr_accessor but additionally declares an attribute as persistent. @param attributes [Symbol] Name of the instance variable
# File lib/perobs/Object.rb, line 56 def attr_persist(*attributes) attributes.each do |attr_name| unless attr_name.is_a?(Symbol) PEROBS.log.fatal "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
New PEROBS
objects must always be created by calling # Store.new()
. PEROBS
users should never call this method or equivalents of derived methods directly. @param p [PEROBS::Handle] PEROBS
handle
# File lib/perobs/Object.rb, line 89 def initialize(p) super(p) # Ensure that the newly created object will be pushed into the database. @store.cache.cache_write(self) end
This is the deprecated name for the attr_persist
method
Public Instance Methods
This method should only be used during store repair operations. It will delete all references to the given object ID. @param id [Integer] targeted object ID
# File lib/perobs/Object.rb, line 161 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 mark_as_modified end
Restore the persistent data from a single data structure. This is a library internal method. Do not use outside of this library. @param data [Hash] attribute values hashed by their name @private
# File lib/perobs/Object.rb, line 176 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
Return a list of all object IDs that the attributes of this instance are referencing. @return [Array of Integer] IDs of referenced objects
# File lib/perobs/Object.rb, line 149 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
Use this method to initialize persistent attributes in the restore() method that have not yet been initialized. This is the case when the object was saved with an earlier version of the program that did not yet have the instance variable. If you want to assign another PEROBS
object to the variable you should use the block variant to avoid unnecessary creation of PEROBS
objects that later need to be collected again.
# File lib/perobs/Object.rb, line 117 def attr_init(attr, val = nil, &block) if _all_attributes.include?(attr) unless instance_variable_defined?('@' + attr.to_s) _set(attr, block_given? ? yield : val) end return true else PEROBS.log.fatal "'#{attr}' is not a defined persistent " + "attribute of class #{self.class}" end false end
This method is deprecated. It will be removed in future versions. Please use attr_init
() instead. the database. @param attr [Symbol] Name of the attribute @param val [Any] Value to be set @return [true|false] True if the value was initialized, otherwise false.
# File lib/perobs/Object.rb, line 102 def init_attr(attr, val) if _all_attributes.include?(attr) _set(attr, val) return true end false end
Textual dump for debugging purposes @return [String]
# File lib/perobs/Object.rb, line 186 def inspect "<#{self.class}:#{@_id}>\n{\n" + _all_attributes.map do |attr| ivar = ('@' + attr.to_s).to_sym if (value = instance_variable_get(ivar)).respond_to?(:is_poxreference?) " #{attr} => <PEROBS::ObjectBase:#{value._id}>" else " #{attr} => #{value.inspect}" end end.join(",\n") + "\n}\n" end
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!
# File lib/perobs/Object.rb, line 142 def mark_as_modified @store.cache.cache_write(self) end
Private Instance Methods
# File lib/perobs/Object.rb, line 228 def _all_attributes # Collect all persistent attributes from this class and all # super classes into a single Array. attributes = [] klass = self.class while klass && klass.respond_to?(:attributes) if (attrs = klass.attributes) attributes += attrs end klass = klass.superclass end # PEROBS objects that don't have persistent attributes declared don't # really make sense. if attributes.empty? PEROBS.log.fatal "No persistent attributes have been declared for " + "class #{self.class} or any parent class. Use 'attr_persist' " + "to declare them." end attributes end
# File lib/perobs/Object.rb, line 224 def _get(attr) instance_variable_get(('@' + attr.to_s).to_sym) end
Return a single data structure that holds all persistent data for this class.
# File lib/perobs/Object.rb, line 203 def _serialize attributes = {} _all_attributes.each do |attr| ivar = ('@' + attr.to_s).to_sym value = instance_variable_get(ivar) attributes[attr.to_s] = value.respond_to?(:is_poxreference?) ? POReference.new(value.id) : value end attributes end
# File lib/perobs/Object.rb, line 214 def _set(attr, val) _check_assignment_value(val) instance_variable_set(('@' + attr.to_s).to_sym, val) # Let the store know that we have a modified object. If we restored the # object from the DB, we don't mark it as modified. mark_as_modified val end