class SoftLayer::ModelBase

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.

Attributes

id[R]

The unique identifier of this object within its API service

softlayer_client[R]

The client environment that this model object belongs to

softlayer_hash[R]

The #softlayer_hash stores the low-level information about an object as it was retrieved from the SoftLayer API.

Public Class Methods

new(softlayer_client, network_hash) click to toggle source

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)

# 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
sl_attr(attribute_symbol, hash_key = nil) click to toggle source

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.

# 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

Public Instance Methods

[](softlayer_property) click to toggle source

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.

# File lib/softlayer/ModelBase.rb, line 75
def [](softlayer_property)
  self.softlayer_hash[softlayer_property.to_s]
end
has_sl_property?(softlayer_property) click to toggle source

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

# 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) click to toggle source

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.

# File lib/softlayer/ModelBase.rb, line 65
def refresh_details(object_mask = nil)
  @softlayer_hash = self.softlayer_properties(object_mask)
end
service() click to toggle source

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.

# File lib/softlayer/ModelBase.rb, line 54
def service
  raise "Abstract method service in ModelBase was called"
end
to_ary() click to toggle source

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

# File lib/softlayer/ModelBase.rb, line 102
def to_ary()
  return nil;
end

Protected Instance Methods

softlayer_properties(object_mask = nil) click to toggle source

Subclasses should implement this method as part of enabling the #refresh_details functionality 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.

# File lib/softlayer/ModelBase.rb, line 115
def softlayer_properties(object_mask = nil)
  raise "Abstract method softlayer_properties in ModelBase was called"
end