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.

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

#==, _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:



84
85
86
87
88
89
# File 'lib/perobs/Object.rb', line 84

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.



78
79
80
# File 'lib/perobs/Object.rb', line 78

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.

Parameters:

  • attributes (Symbol)

    Name of the instance variable



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/perobs/Object.rb', line 54

def po_attr(*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 referenced to the given object ID.

Parameters:

  • id (Fixnum/Bignum)

    targeted object ID



156
157
158
159
160
161
162
163
164
# File 'lib/perobs/Object.rb', line 156

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.

Parameters:

  • data (Hash)

    attribute values hashed by their name



170
171
172
173
174
175
176
# File 'lib/perobs/Object.rb', line 170

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 Fixnum or Bignum

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

Returns:

  • (Array of Fixnum or Bignum)

    IDs of referenced objects



144
145
146
147
148
149
150
151
# File 'lib/perobs/Object.rb', line 144

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 object that later need to be collected again.



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/perobs/Object.rb', line 112

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.



97
98
99
100
101
102
103
104
# File 'lib/perobs/Object.rb', line 97

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)


180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/perobs/Object.rb', line 180

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!



137
138
139
# File 'lib/perobs/Object.rb', line 137

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