class Vagrant::BoxMetadata

BoxMetadata represents metadata about a box, including the name it should have, a description of it, the versions it has, and more.

Attributes

description[RW]

The long-form human-readable description of a box.

@return [String]

name[RW]

The name that the box should be if it is added.

@return [String]

Public Class Methods

new(io) click to toggle source

Loads the metadata associated with the box from the given IO.

@param [IO] io An IO object to read the metadata from.

# File lib/vagrant/box_metadata.rb, line 22
def initialize(io)
  begin
    @raw = JSON.load(io)
  rescue JSON::ParserError => e
    raise Errors::BoxMetadataMalformed,
      error: e.to_s
  end

  @raw ||= {}
  @name = @raw["name"]
  @description = @raw["description"]
  @version_map = (@raw["versions"] || []).map do |v|
    begin
      [Gem::Version.new(v["version"]), v]
    rescue ArgumentError
      raise Errors::BoxMetadataMalformedVersion,
        version: v["version"].to_s
    end
  end
  @version_map = Hash[@version_map]
end

Public Instance Methods

version(version, **opts) click to toggle source

Returns data about a single version that is included in this metadata.

@param [String] version The version to return, this can also

be a constraint.

@return [Version] The matching version or nil if a matching

version was not found.
# File lib/vagrant/box_metadata.rb, line 51
def version(version, **opts)
  requirements = version.split(",").map do |v|
    Gem::Requirement.new(v.strip)
  end

  providers = nil
  providers = Array(opts[:provider]).map(&:to_sym) if opts[:provider]

  @version_map.keys.sort.reverse.each do |v|
    next if !requirements.all? { |r| r.satisfied_by?(v) }
    version = Version.new(@version_map[v])
    next if (providers & version.providers).empty? if providers
    return version
  end

  nil
end