Class: BindingDumper::Dumpers::MagicDumper

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

Overview

Class responsible for converting ‘magical’ objects to marshalable hash

You should define which objects are ‘magical’.

The difference between 'magical' and 'regular' objects is that
'magical' objects are the same for every sessions
like Rails.application.config or Rails.env

To make object ‘magical’, use BindingDumper::MagicObjects.register(object)

When you convert a ‘magical’ object to marshalable hash it returns ‘the way how to get it’,

not the object itself

Examples:

class A
  class << self
    @config = :config
    attr_reader :config
  end
end
BindingDumper::MagicObjects.register(A)
BindingDumper::MagicObjects.pool
{
  47472500 => "A",
  600668 => "A.instance_variable_get(:@config)"
}
# (the mapping 'object id' => 'the way to get an object')

dump = BindingDumper::Dumpers::MagicDumper.new(A.config).convert
# => { :_magic => "A.instance_variable_get(:@config)" }
restored = BindingDumper::Dumpers::MagicDumper.new(dump).deconvert
# => :config

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?true, false

Returns true if MagicDumper can convert passed abstract_object

Returns:

  • (true, false)


41
42
43
# File 'lib/binding_dumper/dumpers/magic_dumper.rb', line 41

def can_convert?
  MagicObjects.magic?(object)
end

#can_deconvert?true, false

Returns true if MagicDumper can deconvert passed abstract_object

Returns:

  • (true, false)


49
50
51
# File 'lib/binding_dumper/dumpers/magic_dumper.rb', line 49

def can_deconvert?
  abstract_object.is_a?(Hash) && abstract_object.has_key?(:_magic)
end

#convertHash

Converts passed abstract_object to marshalable Hash

Returns:

  • (Hash)


57
58
59
60
61
62
63
64
# File 'lib/binding_dumper/dumpers/magic_dumper.rb', line 57

def convert
  return unless should_convert?

  object_magic = MagicObjects.get_magic(object)
  {
    _magic: object_magic
  }
end

#deconvertObject

Deconverts passed abstract_object back to the original state

Returns:

  • (Object)


70
71
72
73
# File 'lib/binding_dumper/dumpers/magic_dumper.rb', line 70

def deconvert
  # release magic!
  eval(abstract_object[:_magic])
end