Class: Vagrant::BoxMetadata

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/box_metadata.rb

Overview

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

Defined Under Namespace

Classes: Provider, Version

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ BoxMetadata

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

Parameters:

  • io (IO)

    An IO object to read the metadata from.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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

Instance Attribute Details

#descriptionString

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

Returns:

  • (String)


16
17
18
# File 'lib/vagrant/box_metadata.rb', line 16

def description
  @description
end

#nameString

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

Returns:

  • (String)


11
12
13
# File 'lib/vagrant/box_metadata.rb', line 11

def name
  @name
end

Instance Method Details

#version(version, **opts) ⇒ Version

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

Parameters:

  • version (String)

    The version to return, this can also be a constraint.

Returns:

  • (Version)

    The matching version or nil if a matching version was not found.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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

#versionsObject

Returns all the versions supported by this metadata. These versions are sorted so the last element of the list is the latest version.

@return[Array]



74
75
76
# File 'lib/vagrant/box_metadata.rb', line 74

def versions
  @version_map.keys.sort.map(&:to_s)
end