class Vagrant::BoxMetadata::Version

Represents a single version within the metadata.

Attributes

version[RW]

The version that this Version object represents.

@return [String]

Public Class Methods

new(raw=nil, **_) click to toggle source
# File lib/vagrant/box_metadata.rb, line 126
def initialize(raw=nil, **_)
  return if !raw

  @version = raw["version"]
  @providers = raw.fetch("providers", []).map do |data|
    Provider.new(data)
  end
  @provider_map = @providers.group_by(&:name)
  @provider_map = Util::HashWithIndifferentAccess.new(@provider_map)
end

Public Instance Methods

provider(name, architecture=nil) click to toggle source

Returns a [Provider] for the given name, or nil if it isn’t supported by this version.

# File lib/vagrant/box_metadata.rb, line 139
def provider(name, architecture=nil)
  name = name.to_sym
  arch_name = architecture
  arch_name = Util::Platform.architecture if arch_name == :auto
  arch_name = arch_name.to_s if arch_name

  # If the provider doesn't exist in the map, return immediately
  return if !@provider_map.key?(name)

  # If the arch_name value is set, filter based
  # on architecture and return match if found. If
  # no match is found and architecture wasn't automatically
  # detected, return nil as an explicit match is
  # being requested
  if arch_name
    match = @provider_map[name].detect do |p|
      p.architecture == arch_name
    end

    return match if match || architecture != :auto
  end

  # If the passed architecture value was :auto and no explicit
  # match for the architecture was found, check for a provider
  # that is flagged as the default architecture, and has an
  # architecture value of "unknown"
  #
  # NOTE: This preserves expected behavior with legacy boxes
  if architecture == :auto
    match = @provider_map[name].detect do |p|
      p.architecture == "unknown" &&
        p.default_architecture
    end

    return match if match
  end

  # If the architecture value is set to nil, then just return
  # whatever is defined as the default architecture
  if architecture.nil?
    match = @provider_map[name].detect(&:default_architecture)

    return match if match
  end

  # The metadata consumed may not include architecture information,
  # in which case the match would just be the single provider
  # defined within the provider map for the name
  if @provider_map[name].size == 1 && !@provider_map[name].first.architecture_support?
    return @provider_map[name].first
  end

  # Otherwise, there is no match
  nil
end
providers(architecture=nil) click to toggle source

Returns the providers that are available for this version of the box.

@return [Array<Symbol>]

# File lib/vagrant/box_metadata.rb, line 199
def providers(architecture=nil)
  return @provider_map.keys.map(&:to_sym) if architecture.nil?

  @provider_map.keys.find_all { |k|
    provider(k, architecture)
  }.map(&:to_sym)
end