lib/aqua/object/pack.rb in baccigalupi-aqua-0.1.1 vs lib/aqua/object/pack.rb in baccigalupi-aqua-0.1.2

- old
+ new

@@ -1,8 +1,8 @@ # This module is responsible for packing objects into Storage Objects # The Storage Object is expected to be a Mash-Like thing (Hash with indifferent access). -# It is the job of the storage engine to convert the Mash into the actual storage data. +# It is the job of the storage engine to convert the Mash into the actual storage ivars. module Aqua::Pack def self.included( klass ) klass.class_eval do extend ClassMethods @@ -66,10 +66,11 @@ class_name = self.class.to_s self.__pack = Aqua::Storage.new self.__pack.id = @id if @id self.__pack[:_rev] = _rev if _rev self.__pack[:class] = class_name + self.__pack[:keys] = [] _pack_properties _pack_singletons __pack end @@ -134,13 +135,13 @@ # Object packing methods ------------ # Examines each ivar and converts it to a hash, array, string combo # @api private def _pack_properties - self.__pack[:data] = _pack_ivars( self ) + self.__pack[:ivars] = _pack_ivars( self ) initializations = _pack_initializations( self ) - self.__pack[:initialization] = initializations unless initializations.empty? + self.__pack[:init] = initializations unless initializations.empty? end def _pack_initializations( obj ) ancestors = obj.class.ancestors initializations = {} @@ -176,30 +177,31 @@ def _pack_object( obj ) klass = obj.class if klass == String obj elsif [TrueClass, FalseClass].include?( klass ) - { 'class' => klass.to_s, 'initialization' => obj.to_s } + { 'class' => klass.to_s, 'init' => obj.to_s } elsif [Time, Date, Fixnum, Bignum, Float ].include?( klass ) { 'class' => klass.to_s, - 'initialization' => obj.to_s + 'init' => obj.to_s } elsif klass == Rational { 'class' => klass.to_s, - 'initialization' => obj.to_s.match(/(\d*)\/(\d*)/).to_a.slice(1,2) + 'init' => obj.to_s.match(/(\d*)\/(\d*)/).to_a.slice(1,2) } else # a more complex object, including an array or a hash like thing return_hash = {} - if obj.aquatic? # TODO distinguish between internal storage, stubbing and external (obj.aquatic? && obj._embed_me == true) + if obj.aquatic? + # TODO distinguish between internal storage, stubbing and external (obj.aquatic? && obj._embed_me == true) return_hash = obj._pack elsif !obj.aquatic? initialization = _pack_initializations( obj ) - return_hash['initialization'] = initialization unless initialization.empty? + return_hash['init'] = initialization unless initialization.empty? data = _pack_ivars( obj ) - return_hash['data'] = data unless data.empty? + return_hash['ivars'] = data unless data.empty? return_hash['class'] = klass.to_s # TODO: distinguish between internal storage, stubbing and external (obj.aquatic? && obj._embed_me == true) # elsif obj._embed_me.class == Hash # return_hash = _stub( obj ) # else @@ -214,12 +216,21 @@ # @return [Hash] The parsed Hash representation of the argument Hash # # @api private def _pack_hash( hash ) return_hash = {} - hash.each do |key, value| - raise ArgumentError, 'Currently Hash keys must be either strings or symbols' unless [Symbol, String].include?( key.class ) - return_hash[key.to_s] = _pack_object( value ) + hash.each do |raw_key, value| + key_class = raw_key.class + if key_class == Symbol + key = ":#{raw_key.to_s}" + elsif key_class == String + key = raw_key + else + index = self.__pack[:keys].length + self.__pack[:keys] << _pack_object( raw_key ) + key = "/OBJECT_#{index}" + end + return_hash[key] = _pack_object( value ) end return_hash end def _pack_struct( struct ) \ No newline at end of file