Class: JSON2Ruby::RubyWriter
- Inherits:
-
Object
- Object
- JSON2Ruby::RubyWriter
- Defined in:
- lib/json2ruby/ruby_writer.rb
Overview
The RubyWriter class contains methods to output ruby code from a given Entity.
Class Method Summary collapse
-
.attributes_to_ruby(entity, indent = 0, options = {}) ⇒ Object
Return a String containing the Ruby code for each Attribute definition for in the supplied Entity.
-
.to_code(entity, indent = 0, options = {}) ⇒ Object
Return a String containing a Ruby class/module definition for the given Entity.
Class Method Details
.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, = {}) ident = (' '*indent) x = "" entity.attributes.each do |k,v| if (v.is_a?(Collection)) x += "#{ident}#{[:collectionmethod]} :#{k}" else x += "#{ident}#{[:attributemethod]} :#{k}" end if [:includetypes] unless v.is_a?(Primitive) name = ![:namespace].nil? && [:namespace]!="" ? ([:namespace]+"::"+v.name) : v.name x += ", '#{name}'" end end x += " # #{v.comment}\r\n" end x end |
.to_code(entity, indent = 0, options = {}) ⇒ Object
Return a String containing a Ruby class/module definition for the given Entity. Optionally, supply indent to set the indent of the generated code in spaces, and supply a Hash of options as follows:
-
:modules - Boolean if true, generate Ruby
modulefiles instead of classes. -
:require - Array of String items, each of which will generate a ‘require ’<x>‘` statement for each item
-
:superclass_name - String, if supplied, the superclass of the class to geneerate
-
:extend - Array of String items, each of which will generate a ‘extend ’<x>‘` statement for each item in the class
-
:include - Array of String items, each of which will generate a ‘include ’<x>‘` statement for each item in the class
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/json2ruby/ruby_writer.rb', line 16 def self.to_code(entity, indent = 0, = {}) x = "" if .has_key?(:require) [:require].each { |r| x += "require '#{r}'\r\n" } x += "\r\n" end idt = (' '*indent) x += "#{(' '*indent)}#{[:modules] ? "module" : "class"} #{entity.name}" x += " < #{[:superclass_name]}" if .has_key?(:superclass_name) x += "\r\n" if .has_key?(:extend) [:extend].each { |r| x += "#{(' '*(indent+2))}extend #{r}\r\n" } x += "\r\n" end if .has_key?(:include) [:include].each { |r| x += "#{(' '*(indent+2))}include #{r}\r\n" } x += "\r\n" end x += attributes_to_ruby(entity, indent+2, ) x += "#{(' '*indent)}end\r\n" x end |