Method: JSON2Ruby::RubyWriter.attributes_to_ruby

Defined in:
lib/json2ruby/ruby_writer.rb

.attributes_to_ruby(entity, indent = 0, options = {}) ⇒ Object

Return a String containing the Ruby code for each Attribute definition for in the supplied Entity. Optionally, supply indent to set the indent of the generated code in spaces, and supply a Hash of options as follows:

  • :attributemethod - String, the method to call to define attributes

  • :collectionmethod - String, the method to call to define collections

  • :includetypes - Boolean if true, include the string of the Attribute type as a second parameter to the definition call.

  • :namespace - String, the namespace of the type classes in the format ‘Module::SubModule’…



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/json2ruby/ruby_writer.rb', line 46

def self.attributes_to_ruby(entity, indent = 0, options = {})
  ident = (' '*indent)
  x = ""
  entity.attributes.each do |k,v|
    if (v.is_a?(Collection))
      x += "#{ident}#{options[:collectionmethod]} :#{k}"
    else
      x += "#{ident}#{options[:attributemethod]} :#{k}"
    end
    if options[:includetypes]
      unless v.is_a?(Primitive)
        name = !options[:namespace].nil? && options[:namespace]!="" ? (options[:namespace]+"::"+v.name) : v.name
        x += ", '#{name}'"
      end
    end
    x += " # #{v.comment}\r\n"
  end
  x
end