Class: Vagrant::BoxCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/vagrant/box_collection.rb

Overview

Represents a collection of boxes, providing helpful methods for finding boxes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory, action_runner) ⇒ BoxCollection

Initializes the class to search for boxes in the given directory.



15
16
17
18
19
20
21
# File 'lib/vagrant/box_collection.rb', line 15

def initialize(directory, action_runner)
  @directory     = directory
  @boxes         = []
  @action_runner = action_runner

  reload!
end

Instance Attribute Details

#directoryObject (readonly)

The directory that the boxes are being searched for.



12
13
14
# File 'lib/vagrant/box_collection.rb', line 12

def directory
  @directory
end

Instance Method Details

#add(name, url) ⇒ Object

Adds a box to this collection with the given name and located at the given URL.



35
36
37
38
39
40
41
42
# File 'lib/vagrant/box_collection.rb', line 35

def add(name, url)
  raise Errors::BoxAlreadyExists, :name => name if find(name)

  @action_runner.run(:box_add,
                     :box_name => name,
                     :box_url => url,
                     :box_directory => @directory.join(name))
end

#find(name) ⇒ Object

Find a box in the collection by the given name. The name must be a string, for now.



25
26
27
28
29
30
31
# File 'lib/vagrant/box_collection.rb', line 25

def find(name)
  @boxes.each do |box|
    return box if box.name == name
  end

  nil
end

#reload!Object

Loads the list of all boxes from the source. This modifies the current array.



46
47
48
49
50
51
52
53
54
55
# File 'lib/vagrant/box_collection.rb', line 46

def reload!
  @boxes.clear

  Dir.open(@directory) do |dir|
    dir.each do |d|
      next if d == "." || d == ".." || !@directory.join(d).directory?
      @boxes << Box.new(d, @directory.join(d), @action_runner)
    end
  end
end