Sha256: 87ae48af9fcf6c19109bdbf5da3877fb13a37135aa5a8a7580559e76c4e9deae

Contents?: true

Size: 1.81 KB

Versions: 96

Compression:

Stored size: 1.81 KB

Contents

# This module should be included when a class can be serialized to yaml and
# needs to handle the deserialization from Psych with more control. Psych normally
# pokes values directly into an instance using `instance_variable_set` which completely
# bypasses encapsulation.
#
# The class that includes this module must implement an instance method `initialize_from_hash`
# that is given a hash with attribute to value mappings.
#
module Puppet::Util::PsychSupport
  # This method is called from the Psych Yaml deserializer.
  # The serializer calls this instead of doing the initialization itself using
  # `instance_variable_set`. The Status class requires this because it serializes its TagSet
  # as an `Array` in order to optimize the size of the serialized result.
  # When this array is later deserialized it must be reincarnated as a TagSet. The standard
  # Psych way of doing this via poking values into instance variables cannot do this.
  #
  def init_with(psych_coder)
    # The PsychCoder has a hashmap of instance variable name (sans the @ symbol) to values
    # to set, and can thus directly be fed to initialize_from_hash.
    #
    initialize_from_hash(psych_coder.map)
  end

  # This method is called from the Psych Yaml serializer
  # The serializer will call this method to create a hash that will be serialized to YAML.
  # Instead of using the object itself during the mapping process we use what is
  # returned by calling `to_data_hash` on the object itself since some of the
  # objects we manage have asymmetrical serialization and deserialization.
  #
  def encode_with(psych_encoder)
    tag = Psych.dump_tags[self.class]
    unless tag
      klass = self.class == Object ? nil : self.class.name
      tag   = ['!ruby/object', klass].compact.join(':')
    end
    psych_encoder.represent_map(tag, to_data_hash)
  end
end

Version data entries

96 entries across 96 versions & 2 rubygems

Version Path
puppet-retrospec-1.8.0 vendor/pup410/lib/puppet/util/psych_support.rb
puppet-retrospec-1.7.0 vendor/pup410/lib/puppet/util/psych_support.rb
puppet-4.10.12 lib/puppet/util/psych_support.rb
puppet-4.10.12-x86-mingw32 lib/puppet/util/psych_support.rb
puppet-4.10.12-x64-mingw32 lib/puppet/util/psych_support.rb
puppet-4.10.12-universal-darwin lib/puppet/util/psych_support.rb
puppet-4.10.11 lib/puppet/util/psych_support.rb
puppet-4.10.11-x86-mingw32 lib/puppet/util/psych_support.rb
puppet-4.10.11-x64-mingw32 lib/puppet/util/psych_support.rb
puppet-4.10.11-universal-darwin lib/puppet/util/psych_support.rb
puppet-4.10.10 lib/puppet/util/psych_support.rb
puppet-4.10.10-x86-mingw32 lib/puppet/util/psych_support.rb
puppet-4.10.10-x64-mingw32 lib/puppet/util/psych_support.rb
puppet-4.10.10-universal-darwin lib/puppet/util/psych_support.rb
puppet-retrospec-1.6.1 vendor/pup410/lib/puppet/util/psych_support.rb
puppet-retrospec-1.6.0 vendor/pup410/lib/puppet/util/psych_support.rb
puppet-4.10.9 lib/puppet/util/psych_support.rb
puppet-4.10.9-x86-mingw32 lib/puppet/util/psych_support.rb
puppet-4.10.9-x64-mingw32 lib/puppet/util/psych_support.rb
puppet-4.10.9-universal-darwin lib/puppet/util/psych_support.rb