class Hash

This extension to the Hash class to allows object masks to be constructed from built-in Ruby types and converted to object masks strings for presentation to the SoftLayer API

Public Instance Methods

_to_sl_object_mask_property() click to toggle source

Returns a string representing the hash as a property within a larger object mask. This routine is an implementation detail used in the conversion of hashes to object mask strings. You should not have to call this method directly.

# File lib/softlayer/object_mask_helpers.rb, line 29
def _to_sl_object_mask_property()
  key_strings = __sl_object_mask_properties_for_keys();
  "#{key_strings.join(',')}"
end
to_sl_object_mask() click to toggle source

Given a hash, generate an Object Mask string from the structure found within the hash. This allows object masks to be constructed as hashes, then converted to strings when they must be passed to the API. The routine does some very rudimentary validation to ensure that the hash represents a valid object mask, but care must still be taken when constructing the hash.

# File lib/softlayer/object_mask_helpers.rb, line 18
def to_sl_object_mask()
  raise RuntimeError, "An object mask must contain properties" if empty?
  raise RuntimeError, "An object mask must start with root properties" if keys().find { |key| !__valid_root_property_key?(key) }

  key_strings = __sl_object_mask_properties_for_keys();
  key_strings.count > 1 ? "[#{key_strings.join(',')}]" : "#{key_strings[0]}"
end

Private Instance Methods

__sl_object_mask_properties_for_keys() click to toggle source
# File lib/softlayer/object_mask_helpers.rb, line 40
def __sl_object_mask_properties_for_keys
  key_strings = [];

  each do |key, value|
    return "" if !value

    string_for_key = key._to_sl_object_mask_property

    if value.kind_of?(String) || value.kind_of?(Symbol) then
      string_for_key = "#{string_for_key}.#{value._to_sl_object_mask_property}"
    end

    if value.kind_of?(Array) || value.kind_of?(Hash) then
      value_string = value._to_sl_object_mask_property
      if value_string && !value_string.empty?
        string_for_key = "#{string_for_key}[#{value_string}]"
      end
    end

    key_strings.push(string_for_key)
  end

  key_strings
end
__valid_root_property_key?(key_string) click to toggle source
# File lib/softlayer/object_mask_helpers.rb, line 36
def __valid_root_property_key?(key_string)
  return key_string == "mask" || (0 == (key_string =~ /\Amask\([a-z][a-z0-9_]*\)\z/i))
end