Module: Aqua::Unpack::InstanceMethods
Public Visibility
Public Instance Method Summary
#_clear_store |
Makes @_store nil to converve on memory. |
---|---|
#_get_store |
Retrieves objects storage from its engine. Returns: Aqua::Storage |
#_reload |
Actual mechanism for reloading an object from stored data. Returns: Object |
#_unpack |
Unpacks an object from hash representation of data and metadata. Returns: Aqua::Storage |
#_unpack_array(obj) |
Unpacks an Array. Returns: Object |
#_unpack_hash(obj) |
Unpacks a Hash. Returns: Object |
#_unpack_initialization(obj) |
Unpacks an the initialization object from a hash into a real object. Returns: Object |
#_unpack_ivars(obj, data) |
Unpacks an object’s instance variables. |
#_unpack_object(store_pack) |
The real workhorse behind object construction: it recursively rebuilds objects based on whether the passed in object is an Array, String or a Hash. Returns: Object |
#_unpack_stub(index) |
Retrieves and unpacks a stubbed object from its separate storage area. Returns: Aqua::Stub |
#reload(mask_exceptions = true) |
Reloads database information into the object. Returns: Object |
#reload! |
Reloads database information into the object, and raises an error on failure. Returns: Object |
Public Instance Method Details
_clear_store
Makes @_store nil to converve on memory
97 98 99 |
# File 'lib/aqua/object/unpack.rb', line 97 def _clear_store @_store = nil end |
_get_store
Retrieves objects storage from its engine.
75 76 77 78 |
# File 'lib/aqua/object/unpack.rb', line 75 def _get_store # this is kind of klunky, should refactor self._store = Aqua::Storage.new(:id => self.id).retrieve end |
_reload
Actual mechanism for reloading an object from stored data.
64 65 66 67 68 69 |
# File 'lib/aqua/object/unpack.rb', line 64 def _reload _get_store _unpack _clear_store self end |
_unpack
Unpacks an object from hash representation of data and metadata
85 86 87 88 89 90 91 92 |
# File 'lib/aqua/object/unpack.rb', line 85 def _unpack if init = _unpack_initialization( _store ) replace( init ) end if ivars = _store[:ivars] _unpack_ivars( self, ivars ) end end |
_unpack_array
Unpacks an Array.
152 153 154 155 156 157 158 159 |
# File 'lib/aqua/object/unpack.rb', line 152 def _unpack_array( obj ) arr = [] obj.each do |value| value = _unpack_object( value ) unless value.class == String arr << value end arr end |
_unpack_hash
Unpacks a Hash.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/aqua/object/unpack.rb', line 166 def _unpack_hash( obj ) hash = {} obj.each do |raw_key, value| value = _unpack_object( value ) unless value.class == String if raw_key.match(/\A(:)/) key = raw_key.gsub($1, '').to_sym elsif raw_key.match(/\A\/OBJECT_(\d*)\z/) key = _unpack_object( self._store[:keys][$1.to_i] ) else key = raw_key end hash[key] = value end hash end |
_unpack_initialization
Unpacks an the initialization object from a hash into a real object.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/aqua/object/unpack.rb', line 121 def _unpack_initialization( obj ) if init = obj[:init] init_class = init.class if init_class == String if init.match(/\A\/STUB_(\d*)\z/) _unpack_stub( $1.to_i ) else init end elsif init.class == Array _unpack_array( init ) else _unpack_hash( init ) end end end |
_unpack_ivars
Unpacks an object’s instance variables
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/aqua/object/unpack.rb', line 105 def _unpack_ivars( obj, data ) data.each do |ivar_name, data_package| unpacked = if data_package.class == String data_package else _unpack_object( data_package ) end obj.instance_variable_set( ivar_name, unpacked ) end end |
_unpack_object
The real workhorse behind object construction: it recursively rebuilds objects based on whether the passed in object is an Array, String or a Hash. A hash that has the class key is an object representation. If it does not have a hash key then it is an ordinary hash. An array will either have strings or object representations values.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/aqua/object/unpack.rb', line 191 def _unpack_object( store_pack ) package_class = store_pack.class if package_class == String store_pack elsif package_class == Array _unpack_array( store_pack ) else # package_class == Hash -or- Mash if store_pack['class'] # Constantize the objects class obj_class = store_pack['class'].constantize rescue nil # build from initialization init = _unpack_initialization( store_pack ) return_object = if init obj_class == Aqua::Stub ? init : obj_class.aqua_init( init ) end # Build uninitialized object if return_object.nil? if obj_class return_object = obj_class.new else # should log an error internally return_object = OpenStruct.new end end # add the ivars if ivars = store_pack['ivars'] ivars.delete('@table') if obj_class.ancestors.include?( OpenStruct ) _unpack_ivars( return_object, ivars ) end return_object else # not a packaged object, just a hash, so unpack _unpack_hash( hash ) end end end |
_unpack_stub
Retrieves and unpacks a stubbed object from its separate storage area
142 143 144 145 |
# File 'lib/aqua/object/unpack.rb', line 142 def _unpack_stub( index ) hash = _store[:stubs][index] Aqua::Stub.new( hash ) end |
reload
Reloads database information into the object.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/aqua/object/unpack.rb', line 31 def reload( mask_exceptions = true ) if id.nil? if mask_exceptions false else raise ObjectNotFound, "#{self.class} instance must have an id to be reloaded" end else begin _reload rescue Exception => e if mask_exceptions false else raise e end end end end |
reload!
Reloads database information into the object, and raises an error on failure.
55 56 57 |
# File 'lib/aqua/object/unpack.rb', line 55 def reload! reload( false ) end |