class AzureResourceDynamicMethods

@since 0.2.0
@author Russell Seymour
reused easily.
rather than itself. This means that there is less duplication of code and it can be
The class is a helper class essentially as it creates the methods on the calling class
information returned from Azure.
to be testable. To do this no methods are hardcoded, each on is craeted based on the
Each of the Azure Resources have different attributes and properties, and they all need
Class to create methods on the calling object at run time.

def create_method(object, name, value)

Parameters:
  • variant () -- value The value that needs to be returned by the method
  • string () -- name The name of the method
  • AzureResourceProbe|AzureResource () -- object Object on which the methods need to be created

Other tags:
    Private: -
def create_method(object, name, value)
  # Create the necessary method based on the var that has been passed
  # Test the value for its type so that the method can be setup correctly
  case value.class.to_s
  when 'String', 'Integer', 'TrueClass', 'FalseClass', 'Fixnum'
    object.define_singleton_method name do
      value
    end
  when 'Hash'
    value.count.zero? ? return_value = value : return_value = AzureResourceProbe.new(value)
    object.define_singleton_method name do
      return_value
    end
  when /^Azure::Resources::Mgmt::.*::Models::ResourceGroupProperties$/
    # This is a special case where the properties of the resource group is not a simple JSON model
    # This is because the plugin is using the Azure SDK to get this information so it is an SDK object
    # that has to be interrogated in a different way. This is the only object type that behaves like this
    value.instance_variables.each do |var|
      create_method(object, var.to_s.delete('@'), value.instance_variable_get(var))
    end
  when 'Array'
    # Some things are just string or integer arrays
    # Check this by seeing if the first element is a string / integer / boolean or
    # a hashtable
    # This may not be the best methid, but short of testing all elements in the array, this is
    # the quickest test
    case value[0].class.to_s
    when 'String', 'Integer', 'TrueClass', 'FalseClass', 'Fixnum'
      probes = value
    else
      probes = []
      value.each do |value_item|
        probes << AzureResourceProbe.new(value_item)
      end
    end
    object.define_singleton_method name do
      probes
    end
  end
end

def create_methods(object, data)

Parameters:
  • variant () -- data The data from which the methods should be created
  • AzureResourceProbe|AzureResource () -- object The object on which the methods should be craeted
def create_methods(object, data)
  # Check the type of data as this affects the setup of the methods
  # If it is an Azure Generic Resource then setup methods for each of
  # the instance variables
  case data.class.to_s
  when /^Azure::Resources::Mgmt::.*::Models::GenericResource$/,
       /^Azure::Resources::Mgmt::.*::Models::ResourceGroup$/
    # iterate around the instance variables
    data.instance_variables.each do |var|
      create_method(object, var.to_s.delete('@'), data.instance_variable_get(var))
    end
  # When the data is a Hash object iterate around each of the key value pairs and
  # craete a method for each one.
  when 'Hash'
    data.each do |key, value|
      create_method(object, key, value)
    end
  end
end