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)


49
50
51
52
53
54
55
56
57
58
# File 'lib/softlayer/ModelBase.rb', line 49

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



41
42
43
# File 'lib/softlayer/ModelBase.rb', line 41

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.



116
117
118
119
120
121
# File 'lib/softlayer/ModelBase.rb', line 116

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 underly this model object. The need for this is not uncommon, but using this method should still be done with deliberation.



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

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)


108
109
110
# File 'lib/softlayer/ModelBase.rb', line 108

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.



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

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.



70
71
72
# File 'lib/softlayer/ModelBase.rb', line 70

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

#softlayer_properties(object_mask = nil) ⇒ Object

Subclasses should implement this method as part of enabling the refresh_details fuctionality The implementation should make a request to the SoftLayer API and retrieve an up-to-date SoftLayer hash representation of this object. That hash should be the return value of this routine.



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

def softlayer_properties(object_mask = nil)
  raise "Abstract method softlayer_properties 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



129
130
131
# File 'lib/softlayer/ModelBase.rb', line 129

def to_ary()
  return nil;
end