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:



34
35
36
37
38
39
# File 'lib/base_object.rb', line 34

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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/base_object.rb', line 146

def method_missing(method_name, *args)
  # First of all search throught array for method name
  m = search_for_method(method_name)
  if m.nil?
    if method_name == :"valid_provider?"
      return providers.any? { |p| p.keys.include? args.first.to_sym }
    end
    if method_name == :"valid_provider_url?"
      return providers.any? { |p| !p.find { |k, v| v.find { |u| u[:url] == args.first } }.nil? }
    end
    super
  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.



27
28
29
# File 'lib/base_object.rb', line 27

def base_name
  @base_name
end

#clientObject (readonly)

Returns the value of attribute client.



27
28
29
# File 'lib/base_object.rb', line 27

def client
  @client
end

#idObject (readonly)

Returns the value of attribute id.



27
28
29
# File 'lib/base_object.rb', line 27

def id
  @id
end

#objectsObject (readonly)

Returns the value of attribute objects.



28
29
30
# File 'lib/base_object.rb', line 28

def objects
  @objects
end

#urlObject (readonly) Also known as: uri

Returns the value of attribute url.



27
28
29
# File 'lib/base_object.rb', line 27

def url
  @url
end

Instance Method Details

#add_addresses!(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>


112
113
114
115
116
117
118
# File 'lib/base_object.rb', line 112

def add_addresses!(collection_name, values=[])
  @objects << {
    :type => :collection,
    :method_name => collection_name.sanitize,
    :values => values.collect { |v| { :address => v.text.strip, :type => v[:type] }}
  }
end

#add_authentication!(auth_type, values = []) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/base_object.rb', line 79

def add_authentication!(auth_type, values=[])
  value = { :key => (values/'login/keyname').text.strip } if auth_type == 'key'
  if auth_type == 'password'
    value = {
      :username => (values/'login/username').text.strip,
      :username => (values/'login/password').text.strip
    }
  end
  @objects << {
    :type => :collection,
    :method_name => 'authentication',
    :values => value
  }
end

#add_blob!(blob_name) ⇒ Object

This method adds blobs to the blob_list property of a bucket



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/base_object.rb', line 165

def add_blob!(blob_name)
  if @blob_list.nil?
    @blob_list = [blob_name]
    @objects << {
      :type => :list,
      :method_name => "blob_list",
      :value => @blob_list
    }
  else
    @blob_list << blob_name
    current = search_for_method('blob_list')
    current[:value] = @blob_list
  end
end

#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>


125
126
127
128
129
130
131
# File 'lib/base_object.rb', line 125

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



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

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”/>



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/base_object.rb', line 43

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_provider!(provider_id, entrypoints) ⇒ Object



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

def add_provider!(provider_id, entrypoints)
  @providers ||= []
  @providers << {
    provider_id.intern => entrypoints.map { |e| { :kind => e[:kind], :url => e.text } }
  }
  @objects << {
    :type => :collection,
    :method_name => 'providers',
    :values => @providers
  }
end

#add_text!(object_name, value) ⇒ Object

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



71
72
73
74
75
76
77
# File 'lib/base_object.rb', line 71

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



135
136
137
138
139
140
141
142
143
144
# File 'lib/base_object.rb', line 135

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]
    when :list then return m[:value].join(", ")
    else raise NoHandlerForMethod
  end
end