Class: DeltaCloud::BaseObject

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

Overview

BaseObject model basically provide the basic operation around REST model, like defining a links between different objects, element with text values, or collection of these elements

Direct Known Subclasses

ActionObject

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) {|_self| ... } ⇒ BaseObject

For initializing new object you require to set id, url, client and name attribute.

Yields:

  • (_self)

Yield Parameters:

Raises:



37
38
39
40
41
42
# File 'lib/base_object.rb', line 37

def initialize(opts={}, &block)
  @id, @url, @client, @base_name = opts[:id], opts[:url], opts[:client], opts[:name]
  @objects = []
  raise BaseObjectParamError if @id.nil? or @url.nil? or @client.nil? or @base_name.nil?
  yield self if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/base_object.rb', line 107

def method_missing(method_name, *args)
  # First of all search throught array for method name
  m = search_for_method(method_name)
  if m.nil?
    warn "[WARNING] Method unsupported by API: '#{self.class}.#{method_name}(#{args.inspect})'"
    return nil
  else
    # Call appropriate handler for method
    method_handler(m, args)
  end
end

Instance Attribute Details

#base_nameObject (readonly)

Returns the value of attribute base_name.



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

def base_name
  @base_name
end

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#objectsObject (readonly)

Returns the value of attribute objects.



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

def objects
  @objects
end

#urlObject (readonly) Also known as: uri

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#add_collection!(collection_name, values = []) ⇒ Object

This method define collection of text elements inside REST model XML syntax: <addresses>

  <address>127.0.0.1</address>
  <address>127.0.0.2</address>
</addresses>


87
88
89
90
91
92
93
# File 'lib/base_object.rb', line 87

def add_collection!(collection_name, values=[])
  @objects << {
    :type => :collection,
    :method_name => collection_name.sanitize,
    :values => values
  }
end

#add_hwp_property!(name, property, type) ⇒ Object

Method add property for hardware profile



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/base_object.rb', line 60

def add_hwp_property!(name, property, type)
  hwp_property=case type
    when :float then DeltaCloud::HWP::FloatProperty.new(property, name)
    when :integer then DeltaCloud::HWP::Property.new(property, name)
  end
  @objects << {
    :type => :property,
    :method_name => name.sanitize,
    :property => hwp_property
  }
end

#add_link!(object_name, id) ⇒ Object

This method add link to another object in REST model XML syntax: <link rel=“destroy” href=“localhost/api/resource” method=“post”/>



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/base_object.rb', line 46

def add_link!(object_name, id)
  @objects << {
    :type => :link,
    :method_name => object_name.sanitize,
    :id => id
  }
  @objects << {
    :type => :text,
    :method_name => "#{object_name.sanitize}_id",
    :value => id
  }
end

#add_text!(object_name, value) ⇒ Object

This method define text object in REST model XML syntax: <name>Instance 1</name>



74
75
76
77
78
79
80
# File 'lib/base_object.rb', line 74

def add_text!(object_name, value)
  @objects << {
    :type => :text,
    :method_name => object_name.sanitize,
    :value => value
  }
end

#method_handler(m, args = []) ⇒ Object

Basic method hander. This define a way how value from property will be returned



97
98
99
100
101
102
103
104
105
# File 'lib/base_object.rb', line 97

def method_handler(m, args=[])
  case m[:type]
    when :link then return @client.send(m[:method_name].singularize, m[:id])
    when :text then return m[:value]
    when :property then return m[:property]
    when :collection then return m[:values]
    else raise NoHandlerForMethod
  end
end