Class: VMware::DataObject

Inherits:
Object
  • Object
show all
Defined in:
lib/vmware/objects/data_object.rb

Overview

Wrapper for data object instances used to enumerate their properties.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ DataObject

Returns a new instance of DataObject.

[View source]

7
8
9
10
11
12
# File 'lib/vmware/objects/data_object.rb', line 7

def initialize(values)
  @values = values
  
  # We cache properties for more efficient access.
  @cached_properties = nil
end

Class Method Details

.dump(obj) ⇒ Object

Dump a data object to a string.

[View source]

56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vmware/objects/data_object.rb', line 56

def self.dump(obj)
  if self.is_base_class(obj)
    obj
  elsif obj.is_a? Array
    items = obj.collect {|o| self.dump(o) }
    "[#{items.join(', ')}]"
  else
    s = []
    obj.properties.each {|k,v| s << "#{k} => #{self.dump(v)}" }
    "{#{s.join(', ')}}"
  end
end

.is_base_class(val) ⇒ Object

[View source]

69
70
71
# File 'lib/vmware/objects/data_object.rb', line 69

def self.is_base_class(val)
  val.nil? or val.is_a? String or val.is_a? TrueClass or val.is_a? FalseClass or val.is_a? Numeric or val.is_a? DateTime
end

.soap_to_data_object(val) ⇒ Object

Convert a VMware SOAP object to a data_object wrapper or basic ruby object.

[View source]

43
44
45
46
47
48
49
50
51
# File 'lib/vmware/objects/data_object.rb', line 43

def self.soap_to_data_object(val)
  if self.is_base_class(val)
    return val
  elsif val.is_a? Array
    return val.collect {|v| self.soap_to_data_object(v) }
  else
    return VMware::DataObject.new(val)
  end
end

Instance Method Details

#[](key) ⇒ Object

Return a specific property.

[View source]

35
36
37
38
# File 'lib/vmware/objects/data_object.rb', line 35

def [](key)
  p = properties
  p[key.to_s]
end

#properties(prop_list = []) ⇒ Object

Retrieve all of the properties from the object as a hash.

[View source]

17
18
19
20
21
22
23
24
25
26
27
# File 'lib/vmware/objects/data_object.rb', line 17

def properties(prop_list = [])
  return @cached_properties if @cached_properties
  
  prop_hash = {}
  @values.instance_variables.each do |var|
    val = @values.instance_eval(var)
    prop_hash[var.gsub(/^@/, "")] = VMware::DataObject::soap_to_data_object(val)
  end
  
  @cached_properties = prop_hash
end

#soap_typeObject

[View source]

28
29
30
# File 'lib/vmware/objects/data_object.rb', line 28

def soap_type
  @values.class.to_s
end