Module: BindingDumper::MagicObjects

Defined in:
lib/binding_dumper/magic_objects.rb

Overview

Module responsible for storing and retrieving ‘magical’ objects

Object is 'magical' if it's the same for every Ruby process

Examples of ‘magical’ objects:

Rails.application
Rails.env
Rails.application.config

To register an object, run: After marking an object as ‘magical’ it (and all embedded objects)

will be added to the object pool of 'magical' objects

Examples:

BindingDumper::MagicObjects.register(MyClass)
# or if it's method that returns always the same data
BindingDumper::MagicObjects.register(:some_method, 'send(:some_method)')
class A
  class << self
    attr_reader :config
    @config = { config: :data }
  end
end

BindingDumper::MagicObjects.register(A)
BindingDumper::MagicObjects.pool
{
  47472500 => "A",
  600668   => "A.instance_variable_get(:@config)"
}
BindingDumper::MagicObjects.magic?(A.config)
# => true
BindingDumper::MagicObjects.get_magic(A.config)
# => "A.instance_variable_get(:@config)"

Class Method Summary collapse

Class Method Details

.flush!Object

Flushes existing information about ‘magical’ objects



95
96
97
# File 'lib/binding_dumper/magic_objects.rb', line 95

def self.flush!
  @pool = {}
end

.get_magic(object) ⇒ String

Returns the way to get a ‘magical’ object

Parameters:

  • object (Object)

Returns:

  • (String)


89
90
91
# File 'lib/binding_dumper/magic_objects.rb', line 89

def self.get_magic(object)
  pool[object.object_id]
end

.magic?(object) ⇒ true, false

Returns true if passed object is ‘magical’

Returns:

  • (true, false)


79
80
81
# File 'lib/binding_dumper/magic_objects.rb', line 79

def self.magic?(object)
  pool.has_key?(object.object_id)
end

.magic_tree_from(object, object_path, result = {}) ⇒ Hash

Builds a tree of objects inside of passed object

Returns:

  • (Hash)


42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/binding_dumper/magic_objects.rb', line 42

def self.magic_tree_from(object, object_path, result = {})
  return if result[object.object_id]

  result[object.object_id] = object_path

  object.instance_variables.each do |ivar_name|
    path = "#{object_path}.instance_variable_get(:#{ivar_name})"
    ivar = object.instance_variable_get(ivar_name)
    magic_tree_from(ivar, path, result)
  end

  result
end

.poolHash

Returns Hash containing all magical objects

Returns:

  • (Hash)

    in format { object_id => way to get an object }



71
72
73
# File 'lib/binding_dumper/magic_objects.rb', line 71

def self.pool
  @pool ||= {}
end

.register(object, object_path = object.name) ⇒ Object

Registers passed object as ‘magical’

Parameters:

  • object (Object)
  • object_path (String) (defaults to: object.name)

    the way how to get an object



61
62
63
64
65
# File 'lib/binding_dumper/magic_objects.rb', line 61

def self.register(object, object_path = object.name)
  tree = magic_tree_from(object, object_path)
  pool.merge!(tree)
  true
end