Class: SoftLayer::ModelBase

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

Overview

The SoftLayer Gem defines an Object Hierarchy representing entities in an account’s SoftLayer environment. This class is the base object class for objects in that hierarchy

The SoftLayer API represents entities as a hash of properties. This class stores that hash and allows the use of subscripting to access those properties directly.

The class also has a model for making network requests that will refresh the stored hash so that it reflects the most up-to-date information about an entity from the server. Subclasses should override softlayer_properties to retrieve information from the server. Client code should call refresh_details to ask an object to update itself.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(softlayer_client, network_hash) ⇒ ModelBase

Construct a new model object in the environment of the given client and with the given hash of network data (presumably returned by the SoftLayer API)

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
# File 'lib/softlayer/ModelBase.rb', line 33

def initialize(softlayer_client, network_hash)
  raise ArgumentError, "A hash is required" if nil == network_hash
  raise ArgumentError, "Model objects must be created in the context of a client" if nil == softlayer_client

  @softlayer_client = softlayer_client
  @softlayer_hash = network_hash

  raise ArgumentError, "The hash used to construct a softlayer model object must have an id" unless has_sl_property?(:id)
  raise ArgumentError, "id must be non-nil and non-empty" unless self[:id]
end

Instance Attribute Details

#softlayer_clientObject (readonly)

The client environment that this model object belongs to



25
26
27
# File 'lib/softlayer/ModelBase.rb', line 25

def softlayer_client
  @softlayer_client
end

Class Method Details

.sl_attr(attribute_symbol, hash_key = nil) ⇒ Object

allows subclasses to define attributes as sl_attr sl_attr are attributes that draw their value from the low-level hash representation of the object.



89
90
91
92
93
94
# File 'lib/softlayer/ModelBase.rb', line 89

def self.sl_attr(attribute_symbol, hash_key = nil)
  raise "The sl_attr expects a symbol for the attribute to define" unless attribute_symbol.kind_of?(Symbol)
  raise "The hash key used to define an attribute cannot be empty" if hash_key && hash_key.empty?

  define_method(attribute_symbol.to_sym) { self[hash_key ? hash_key : attribute_symbol.to_s]}
end

Instance Method Details

#[](softlayer_property) ⇒ Object

Returns the value of of the given property as stored in the softlayer_hash. This gives you access to the low-level, raw properties that underlie this model object. The need for this is not uncommon, but using this method should still be done with deliberation.



75
76
77
# File 'lib/softlayer/ModelBase.rb', line 75

def [](softlayer_property)
  self.softlayer_hash[softlayer_property.to_s]
end

#has_sl_property?(softlayer_property) ⇒ Boolean

Returns true if the given property can be found in the softlayer hash

Returns:

  • (Boolean)


81
82
83
# File 'lib/softlayer/ModelBase.rb', line 81

def has_sl_property?(softlayer_property)
  softlayer_hash && softlayer_hash.has_key?(softlayer_property.to_s)
end

#refresh_details(object_mask = nil) ⇒ Object

Asks a model object to reload itself from the SoftLayer API.

Subclasses should not override this method, rather they should implement softlayer_properties to actually make the API request and return the new hash.



65
66
67
# File 'lib/softlayer/ModelBase.rb', line 65

def refresh_details(object_mask = nil)
  @softlayer_hash = self.softlayer_properties(object_mask)
end

#serviceObject

The service method of a Model object should return a SoftLayer Service that best represents the modeled object. For example, a Ticket models a particular entity in the SoftLayer_Ticket service. The particular entity is identified by its id so the Ticket class would return

softlayer_client[:Ticket].object_with_id

which is a service which would allow calls to the ticket service through that particular object.



54
55
56
# File 'lib/softlayer/ModelBase.rb', line 54

def service
  raise "Abstract method service in ModelBase was called"
end

#to_aryObject

When printing to the console using puts, ruby will call the to_ary method trying to convert an object into an array of lines for stdio. We override to_ary to return nil for model objects so they may be printed



102
103
104
# File 'lib/softlayer/ModelBase.rb', line 102

def to_ary()
  return nil;
end