Class: AlphaSimprini::Heap::Packer

Inherits:
Object
  • Object
show all
Defined in:
lib/alpha_simprini/packer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePacker

Returns a new instance of Packer.



12
13
14
15
16
17
# File 'lib/alpha_simprini/packer.rb', line 12

def initialize
  @classes = {}
  @objects = {}
  @named_objects = {}
  @literals = []
end

Instance Attribute Details

#classesObject (readonly)

Returns the value of attribute classes.



11
12
13
# File 'lib/alpha_simprini/packer.rb', line 11

def classes
  @classes
end

#named_objectsObject (readonly)

Returns the value of attribute named_objects.



11
12
13
# File 'lib/alpha_simprini/packer.rb', line 11

def named_objects
  @named_objects
end

#objectsObject (readonly)

Returns the value of attribute objects.



11
12
13
# File 'lib/alpha_simprini/packer.rb', line 11

def objects
  @objects
end

Instance Method Details

#pack(object, named = nil) ⇒ Object



19
20
21
# File 'lib/alpha_simprini/packer.rb', line 19

def pack(object, named=nil)
  pack_object(object, pack_class(object.class), named)
end

#pack_class(klass) ⇒ Object



66
67
68
# File 'lib/alpha_simprini/packer.rb', line 66

def pack_class(klass)
  @classes[klass.name] ||= @classes.size
end

#pack_object(object, class_reference, named = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/alpha_simprini/packer.rb', line 23

def pack_object(object, class_reference, named=nil)
  return object.object_id if @objects[object.object_id]
  @objects[object.object_id] = reference = {} 
   if object.is_a?(Hash)
    reference.merge!\
      CLASS => class_reference,
      ATTRIBUTES => object.inject({}) {|hash, (name, object)| hash[pack(name)] = pack(object); hash }
  elsif object.is_a?(Array)
    reference.merge!\
      CLASS => class_reference,
      ATTRIBUTES => object.map{|item| pack(item) }
  elsif object.is_a?(Numeric)
    reference.merge!\
      CLASS => pack_class(Numeric),
      ATTRIBUTES => @literals.count
    @literals << object
  elsif object.is_a?(Symbol)
    reference.merge!\
      CLASS => pack_class(String),
      ATTRIBUTES => @literals.count
    @literals << object.to_s
  elsif object.is_a?(String)
    reference.merge!\
      CLASS => class_reference,
      ATTRIBUTES => @literals.count
    @literals << object
  else
    reference.merge!\
      CLASS => class_reference,
      ATTRIBUTES => (object.instance_variables.inject({}) do |hash, name|
        value_to_pack = object.instance_variable_get(name)
        if value_to_pack == object
          hash[pack(name)] = object.object_id
        else
          hash[pack(name)] = pack(value_to_pack)
        end
        hash
      end)
  end
  @named_objects[pack(named)] = object.object_id if named
  object.object_id
end

#to_hObject



70
71
72
73
74
75
76
77
# File 'lib/alpha_simprini/packer.rb', line 70

def to_h
  {
    CLASSES => @classes,
    OBJECTS => @objects,
    NAMED_OBJECTS => @named_objects,
    LITERALS => @literals
  }
end