Class: VagrantCloud::Box

Inherits:
Data::Mutable show all
Defined in:
lib/vagrant_cloud/box.rb,
lib/vagrant_cloud/box/version.rb,
lib/vagrant_cloud/box/provider.rb

Defined Under Namespace

Classes: Provider, Version

Constant Summary

Constants inherited from Data

Data::Nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Data::Mutable

#[], attr_mutable, #clean, #clean!, #freeze, load

Methods inherited from Data::Immutable

attr_optional, attr_required, inherited, #inspect, sync

Methods inherited from Data

#[], #inspect

Constructor Details

#initialize(organization:, **opts) ⇒ Box

Create a new instance



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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

Instance Attribute Details

#organizationObject (readonly)

Returns the value of attribute organization.



6
7
8
# File 'lib/vagrant_cloud/box.rb', line 6

def organization
  @organization
end

Instance Method Details

#add_version(version) ⇒ Version

Add a new version of this box



55
56
57
58
59
60
61
62
63
# 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

#deletenil

Note:

This will delete the box, and all versions

Delete this box



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/vagrant_cloud/box.rb', line 38

def delete
  if exist?
    organization..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) ⇒ Boolean

Check if this instance is dirty



69
70
71
72
73
74
75
76
77
78
79
# 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?Boolean



82
83
84
# File 'lib/vagrant_cloud/box.rb', line 82

def exist?
  !!created_at
end

#saveself

Save the box if any changes have been made



110
111
112
113
114
# File 'lib/vagrant_cloud/box.rb', line 110

def save
  save_box if dirty?
  save_versions if dirty?(deep: true)
  self
end

#versions_on_demandArray<Version> Also known as: versions

Note:

This is used to allow versions information to be loaded

only when requested



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/vagrant_cloud/box.rb', line 89

def versions_on_demand
  if !@versions_loaded
    if exist?
      r = self.organization..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