Class: BindingDumper::Dumpers::ExistingObjectDumper

Inherits:
Abstract
  • Object
show all
Defined in:
lib/binding_dumper/dumpers/existing_object_dumper.rb

Overview

profile = Profile.allocate

profile.first_name = profile     # <-- so the object is recursive
profile.last_name = StringIO.new # <-- and unmarshalable
dump = BindingDumper::UniversalDumper.convert(profile)
=>
{
  :_klass => Profile,
  :_ivars => {
    :@first_name => {
      :_existing_object_id => 47687640 # <-- right here
    },
    :@last_name => {
      :_klass => StringIO,
      :_undumpable => true
    }
  },
  :_old_object_id => 47687640
}

restored = BindingDumper::UniversalDumper.deconvert(profile)
restored.profile.equal?(restored)
# => true # (they have the same object id)

Instance Attribute Summary

Attributes inherited from Abstract

#dumped_ids

Instance Method Summary collapse

Methods inherited from Abstract

#initialize

Constructor Details

This class inherits a constructor from BindingDumper::Dumpers::Abstract

Instance Method Details

#can_convert?Boolean

Returns false, this class is only for deconverting

Returns:

  • (Boolean)


47
48
49
# File 'lib/binding_dumper/dumpers/existing_object_dumper.rb', line 47

def can_convert?
  false # really, it's only for deconverting
end

#can_deconvert?true, false

Returns true if ExistingObjectDumper can deconvert passed abstract_object

Returns:

  • (true, false)


55
56
57
# File 'lib/binding_dumper/dumpers/existing_object_dumper.rb', line 55

def can_deconvert?
  hash.is_a?(Hash) && hash.has_key?(:_existing_object_id)
end

#convertObject

Raises an exception, don’t use this class for converting

Raises:

  • (NotImplementedError)


63
64
65
# File 'lib/binding_dumper/dumpers/existing_object_dumper.rb', line 63

def convert
  raise NotImplementedError
end

#deconvertObject

Deconverts passed abstract_object back to the original state

Returns:

  • (Object)

Raises:

  • (RuntimeError)

    when object doesn’t exist in the memory



73
74
75
76
77
78
79
80
81
# File 'lib/binding_dumper/dumpers/existing_object_dumper.rb', line 73

def deconvert
  data_without_object_id = hash.dup.delete(:_existing_object_id)

  unless UniversalDumper.memories.has_key?(existing_object_id)
    raise "Object with id #{existing_object_id} wasn't dumped. Something is wrong."
  end

  UniversalDumper.memories[existing_object_id]
end