Class: VirtualBox::MediumAttachment

Inherits:
AbstractModel show all
Defined in:
lib/virtualbox/medium_attachment.rb

Overview

Represents the attachment of a medium (DVD, Hard Drive, etc) to a virtual machine, and specifically to a StorageController.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractModel

#errors, errors_for_relationship, #existing_record!, #inspect, #lazy_attribute?, #lazy_relationship?, #new_record!, #new_record?, #parent_machine, #populate_attributes, #populate_relationship, #populate_relationships, reload!, #reload!, reload?, reloaded!, #save, #save!, #save_attribute, #save_changed_interface_attributes, #save_interface_attribute, #set_relationship, #validate, #write_attribute

Methods included from AbstractModel::Validatable

#__validates_extract_options, #add_error, #clear_errors, #errors, #errors_on, #full_error_messages, #valid?, #validate, #validates_format_of, #validates_inclusion_of, #validates_numericality_of, #validates_presence_of

Methods included from AbstractModel::Relatable

#destroy_relationship, #destroy_relationships, #has_relationship?, included, #lazy_relationship?, #loaded_relationship?, #populate_relationship, #populate_relationships, #read_relationship, #relationship_class, #relationship_data, #save_relationship, #save_relationships, #set_relationship

Methods included from AbstractModel::VersionMatcher

#assert_version_match, #split_version, #version_match?

Methods included from AbstractModel::Dirty

#changed?, #changes, #clear_dirty!, #ignore_dirty, #method_missing, #set_dirty!

Methods included from AbstractModel::InterfaceAttributes

#load_interface_attribute, #load_interface_attributes, #save_interface_attribute, #save_interface_attributes, #spec_to_proc

Methods included from AbstractModel::Attributable

#attributes, #has_attribute?, included, #lazy_attribute?, #loaded_attribute?, #populate_attributes, #read_attribute, #readonly_attribute?, #write_attribute

Methods included from Logger

included, #logger, #logger_output=

Constructor Details

#initialize(parent, imedium_attachment) ⇒ MediumAttachment

Returns a new instance of MediumAttachment.



31
32
33
34
# File 'lib/virtualbox/medium_attachment.rb', line 31

def initialize(parent, imedium_attachment)
  populate_attributes({:parent => parent}, :ignore_relationships => true)
  initialize_attributes(imedium_attachment)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class VirtualBox::AbstractModel::Dirty

Class Method Details

.populate_relationship(caller, imachine) ⇒ Array<MediumAttachment>

Populates a relationship with another model.

**This method typically won’t be used except internally.**

Returns:



20
21
22
23
24
25
26
27
28
# File 'lib/virtualbox/medium_attachment.rb', line 20

def populate_relationship(caller, imachine)
  relation = Proxies::Collection.new(caller)

  imachine.medium_attachments.each do |ima|
    relation << new(caller, ima)
  end

  relation
end

Instance Method Details

#destroy(opts = {}) ⇒ Object

Destroy this medium attachment. This simply detaches the medium attachment. This will also delete the medium if that option is specified.



56
57
58
59
# File 'lib/virtualbox/medium_attachment.rb', line 56

def destroy(opts={})
  detach
  medium.destroy(opts[:destroy_medium] == :delete) if opts[:destroy_medium] && medium
end

#detachObject

Detaches the medium from it’s parent virtual machine. Note that this **does not delete** the ‘medium` which this attachment references; it merely removes the assocation of said medium with this attachment’s virtual machine.



46
47
48
49
50
51
52
# File 'lib/virtualbox/medium_attachment.rb', line 46

def detach
  parent.with_open_session do |session|
    machine = session.machine

    machine.detach_device(storage_controller.name, port, device)
  end
end

#initialize_attributes(ima) ⇒ Object



36
37
38
39
40
# File 'lib/virtualbox/medium_attachment.rb', line 36

def initialize_attributes(ima)
  load_interface_attributes(ima)
  populate_relationship(:medium, ima.medium)
  populate_relationship(:storage_controller, self)
end