class VagrantCloud::Box

Attributes

organization[R]

Public Class Methods

new(organization:, **opts) click to toggle source

Create a new instance

@return [Box]

Calls superclass method
# File lib/vagrant_cloud/box.rb, line 17
def initialize(organization:, **opts)
  @organization = organization
  @versions_loaded = false
  opts[:username] = organization.username
  super(**opts)
  if opts[:versions] && !opts[:versions].empty?
    self.versions= Array(opts[:versions]).map do |version|
      Box::Version.load(box: self, **version)
    end
  end
  if opts[:current_version]
    clean(data: {current_version: Box::Version.
      load(box: self, **opts[:current_version])})
  end
  clean!
end

Public Instance Methods

add_version(version) click to toggle source

Add a new version of this box

@param [String] version Version number @return [Version]

# File lib/vagrant_cloud/box.rb, line 55
def add_version(version)
  if versions.any? { |v| v.version == version }
    raise Error::BoxError::VersionExistsError,
      "Version #{version} already exists for box #{tag}"
  end
  v = Version.new(box: self, version: version)
  clean(data: {versions: versions + [v]})
  v
end
delete() click to toggle source

Delete this box

@return [nil] @note This will delete the box, and all versions

# File lib/vagrant_cloud/box.rb, line 38
def delete
  if exist?
    organization.account.client.box_delete(
      username: username,
      name: name
    )
    b = organization.boxes.dup
    b.delete(self)
    organization.clean(data: {boxes: b})
  end
  nil
end
dirty?(key=nil, deep: false) click to toggle source

Check if this instance is dirty

@param [Boolean] deep Check nested instances @return [Boolean] instance is dirty

Calls superclass method
# File lib/vagrant_cloud/box.rb, line 69
def dirty?(key=nil, deep: false)
  if key
    super(key)
  else
    d = super() || !exist?
    if deep && !d
      d = Array(plain_versions).any? { |v| v.dirty?(deep: true) }
    end
    d
  end
end
exist?() click to toggle source

@return [Boolean] box exists remotely

# File lib/vagrant_cloud/box.rb, line 82
def exist?
  !!created_at
end
plain_versions()
Alias for: versions
save() click to toggle source

Save the box if any changes have been made

@return [self]

# File lib/vagrant_cloud/box.rb, line 110
def save
  save_box if dirty?
  save_versions if dirty?(deep: true)
  self
end
versions()
Also aliased as: plain_versions
Alias for: versions_on_demand
versions_on_demand() click to toggle source

@return [Array<Version>] @note This is used to allow versions information to be loaded only when requested

# File lib/vagrant_cloud/box.rb, line 89
def versions_on_demand
  if !@versions_loaded
    if exist?
      r = self.organization.account.client.box_get(username: username, name: name)
      v = Array(r[:versions]).map do |version|
        Box::Version.load(box: self, **version)
      end
      clean(data: {versions: v + Array(plain_versions)})
    else
      clean(data: {versions: []})
    end
    @versions_loaded = true
  end
  plain_versions
end
Also aliased as: versions

Protected Instance Methods

save_box() click to toggle source

Save the box

@return [self]

# File lib/vagrant_cloud/box.rb, line 121
def save_box
  req_args = {
    username: username,
    name: name,
    short_description: short_description,
    description: description,
    is_private: self.private
  }
  if exist?
    result = organization.account.client.box_update(**req_args)
  else
    result = organization.account.client.box_create(**req_args)
  end
  clean(data: result, ignores: [:current_version, :versions])
  self
end
save_versions() click to toggle source

Save the versions if any require saving

@return [self]

# File lib/vagrant_cloud/box.rb, line 141
def save_versions
  versions.map(&:save)
  self
end