class Softlayer::Generator::ClassFile

Public Class Methods

new(name) click to toggle source
# File lib/softlayer/generator/class_file.rb, line 4
def initialize(name)
  @softlayer_name = name.dup
  @methods = Softlayer::Generator::Service.new(name).representation_hash
  @name = Converter.type(name)
  @parent = Softlayer::Generator::DataType.parent_for(@softlayer_name)
  @autoload = Softlayer::Generator::DataType.autoload_for(@softlayer_name)
end

Public Instance Methods

count_steps(name) click to toggle source
# File lib/softlayer/generator/class_file.rb, line 187
def count_steps(name)
  name.split("::").size
end
generate() click to toggle source
# File lib/softlayer/generator/class_file.rb, line 12
def generate
  content = generate_header
  content << generate_service_constant
  content << generate_autoload
  content << generate_attributes
  content << generate_methods
  content << generate_representer
  content << generate_footer
end
generate_attributes() click to toggle source
# File lib/softlayer/generator/class_file.rb, line 80
def generate_attributes
  attrs = ""
  steps = count_steps(@name)
  Softlayer::Generator::DataType.attributes_for(@softlayer_name).each_pair do |attribute, type|
    steps.times { attrs << "  " }
    attrs << "attr_accessor :#{attribute}\n"
  end
  attrs << "\n"
  attrs
end
generate_autoload() click to toggle source
# File lib/softlayer/generator/class_file.rb, line 30
def generate_autoload
  return "" if @autoload.nil?
  steps = count_steps(@name)
  autoload = ""
  @autoload.each do |file|
    steps.times { autoload << "  " }
    autoload << "autoload :#{file[0]}, '#{file[1]}'\n"
  end
  autoload
end
generate_header() click to toggle source
# File lib/softlayer/generator/class_file.rb, line 51
def generate_header
  header = ""
  full_name = ""
  step = 0
  total_steps = @name.split("::").size
  @name.split("::").each do |class_name|
    # iteration setup
    full_name << class_name
    step.times { header << "  " }

    # add to header
    object_type = object_type(full_name)
    header << object_type + " " + class_name

    # if have a parent, inherit
    if @parent
      header << " < #{Converter.type(@parent)}" if (step == total_steps - 1)
    else
      header << " < Softlayer::Model" if (step == total_steps - 1)
    end
    header << "\n"

    # iteration teardown
    step = step + 1
    full_name << "::"
  end
  header
end
generate_method(name, hash) click to toggle source
# File lib/softlayer/generator/class_file.rb, line 144
def generate_method(name, hash)
  method = String.new

  # generate method comment
  unless hash[:input].empty?
    hash[:input].each do |param|
      count_steps(@name).times { method << "  " }
      method << "# #{param[:name]}\n"
    end
  end

  # generate method content
  count_steps(@name).times { method << "  " }
  method << "def "
  method << "self." if hash[:method_scope] == :class
  method << "#{name}"
  if has_params?(hash)
    method << "(message)\n"
  else
    method << "\n"
  end

  # method body
  (count_steps(@name) + 1).times { method << "  " }
  if hash[:input].empty?
    request_return = hash[:return]
    request_return = "nil" if request_return.nil?
    method << "request(:#{name}, #{request_return})\n"
  else
    request_return = hash[:return]
    request_return = "nil" if request_return.nil?
    method << "request(:#{name}, #{request_return}, message)\n"
  end
  
  count_steps(@name).times { method << "  " }
  method << "end\n"
  method
end
generate_methods() click to toggle source
# File lib/softlayer/generator/class_file.rb, line 41
def generate_methods
  return "" if @methods.nil?
  methods = ""
  @methods.each_pair do |method_name, method_content|
    methods << generate_method(method_name, method_content)
    methods << "\n"
  end
  methods
end
generate_representer() click to toggle source
# File lib/softlayer/generator/class_file.rb, line 91
def generate_representer
  representer = ""
  steps = count_steps(@name)
  steps.times { representer << "  " }

  if @parent
    representer << "class Representer < #{Converter.type(@parent)}::Representer\n"
  else
    representer << "class Representer < Representable::Decorator\n"
  end
  steps = steps + 1
  steps.times { representer << "  " } ; representer << "include Representable::Hash\n"
  steps.times { representer << "  " } ; representer << "include Representable::Coercion\n"
  Softlayer::Generator::DataType.attributes_for(@softlayer_name).each_pair do |attribute, type|
    representer << generate_representer_option(attribute, type, steps)
  end
  (steps - 1).times { representer << "  " }
  representer << "end\n"
  representer
end
generate_representer_option(attribute, type, steps) click to toggle source
# File lib/softlayer/generator/class_file.rb, line 112
def generate_representer_option(attribute, type, steps)
  type = get_represented_type(type)

  # Do not create relationships on representer
  if type.match(/\AArray\[/) && type.match(/\AArray\[Softlayer::/)
    ""
  elsif type.match(/\AArray\[/)
    ""
  elsif type.match(/\ASoftlayer::/)
    ""
  else
    rep = ""
    steps.times { rep << "  " }
    rep + "property :#{attribute}, type: #{type}\n"
  end
end
generate_service_constant() click to toggle source
# File lib/softlayer/generator/class_file.rb, line 22
def generate_service_constant
  return "" unless File.exist?("data/#{@softlayer_name.sub(/\ASoftLayer_/, '')}.wsdl")
  steps = count_steps(@name)
  service_constant = ""
  steps.times { service_constant << "  " }
  service_constant + "SERVICE = '#{@softlayer_name}'\n"
end
get_represented_type(type) click to toggle source
# File lib/softlayer/generator/class_file.rb, line 129
def get_represented_type(type)
  type
end
has_params?(hash) click to toggle source
# File lib/softlayer/generator/class_file.rb, line 183
def has_params?(hash)
  !hash[:input].empty?
end

Protected Instance Methods

object_type(object) click to toggle source
# File lib/softlayer/generator/class_file.rb, line 192
def object_type(object)
  Softlayer::Generator::DataType.object_type(Converter.to_softlayer_name(object)).to_s
end