Sha256: 74b78b5d00879ed3d2c68a29c0bba63ea48ff073a45c73368bd3af6869a4ad6f

Contents?: true

Size: 1.39 KB

Versions: 32

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

# 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 when it encounters a tag
  # in the form !ruby/object:<class name>.
  #
  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] || "!ruby/object:#{self.class.name}"
    psych_encoder.represent_map(tag, to_data_hash)
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
puppet-8.10.0 lib/puppet/util/psych_support.rb
puppet-8.10.0-x86-mingw32 lib/puppet/util/psych_support.rb
puppet-8.10.0-x64-mingw32 lib/puppet/util/psych_support.rb
puppet-8.10.0-universal-darwin lib/puppet/util/psych_support.rb
puppet-8.9.0 lib/puppet/util/psych_support.rb
puppet-8.9.0-x86-mingw32 lib/puppet/util/psych_support.rb
puppet-8.9.0-x64-mingw32 lib/puppet/util/psych_support.rb
puppet-8.9.0-universal-darwin lib/puppet/util/psych_support.rb
puppet-8.8.1 lib/puppet/util/psych_support.rb
puppet-8.8.1-x86-mingw32 lib/puppet/util/psych_support.rb
puppet-8.8.1-x64-mingw32 lib/puppet/util/psych_support.rb
puppet-8.8.1-universal-darwin lib/puppet/util/psych_support.rb
puppet-8.7.0 lib/puppet/util/psych_support.rb
puppet-8.7.0-x86-mingw32 lib/puppet/util/psych_support.rb
puppet-8.7.0-x64-mingw32 lib/puppet/util/psych_support.rb
puppet-8.7.0-universal-darwin lib/puppet/util/psych_support.rb
puppet-8.6.0 lib/puppet/util/psych_support.rb
puppet-8.6.0-x86-mingw32 lib/puppet/util/psych_support.rb
puppet-8.6.0-x64-mingw32 lib/puppet/util/psych_support.rb
puppet-8.6.0-universal-darwin lib/puppet/util/psych_support.rb