Class: BindingDumper::Dumpers::HashDumper

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

Overview

Class responsible for converting arbitary hashes to marshalable hashes

Examples:

hash = { key: 'value' }
dump = BindingDumper::Dumpers::HashDumper.new(hash).convert
BindingDumper::Dumpers::HashDumper.new(dump).deconvert
# => { key: 'value' }

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 HashDumper can convert passed abstract_object

Returns:

  • (true, false)


17
18
19
# File 'lib/binding_dumper/dumpers/hash_dumper.rb', line 17

def can_convert?
  hash.is_a?(Hash)
end

#can_deconvert?true, false

Returns true if HashDumper can deconvert passed abstract_object

Returns:

  • (true, false)


25
26
27
# File 'lib/binding_dumper/dumpers/hash_dumper.rb', line 25

def can_deconvert?
  abstract_object.is_a?(Hash)
end

#convertHash

Converts passed abstract_object to marshalable Hash

Returns:

  • (Hash)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/binding_dumper/dumpers/hash_dumper.rb', line 33

def convert
  unless should_convert?
    return { _existing_object_id: hash.object_id }
  end

  dumped_ids << hash.object_id

  prepared = hash.map do |k, v|
    converted_k = UniversalDumper.convert(k, dumped_ids)
    converted_v = UniversalDumper.convert(v, dumped_ids)
    [converted_k, converted_v]
  end

  result = Hash[prepared]

  {
    _old_object_id: hash.object_id,
    _object_data: result
  }
end

#deconvert {|result| ... } ⇒ Hash

Deconverts passed abstract_object back to the original state

Yields:

  • (result)

Returns:

  • (Hash)


58
59
60
61
62
63
64
65
66
67
# File 'lib/binding_dumper/dumpers/hash_dumper.rb', line 58

def deconvert
  result = {}
  yield result
  hash.each do |converted_k, converted_v|
    k = UniversalDumper.deconvert(converted_k)
    v = UniversalDumper.deconvert(converted_v)
    result[k] = v
  end
  result
end