Class: DeltaCloud::StatefulObject

Inherits:
ActionObject show all
Defined in:
lib/base_object.rb

Instance Attribute Summary collapse

Attributes inherited from BaseObject

#base_name, #client, #id, #objects, #url

Instance Method Summary collapse

Methods inherited from ActionObject

#action_urls, #actions, #add_action_link!, #base_method_handler

Methods inherited from BaseObject

#add_collection!, #add_hwp_property!, #add_link!, #add_text!, #method_missing

Constructor Details

#initialize(opts = {}, &block) ⇒ StatefulObject

Returns a new instance of StatefulObject.



198
199
200
201
202
# File 'lib/base_object.rb', line 198

def initialize(opts={}, &block)
  super(opts)
  @state = opts[:initial_state] || ''
  add_default_states!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class DeltaCloud::BaseObject

Instance Attribute Details

#stateObject (readonly)

Returns the value of attribute state.



196
197
198
# File 'lib/base_object.rb', line 196

def state
  @state
end

Instance Method Details

#action_method_handlerObject



234
# File 'lib/base_object.rb', line 234

alias :action_method_handler :method_handler

#action_objectsObject



253
254
255
# File 'lib/base_object.rb', line 253

def action_objects
  @objects.select { |o| o[:type] == :action_link }
end

#action_trigger(action) ⇒ Object



227
228
229
230
231
232
# File 'lib/base_object.rb', line 227

def action_trigger(action)
  # Refresh object state after action
  @new_state_object = @client.send(self.base_name, self.id)
  @state = @new_state_object.state
  self.update_actions!
end

#add_default_states!Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/base_object.rb', line 204

def add_default_states!
  @objects << {
    :method_name => 'stopped?',
    :type => :state,
    :state => 'STOPPED'
  }
  @objects << {
    :method_name => 'running?',
    :type => :state,
    :state => 'RUNNING'
  }
  @objects << {
    :method_name => 'pending?',
    :type => :state,
    :state => 'PENDING'
  }
  @objects << {
    :method_name => 'shutting_down?',
    :type => :state,
    :state => 'SHUTTING_DOWN'
  }
end

#evaluate_state(method_state, current_state) ⇒ Object

private



249
250
251
# File 'lib/base_object.rb', line 249

def evaluate_state(method_state, current_state)
  method_state.eql?(current_state)
end

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



236
237
238
239
240
241
242
243
244
245
# File 'lib/base_object.rb', line 236

def method_handler(m, args=[])
  begin
    action_method_handler(m, args)
  rescue NoHandlerForMethod
    case m[:type]
      when :state then evaluate_state(m[:state], @state)
      else raise NoHandlerForMethod
    end
  end
end

#update_actions!Object



257
258
259
260
261
# File 'lib/base_object.rb', line 257

def update_actions!
  new_actions = @new_state_object.action_objects
  @objects.reject! { |o| o[:type] == :action_link }
  @objects = (@objects + new_actions)
end