module CloudFormer::MakesJson

Public Class Methods

included(base) click to toggle source
# File lib/cloud_former/makes_json.rb, line 8
def self.included(base)
  base.extend ClassMethods
  base.class_variable_set('@@json_attributes', [])
  base.class_variable_set('@@embed_properties', false)
end

Public Instance Methods

dump_json() click to toggle source
# File lib/cloud_former/makes_json.rb, line 14
def dump_json
  res = {}
  self.class.class_variable_get('@@json_attributes').each do |att|
    use_name = att[:options][:name] || ActiveSupport::Inflector.camelize(att[:name])
    res[use_name] = self.send(att[:name])
  end

  properties = {}

  self.class.aws_properties.try(:each) do |property|
    val = property.as_json_for(self)
    if !val.nil?
      use_name = property.options[:name]
      use_name ||= ActiveSupport::Inflector.camelize(property.get_name)
      properties[use_name] = val
    end
  end

  attribute_fill = res
  if self.class.get_aws_attribute_wrapper
    attribute_fill = {}
  end

  self.class.aws_attributes.try(:each) do |attribute|
    val = attribute.as_json_for(self)
    if !val.nil?
      use_name = attribute.options[:name]
      use_name ||= ActiveSupport::Inflector.camelize(attribute.get_name)
      attribute_fill[use_name] = val
    end
  end

  if self.class.get_aws_attribute_wrapper
    res[self.class.get_aws_attribute_wrapper] = attribute_fill
  end

  if properties.any?
    res['Properties'] = properties
  end

  if respond_to?('metadata_items') && metadata_items.any?
    metadata = {}
    metadata_items.each do |meta|
      metadata[meta.aws_type] = meta.dump_json
    end
    res['Metadata'] = metadata
  end

  res
end