Class: ZAML::Label
Instance Attribute Summary collapse
-
#this_label_number ⇒ Object
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).
Instance Method Summary collapse
-
#initialize(obj, indent) ⇒ Label
constructor
A new instance of Label.
- #reference ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(obj, indent) ⇒ Label
Returns a new instance of Label.
97 98 99 100 101 |
# File 'lib/vendor/puppet/util/zaml.rb', line 97 def initialize(obj,indent) @indent = indent @this_label_number = nil @obj = obj # prevent garbage collection so that object id isn't reused end |
Instance Attribute Details
#this_label_number ⇒ Object
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).
95 96 97 |
# File 'lib/vendor/puppet/util/zaml.rb', line 95 def this_label_number @this_label_number end |
Instance Method Details
#reference ⇒ Object
107 108 109 |
# File 'lib/vendor/puppet/util/zaml.rb', line 107 def reference @reference ||= '*id%03d' % @this_label_number end |
#to_s ⇒ Object
103 104 105 |
# File 'lib/vendor/puppet/util/zaml.rb', line 103 def to_s @this_label_number ? ('&id%03d%s' % [@this_label_number, @indent]) : '' end |