class Ar2gostruct::Converter

Attributes

association[RW]
klass[RW]
max_col_size[RW]
max_type_size[RW]
orm[RW]
plural[RW]

Public Class Methods

new(klass, option = {}) click to toggle source
   # File lib/ar2gostruct/converter.rb
 3 def initialize(klass, option = {})
 4   @klass = klass
 5   @max_col_size = 0
 6   @max_type_size = 0
 7   @plural = option[:plural]
 8   @orm = option[:orm]
 9   @association = option[:association]
10 end

Public Instance Methods

convert!() click to toggle source
   # File lib/ar2gostruct/converter.rb
13 def convert!
14   get_schema_info
15 end

Private Instance Methods

get_associations() click to toggle source
    # File lib/ar2gostruct/converter.rb
109 def get_associations
110   builder = Ar2gostruct::Builder::Association.new(self.klass, self.max_col_size, self.max_type_size)
111   builder.get_schema_info
112 end
get_max_col_size() click to toggle source
   # File lib/ar2gostruct/converter.rb
56 def get_max_col_size
57   col_name_max_size = self.klass.column_names.collect{|name| name.size}.max || 0
58   assoc_max_size = if self.association
59     self.klass.reflect_on_all_associations.collect{|assoc| assoc.name.to_s.size}.max || 0
60   else
61     0
62   end
63   type_max_size = Ar2gostruct::CONST::TYPE_MAP.collect{|key, value| key.size}.max || 0
64   [col_name_max_size + 1, assoc_max_size, type_max_size].max
65 end
get_max_type_size() click to toggle source
   # File lib/ar2gostruct/converter.rb
67 def get_max_type_size
68   assoc_max_size = if self.association
69     self.klass.reflect_on_all_associations.collect{|assoc| assoc.name.to_s.size + 2}.max || 0
70   else
71     0
72   end
73   type_max_size = Ar2gostruct::CONST::TYPE_MAP.collect{|key, value| key.size}.max || 0
74   [assoc_max_size, type_max_size].max
75 end
get_orm_builder(orm_name) click to toggle source
   # File lib/ar2gostruct/converter.rb
91 def get_orm_builder(orm_name)
92   prefix = "Ar2gostruct::Builder::ORM::"
93   builder = if Object.const_defined?("#{prefix}#{orm_name.upcase}")
94     "#{prefix}#{orm_name.upcase}"
95   elsif Object.const_defined?("#{prefix}#{orm_name.camelize}")
96     "#{prefix}#{orm_name.camelize}"
97   end
98   return builder.constantize.new(self.klass) if builder
99 end
get_orm_options(col) click to toggle source
   # File lib/ar2gostruct/converter.rb
77 def get_orm_options(col)
78   tags ||= []
79   self.orm.split(",").each do |orm_name|
80     builder = get_orm_builder orm_name
81     if builder
82       option = builder.get_option col
83       tags << option if option
84     end
85   end
86   return tags
87 rescue => e
88   []
89 end
get_schema_info() click to toggle source
   # File lib/ar2gostruct/converter.rb
19 def get_schema_info
20   info = "// Table name: #{self.klass.table_name}\n"
21   struct_name = get_struct_name
22   info << "type #{struct_name} struct {\n"
23 
24   self.max_col_size = get_max_col_size
25   self.max_type_size = get_max_type_size
26 
27   self.klass.columns.each do |col|
28     tags = []
29 
30     # add json tag
31     tags << json_tag(col)
32 
33     if self.orm
34       orm_options = get_orm_options(col)
35       tags << orm_options if orm_options && orm_options.length > 0
36     end
37 
38     col_type = col.type.to_s
39     case col_type
40     when "integer"
41       type = CONST::TYPE_MAP["integer(#{col.limit})"] || "int32"
42       type = "u#{type}" if col.sql_type.match("unsigned").present?
43     else
44       type = CONST::TYPE_MAP[col_type] || "string"
45     end
46 
47     info << sprintf("\t%-#{self.max_col_size}.#{self.max_col_size}s%-#{self.max_type_size}.#{self.max_type_size}s`%s`\n", col.name.camelize, type, tags.join(" "))
48 
49   end
50   info << get_associations if self.association
51 
52   info << "}\n\n"
53   return info
54 end
get_struct_name() click to toggle source
    # File lib/ar2gostruct/converter.rb
101 def get_struct_name
102   if self.plural
103     self.klass.table_name.camelize
104   else
105     self.klass.to_s.tr_s('::', '')
106   end
107 end
json_tag(col) click to toggle source
    # File lib/ar2gostruct/converter.rb
114 def json_tag(col)
115   "json:\"#{col.name}\""
116 end