Class: VirtualBox::DHCPServer

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

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_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(raw) ⇒ DHCPServer

Returns a new instance of DHCPServer



39
40
41
# File 'lib/virtualbox/dhcp_server.rb', line 39

def initialize(raw)
  initialize_attributes(raw)
end

Dynamic Method Handling

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

Class Method Details

.create(proxy, network_name) ⇒ Object

Creates a DHCP server for the given network name. This method should not be called directly, its recommended that you call the `create` method on the `dhcp_servers` relationship on `VirtualBox::Global` object.



33
34
35
36
# File 'lib/virtualbox/dhcp_server.rb', line 33

def create(proxy, network_name)
  interface = proxy.parent.lib.virtualbox.create_dhcp_server(network_name)
  new(interface)
end

.populate_relationship(caller, servers) ⇒ Array<DHCPServer>

Populates a relationship with another model.

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

Returns:



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

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

  servers.each do |interface|
    relation << new(interface)
  end

  relation
end

Instance Method Details

#added_to_relationship(proxy) ⇒ Object



50
51
52
53
# File 'lib/virtualbox/dhcp_server.rb', line 50

def added_to_relationship(proxy)
  write_attribute(:parent, proxy.parent)
  write_attribute(:parent_collection, proxy)
end

#destroyObject

Removes the DHCP server.



73
74
75
76
77
# File 'lib/virtualbox/dhcp_server.rb', line 73

def destroy
  parent.lib.virtualbox.remove_dhcp_server(interface)
  parent_collection.delete(self, true)
  true
end

#host_networkObject

Returns the host network associated with this DHCP server, if it exists.



81
82
83
84
85
86
87
# File 'lib/virtualbox/dhcp_server.rb', line 81

def host_network
  return nil unless network_name =~ /^HostInterfaceNetworking-(.+?)$/

  parent.host.network_interfaces.detect do |i|
    i.interface_type == :host_only && i.name == $1.to_s
  end
end

#initialize_attributes(raw) ⇒ Object



43
44
45
46
47
48
# File 'lib/virtualbox/dhcp_server.rb', line 43

def initialize_attributes(raw)
  write_attribute(:interface, raw)

  load_interface_attributes(interface)
  existing_record!
end

#saveObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/virtualbox/dhcp_server.rb', line 55

def save
  configs = [:ip_address, :network_mask, :lower_ip, :upper_ip]
  configs_changed = configs.map { |key| changed?(key) }.any? { |i| i }

  if configs_changed
    interface.set_configuration(ip_address, network_mask, lower_ip, upper_ip)

    # Clear the dirtiness so that the abstract model doesn't try
    # to save the attributes
    configs.each do |key|
      clear_dirty!(key)
    end
  end

  save_changed_interface_attributes(interface)
end