Sha256: c48ab36161a7aa5f1c7a3599c0935c5cd0e94b8b442a22b4d856987200e1c1d6

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

require 'yaml/store'
require 'auto_network/pool'

module AutoNetwork
  # This is a specialized subclass of `YAML::Store` that knows how to
  # initialize and upgrade AutoNetwork Pool files.
  #
  # @api private
  class PoolStorage < YAML::Store
    POOLFILE_VERSION = 2
    POOLFILE_SKELETON =  {
      'poolfile_version' => POOLFILE_VERSION,
      'pools' => {},
    }

    # Creates a new pool file at a target location and fills it with default
    # data.
    #
    # @param path [String, Pathname] the location of the new pool file.
    # @return [void]
    def self.init(path)
      File.write(path, POOLFILE_SKELETON.to_yaml)
    end

    private

    # Override the method inherited from `YAML::Store`. All `PStore` object
    # expect load to strictly return a `Hash`. This override allows us to
    # perform on-the-fly upgrading of data loaded from old pool files and
    # ensure the right structure is returned.
    #
    # @api private
    #
    # @param content [String] serialized YAML read from the pool file.
    # @return [Hash]
    def load(content)
      data = super(content)

      if data.is_a? AutoNetwork::Pool
        upgrade_from_version_1! data
      else
        data
      end
    end

    # The loosely defined "version 1" of the pool file just serialized a single
    # {AutoNetwork::Pool} object. All AutoNetwork releases that used Version 1
    # files only supported the Vagrant VirtualBox provider, so we return a new
    # Hash-based data structure that includes the old Pool.
    #
    # @api private
    #
    # @param data [AutoNetwork::Pool] the old pool object.
    # @return [Hash] a hash containing the old pool object.
    def upgrade_from_version_1!(data)
      {
        'poolfile_version' => POOLFILE_VERSION,
        'pools' => {
          'virtualbox' => data,
        },
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vagrant-auto_network-1.0.0.beta1 lib/auto_network/pool_storage.rb