Class: PEROBS::Object

Inherits:
ObjectBase show all
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.

Constant Summary

Constants inherited from ObjectBase

PEROBS::ObjectBase::NATIVE_CLASSES

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from ObjectBase

#_id, #myself, #store

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ObjectBase

#==, #_check_assignment_value, _finalize, #_initialize, #_restore, #_stash, #_sync, #_transfer, read, #restore

Constructor Details

#initialize(p) ⇒ 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.

Parameters:



89
90
91
92
93
94
# 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

Class Attribute Details

.attributesObject (readonly)

Returns the value of attribute attributes.



49
50
51
# File 'lib/perobs/Object.rb', line 49

def attributes
  @attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



83
84
85
# File 'lib/perobs/Object.rb', line 83

def attributes
  @attributes
end

Class Method Details

.attr_persist(*attributes) ⇒ Object Also known as: po_attr

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.

Parameters:

  • attributes (Symbol)

    Name of the instance variable



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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

Instance Method Details

#_delete_reference_to_id(id) ⇒ Object

This method should only be used during store repair operations. It will delete all references to the given object ID.

Parameters:

  • id (Integer)

    targeted object ID



161
162
163
164
165
166
167
168
169
170
# 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

#_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.

Parameters:

  • data (Hash)

    attribute values hashed by their name



176
177
178
179
180
181
182
# 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

#_referenced_object_idsArray of Integer

Return a list of all object IDs that the attributes of this instance are referencing.

Returns:

  • (Array of Integer)

    IDs of referenced objects



149
150
151
152
153
154
155
156
# 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

#attr_init(attr, val = nil, &block) ⇒ Object

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.



117
118
119
120
121
122
123
124
125
126
127
128
129
# 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

#init_attr(attr, val) ⇒ true|false

This method is deprecated. It will be removed in future versions. Please use attr_init() instead. the database.

Parameters:

  • attr (Symbol)

    Name of the attribute

  • val (Any)

    Value to be set

Returns:

  • (true|false)

    True if the value was initialized, otherwise false.



102
103
104
105
106
107
108
109
# 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

#inspectString

Textual dump for debugging purposes

Returns:

  • (String)


186
187
188
189
190
191
192
193
194
195
196
197
# 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

#mark_as_modifiedObject

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!



142
143
144
# File 'lib/perobs/Object.rb', line 142

def mark_as_modified
  @store.cache.cache_write(self)
end