Class: VirtualBox::Image Abstract

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

Overview

This class is abstract.

An abstract class which encapsulates the shared behaviour of images such as HardDrive and DVD.

Attributes

All images expose the following attributes. If you don't know how to read this than read Attributable.

attribute :uuid, :readonly => true
attribute :location
attribute :accessible, :readonly => true

Direct Known Subclasses

DVD, HardDrive

Class Method Summary

Instance Method Summary

Methods included from SubclassListing

included

Methods inherited from AbstractModel

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

Methods included from AbstractModel::Attributable

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

Methods included from AbstractModel::Dirty

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

Methods included from AbstractModel::Relatable

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

Methods included from AbstractModel::Validatable

#add_error, #clear_errors, #errors, #valid?, #validate, #validates_presence_of

Constructor Details

- (Image) initialize(info = nil)

This should never be called directly on Image. Instead, initialize one of the subclasses.



104
105
106
107
108
# File 'lib/virtualbox/image.rb', line 104

def initialize(info=nil)
  super()

  populate_attributes(info) if info
end

Dynamic Method Handling

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

Class Method Details

+ (Hash) parse_block(block)

Parses a single block from VirtualBox output.

This method typically won't be used except internally.

Returns:

  • (Hash)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/virtualbox/image.rb', line 50

def parse_block(block)
  return nil unless block =~ /^UUID:/i
  hd = {}

  # Parses each line which should be in the format:
  # KEY: VALUE
  block.split("\n").each do |line|
    next unless line =~ /^(.+?):\s+(.+?)$/
    hd[$1.downcase.to_sym] = $2.to_s
  end

  # If we don't have a location but have a path, use that, as they
  # are equivalent but not consistent.
  hd[:location] = hd[:path] if hd.has_key?(:path)

  hd
end

+ (Array<Hash>) parse_blocks(raw)

Parses the blocks of the output from virtualbox. VirtualBox outputs image listing in "blocks" which are then parsed down to their attributes.

This method typically won't be used except internally.

Returns:

  • (Array<Hash>)


41
42
43
# File 'lib/virtualbox/image.rb', line 41

def parse_blocks(raw)
  raw.split(/\n\n/).collect { |v| parse_block(v.chomp) }.compact
end

+ (Array<Image>) parse_raw(raw)

Parses the raw output of virtualbox into image objects. Used by subclasses to parse the output of their respective listing functions.

This method typically won't be used except internally.

Returns:



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

def parse_raw(raw)
  parse_blocks(raw).collect { |v| new(v) }
end

+ (Array<Image>) populate_relationship(caller, data)

Searches the subclasses which implement all method, searching for a matching UUID and returning that as the relationship.

This method typically won't be used except internally.

Returns:



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/virtualbox/image.rb', line 74

def populate_relationship(caller, data)
  return DVD.empty_drive if data[:uuid].nil?

  subclasses.each do |subclass|
    next unless subclass.respond_to?(:all)

    matching = subclass.all.find { |obj| obj.uuid == data[:uuid] }
    return matching unless matching.nil?
  end

  nil
end

+ (Image) set_relationship(caller, old_value, new_value)

Sets an image onto a relationship and/or removes it from a relationship. This method is automatically called by Relatable.

This method typically won't be used except internally.



93
94
95
96
97
98
99
# File 'lib/virtualbox/image.rb', line 93

def set_relationship(caller, old_value, new_value)
  # We don't actually destroy any images using this method,
  # so just return the new value as long as its a valid object
  raise Exceptions::InvalidRelationshipObjectException.new if new_value && !new_value.is_a?(Image)

  return new_value
end

Instance Method Details

- (Boolean) empty_drive?

Returns boolean showing if empty drive or not. This method should be overriden by any subclass and is expected to return true of false showing if this image represents an empty drive of whatever type the subclass is.

Returns:

  • (Boolean)


126
127
128
# File 'lib/virtualbox/image.rb', line 126

def empty_drive?
  false
end

- (String) filename

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

Returns:

  • (String)


133
134
135
# File 'lib/virtualbox/image.rb', line 133

def filename
  File.basename(location.to_s)
end

- (String) image_type

The image type as a string for the virtualbox command line. This method should be overridden by any subclass and is expected to return the type which is used in command line parameters for attaching to storage controllers.

Returns:

  • (String)


116
117
118
# File 'lib/virtualbox/image.rb', line 116

def image_type
  raise "This must be implemented by any subclasses"
end