Class: VBox::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/virtualbox/base.rb

Direct Known Subclasses

ManagedObjectRef, WebsessionManager

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj_ref) ⇒ Base

Returns a new instance of Base.



6
7
8
# File 'lib/virtualbox/base.rb', line 6

def initialize(obj_ref)
  @ref = obj_ref
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/virtualbox/base.rb', line 75

def method_missing(name, *args)
  name = fix_savon_method_name name
  if name.end_with? '='
    name = name.chop
    savon_method = soap_method(name, 'set')
    if WebService.operations.include?(savon_method)
      savon_tag = fix_savon_tag_name(savon_method) || name
      soap_body = _this.merge(savon_tag => args[0])
    else
      return super
    end
  else
    if WebService.operations.include?(soap_method(name, 'get'))
      savon_method = soap_method(name, 'get')
      soap_body = _this
    elsif WebService.operations.include?(soap_method(name))
      savon_method = soap_method(name)
      args_hash = ensure_hash(args[-1] || {})
      soap_body = _this.merge(args_hash.referize!)
    else
      return super
    end
  end
  request = WebService.send_request(savon_method, soap_body)
  process_result(request, arrayize?(savon_method))
end

Instance Attribute Details

#refObject (readonly)

Returns the value of attribute ref.



4
5
6
# File 'lib/virtualbox/base.rb', line 4

def ref
  @ref
end

Class Method Details

.class_nameObject



30
31
32
# File 'lib/virtualbox/base.rb', line 30

def self.class_name
  self.name.split('::').last.to_underscore
end

.soap_method(method_name, modifier = nil) ⇒ Object



20
21
22
23
24
# File 'lib/virtualbox/base.rb', line 20

def self.soap_method(method_name, modifier=nil)
  is_acronym_part = self.name.split('::').last.match(/^[A-Z]{2,}/).nil? ? '_' : ''
  modifier_part = modifier.nil? ? '' : modifier + '_'
  "i#{is_acronym_part}#{class_name}_#{modifier_part}#{method_name}".to_sym
end

Instance Method Details

#_thisObject



10
11
12
13
14
# File 'lib/virtualbox/base.rb', line 10

def _this
  _this = {}
  _this[:_this] = @ref unless @ref.nil?
  _this
end

#arrayize?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/virtualbox/base.rb', line 71

def arrayize?(method_name)
  WebService::BROKEN_ARRAYS.include? method_name
end

#class_nameObject



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

def class_name
  self.class.class_name
end

#ensure_hash(args) ⇒ Object



16
17
18
# File 'lib/virtualbox/base.rb', line 16

def ensure_hash(args)
  args.is_a?(Hash) ? args : raise(ArgumentError, 'Method arguments must be a hash')
end

#fix_savon_method_name(method_name) ⇒ Object



60
61
62
63
64
65
# File 'lib/virtualbox/base.rb', line 60

def fix_savon_method_name(method_name)
  method_name.
      gsub('_3d_', '3_d_').
      gsub('_2d_', '2_d_').
      gsub('_ipv6_', '_i_pv6_')
end

#fix_savon_tag_name(method_name) ⇒ Object



67
68
69
# File 'lib/virtualbox/base.rb', line 67

def fix_savon_tag_name(method_name)
  WebService::BROKEN_TAGS[method_name] if WebService::BROKEN_TAGS.include?(method_name)
end

#process_result(result, force_array = false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/virtualbox/base.rb', line 38

def process_result(result, force_array=false)
  if force_array
    return [] if result.nil?
    result = result.to_a
    vbox_class = result.first.vbox_class
    result.map { |item| item.to_vbox_object(vbox_class) }
  else
    return if result.nil?
    if result.is_a?(Array)
      result.map { |item| process_result(item) }
    elsif result.is_a?(Hash)
      result.update(result) { |_, value| process_result(value) }
    elsif !!result == result
      result
    elsif !result.match(/^[0-9a-f]{16}-[0-9a-f]{16}$/).nil?
      result.to_vbox_object(result.vbox_class)
    else
      result.to_num
    end
  end
end

#soap_method(method_name, modifier = nil) ⇒ Object



26
27
28
# File 'lib/virtualbox/base.rb', line 26

def soap_method(method_name, modifier=nil)
  self.class.soap_method(method_name, modifier)
end