Class: ZAML::Label

Inherits:
Object show all
Defined in:
lib/icss/serialization/zaml.rb

Overview

Label class – resolves circular references

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, indent) ⇒ Label

Returns a new instance of Label.



165
166
167
168
169
# File 'lib/icss/serialization/zaml.rb', line 165

def initialize(obj,indent)
  @indent = indent
  @this_label_number = nil
  @@previously_emitted_object[obj.object_id] = self
end

Class Method Details

.counter_resetObject

YAML only wants objects in the datastream once; if the same object

occurs more than once, we need to emit a label ("&idxxx") on the
first occurrence and then emit a back reference (*idxxx") on any
subsequent occurrence(s).

To accomplish this we keeps a hash (by object id) of the labels of

the things we serialize as we begin to serialize them.  The labels
initially serialize as an empty string (since most objects are only
going to be be encountered once), but can be changed to a valid
(by assigning it a number) the first time it is subsequently used,
if it ever is.  Note that we need to do the label setup BEFORE we
start to serialize the object so that circular structures (in
which we will encounter a reference to the object as we serialize
it can be handled).


161
162
163
164
# File 'lib/icss/serialization/zaml.rb', line 161

def self.counter_reset
  @@previously_emitted_object = {}
  @@next_free_label_number = 0
end

.for(obj) ⇒ Object



177
178
179
# File 'lib/icss/serialization/zaml.rb', line 177

def self.for(obj)
  @@previously_emitted_object[obj.object_id]
end

Instance Method Details

#referenceObject



173
174
175
176
# File 'lib/icss/serialization/zaml.rb', line 173

def reference
  @this_label_number ||= (@@next_free_label_number += 1)
  @reference         ||= '*id%03d' % @this_label_number
end

#to_sObject



170
171
172
# File 'lib/icss/serialization/zaml.rb', line 170

def to_s
  @this_label_number ? ('&id%03d%s' % [@this_label_number, @indent]) : ''
end