Class: VirtualBox::Medium

Inherits:
AbstractModel show all
Includes:
SubclassListing
Defined in:
lib/virtualbox/medium.rb

Overview

Represents a medium object part of VirtualBox. A medium is a hard drive, DVD, floppy disk, etc. Each of these share common properties represented here.

Direct Known Subclasses

DVD, HardDrive

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SubclassListing

included

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(imedium) ⇒ Medium

Initializes a medium object, retrieving the attributes and information from the COM::Interface::Medium object given as the parameter. This initialization is done automatically by virtualbox when populating a relationship. Mediums should never be initialized manually.

Parameters:

  • imedium (COM::Interface::Medium)


91
92
93
94
# File 'lib/virtualbox/medium.rb', line 91

def initialize(imedium)
  write_attribute(:interface, imedium)
  initialize_attributes(imedium)
end

Dynamic Method Handling

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

Class Method Details

.device_typeSymbol

Specifies the device type that a VirtualBox::Medium class is interested in. This is ‘:all` on VirtualBox::Medium, but is expected to be overridden by child classes. The value returned should be one of COM::Interface::DeviceType.

Returns:

  • (Symbol)


80
81
82
# File 'lib/virtualbox/medium.rb', line 80

def device_type
  :all
end

.populate_array_relationship(caller, media) ⇒ Array<Medium>

Populates a relationship which has many media.

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

Returns:



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

def populate_array_relationship(caller, media)
  relation = Proxies::Collection.new(caller)

  media.each do |medium|
    # Skip media this class isn't interested in
    next if device_type != :all && medium.device_type != device_type

    # Wrap it up and add to the relationship
    relation << new(medium)
  end

  relation
end

.populate_relationship(caller, media) ⇒ Object

Populates a relationship with another model. Depending on the data sent through as the ‘media` parameter, this can either return a single value or an array of values. Global, for example, has a relationship of media, while a VirtualBox::MediumAttachment has a relationship with a single medium.

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



23
24
25
26
27
28
29
30
31
# File 'lib/virtualbox/medium.rb', line 23

def populate_relationship(caller, media)
  if media.is_a?(Array)
    # has many relationship
    populate_array_relationship(caller, media)
  else
    # has one relationship
    populate_single_relationship(caller, media)
  end
end

.populate_single_relationship(caller, medium) ⇒ Medium

Populates a relationship which has one medium. This method goes one step further and instantiates the proper class for the type of medium given. For example, given a ‘device_type` of `:hard_drive`, it would return a HardDrive object.

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

Returns:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/virtualbox/medium.rb', line 60

def populate_single_relationship(caller, medium)
  return nil if medium.nil?

  subclasses.each do |subclass|
    # Skip this class unless the device type matches
    next unless subclass.device_type == medium.device_type

    # Wrap it up and return it
    return subclass.new(medium)
  end

  # If all else fails, just wrap it in a Medium
  new(medium)
end

Instance Method Details

#destroy(destroy_backing = false) ⇒ Object

Destroys the medium, optionally also phsyically destroying the backing storage. This removes this medium from the VirtualBox media registry. In order to remove the medium, it must not be attached to any virtual machine. If it is, an exception of some sort will be raised. This action happens immediately when the method is called, and is not deferred to a save.



129
130
131
132
133
134
135
# File 'lib/virtualbox/medium.rb', line 129

def destroy(destroy_backing=false)
  if destroy_backing
    destroy_storage
  else
    interface.close
  end
end

#destroy_storageObject

Destroys the backing store of the medium and the media registry. This is analagous to calling #destroy with the first parameter set to true. This method requires that the medium not be attached to any virtual machine (running or not).



141
142
143
# File 'lib/virtualbox/medium.rb', line 141

def destroy_storage
  interface.delete_storage.wait_for_completion(-1)
end

#filenameString

Returns the basename of the images location (the file name +extension)

Returns:

  • (String)


119
120
121
# File 'lib/virtualbox/medium.rb', line 119

def filename
  File.basename(location.to_s)
end

#initialize_attributes(imedium) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/virtualbox/medium.rb', line 96

def initialize_attributes(imedium)
  # First refresh the state (required for many attributes)
  imedium.refresh_state

  # Load interface attributes
  load_interface_attributes(imedium)

  # Clear dirtiness, since this should only be called initially and
  # therefore shouldn't affect dirtiness
  clear_dirty!

  # But this is an existing record
  existing_record!
end

#load_relationship(name) ⇒ Object



111
112
113
114
# File 'lib/virtualbox/medium.rb', line 111

def load_relationship(name)
  # Populate children
  populate_relationship(name, interface.children)
end