Class: Puppet::Pops::Serialization::Serializer
- Defined in:
- lib/puppet/pops/serialization/serializer.rb
Overview
The serializer is capable of writing, arrays, maps, and complex objects using an underlying protocol writer. It takes care of tabulating and disassembling complex objects.
Instance Attribute Summary collapse
-
#writer ⇒ Object
readonly
private
Provides access to the writer.
Instance Method Summary collapse
-
#finish ⇒ Object
Tell the underlying writer to finish.
-
#initialize(writer, options = EMPTY_HASH) ⇒ Serializer
constructor
A new instance of Serializer.
- #inspect ⇒ Object
- #push_written(value) ⇒ Object
-
#start_array(size) ⇒ Object
private
Write the start of an array.
-
#start_map(size) ⇒ Object
private
Write the start of a map (hash).
-
#start_object(attr_count) ⇒ Object
private
Write the start of a complex object.
-
#start_pcore_object(type_ref, attr_count) ⇒ Object
private
Write the start of a complex pcore object.
-
#start_sensitive ⇒ Object
private
Write the start of a sensitive object.
- #to_s ⇒ Object
- #type_by_reference? ⇒ Boolean
-
#write(value) ⇒ Object
Write an object.
-
#write_tabulated_first_time(value) ⇒ Object
private
First time write of a tabulated object.
Constructor Details
#initialize(writer, options = EMPTY_HASH) ⇒ Serializer
Returns a new instance of Serializer.
18 19 20 21 22 |
# File 'lib/puppet/pops/serialization/serializer.rb', line 18 def initialize(writer, = EMPTY_HASH) @written = {} @writer = writer @options = end |
Instance Attribute Details
#writer ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Provides access to the writer.
12 13 14 |
# File 'lib/puppet/pops/serialization/serializer.rb', line 12 def writer @writer end |
Instance Method Details
#finish ⇒ Object
Tell the underlying writer to finish
26 27 28 |
# File 'lib/puppet/pops/serialization/serializer.rb', line 26 def finish @writer.finish end |
#inspect ⇒ Object
96 97 98 |
# File 'lib/puppet/pops/serialization/serializer.rb', line 96 def inspect to_s end |
#push_written(value) ⇒ Object
78 79 80 |
# File 'lib/puppet/pops/serialization/serializer.rb', line 78 def push_written(value) @written[value.object_id] = @written.size end |
#start_array(size) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Write the start of an array.
52 53 54 |
# File 'lib/puppet/pops/serialization/serializer.rb', line 52 def start_array(size) @writer.write(Extension::ArrayStart.new(size)) end |
#start_map(size) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Write the start of a map (hash).
59 60 61 |
# File 'lib/puppet/pops/serialization/serializer.rb', line 59 def start_map(size) @writer.write(Extension::MapStart.new(size)) end |
#start_object(attr_count) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Write the start of a complex object
74 75 76 |
# File 'lib/puppet/pops/serialization/serializer.rb', line 74 def start_object(attr_count) @writer.write(Extension::ObjectStart.new(attr_count)) end |
#start_pcore_object(type_ref, attr_count) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Write the start of a complex pcore object
67 68 69 |
# File 'lib/puppet/pops/serialization/serializer.rb', line 67 def start_pcore_object(type_ref, attr_count) @writer.write(Extension::PcoreObjectStart.new(type_ref, attr_count)) end |
#start_sensitive ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Write the start of a sensitive object
84 85 86 |
# File 'lib/puppet/pops/serialization/serializer.rb', line 84 def start_sensitive @writer.write(Extension::SensitiveStart::INSTANCE) end |
#to_s ⇒ Object
92 93 94 |
# File 'lib/puppet/pops/serialization/serializer.rb', line 92 def to_s "#{self.class.name} with #{@writer}" end |
#type_by_reference? ⇒ Boolean
88 89 90 |
# File 'lib/puppet/pops/serialization/serializer.rb', line 88 def type_by_reference? @options[:type_by_reference] == true end |
#write(value) ⇒ Object
Write an object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/puppet/pops/serialization/serializer.rb', line 33 def write(value) case value when Integer, Float, String, true, false, nil @writer.write(value) when :default @writer.write(Extension::Default::INSTANCE) else index = @written[value.object_id] if index.nil? write_tabulated_first_time(value) else @writer.write(Extension::Tabulation.new(index)) end end end |
#write_tabulated_first_time(value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
First time write of a tabulated object. This means that the object is written and then remembered. Subsequent writes of the same object will yield a write of a tabulation index instead.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/puppet/pops/serialization/serializer.rb', line 104 def write_tabulated_first_time(value) case when value.instance_of?(Symbol), value.instance_of?(Regexp), value.instance_of?(SemanticPuppet::Version), value.instance_of?(SemanticPuppet::VersionRange), value.instance_of?(Time::Timestamp), value.instance_of?(Time::Timespan), value.instance_of?(Types::PBinaryType::Binary), value.is_a?(URI) push_written(value) @writer.write(value) when value.instance_of?(Array) push_written(value) start_array(value.size) value.each { |elem| write(elem) } when value.instance_of?(Hash) push_written(value) start_map(value.size) value.each_pair { |key, val| write(key); write(val) } when value.instance_of?(Types::PSensitiveType::Sensitive) start_sensitive write(value.unwrap) when value.instance_of?(Types::PTypeReferenceType) push_written(value) @writer.write(value) when value.is_a?(Types::PuppetObject) value._pcore_type.write(value, self) else impl_class = value.class type = Loaders.implementation_registry.type_for_module(impl_class) raise SerializationError, _("No Puppet Type found for %{klass}") % { klass: impl_class.name } unless type.is_a?(Types::PObjectType) type.write(value, self) end end |