Class: SchemaDoc::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/schemadoc/worker.rb

Defined Under Namespace

Classes: AbstractModel

Instance Method Summary collapse

Constructor Details

#initialize(config, models = []) ⇒ Worker

Returns a new instance of Worker.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/schemadoc/worker.rb', line 14

def initialize( config, models=[] )
  ## split into db config (for connection) and
  ## schemadoc config

  @config    = {}
  @db_config = {}

  config.each do |k,v|
    if k == 'database'
      @db_config = v   # note: discard key; use hash as connection spec
    else
      @config[ k ] = v
    end
  end

  puts "database connection spec:"
  pp @db_config
  
  @models = models
end

Instance Method Details

#build_indexObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/schemadoc/worker.rb', line 169

def build_index
  ####
  # build symbol index hash

  symbols = {}

  letters = %w(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)

  ## add a to z [26 entries]

  letters.each do |letter|
    symbols[letter] = {
       name:   letter,
       tables: [],
       columns: []
    }
  end

  @con.tables.sort.each do |name|

    table_key  = name[0].upcase
    symbols[table_key][:tables] << name

    @con.columns( name ).each do |col|
      col_key = col.name[0].upcase
      cols_ary = symbols[col_key][:columns]

      ## search for column name
      col_hash = cols_ary.find { |item| item[:name] == col.name }
      if col_hash.nil?
        col_hash = { name: col.name, tables: [] }
        cols_ary << col_hash
      end

      col_hash[:tables] << name  
    end
  end

  ## sort tables, cols and (in tables)
  symbols.each do |k,h|
    h[:tables] = h[:tables].sort

    h[:columns] = h[:columns].sort { |l,r| l[:name] <=> r[:name] }
    h[:columns].each { |col| col[:tables] = col[:tables].sort }
  end

  data = []
  symbols.each do |k,v|
    data << v     # turn data json into an array of letters (ever letter is a hash w/ name,tables,columns,etc.)
  end

  data
end

#build_modelsObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/schemadoc/worker.rb', line 123

def build_models
  #########################
  # build models hash
  
  models = []

  @models.each do |model|

    puts model.name
    puts "=" * model.name.size

    m = {
      name: model.name,     ## todo: split into name and module/package name ?? 
      table_name: model.table_name,
      columns: [],
      assocs: [],
    }

    model.columns.each do |column|
      c = {
        name:     column.name,
        null:     column.null,
        default:  column.default,
        sql_type: column.sql_type,
        cast_type: column.cast_type.class.name,
      }
      m[:columns] << c
    end

    ## check assocs
    assocs = model.reflections.values
    assocs.each do |assoc|
      a = {
        macro:   assoc.macro,     ## rename to rel or something - why, why not??
        name:    assoc.name,
        options: assoc.options    ## filter options - why, why not??
      }
      m[:assocs] << a
    end

    models << m
  end # each model
  models
end

#build_schemaObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/schemadoc/worker.rb', line 61

def build_schema
  ####
  # build schema hash

  schemas = {}

  @con.tables.sort.each do |name|

    t = { name: name,
          columns: []
        }
    @con.columns( name ).each do |col|
      t[:columns] << {
         name:    col.name,
         type:    col.sql_type.downcase,   # note: use integer (instead of INTEGER)
         default: col.default,
         null:    col.null
      }
    end

    schema_name = find_schema_for_table(name)
    puts "add '#{name}' to schema '#{schema_name}'"

    schema = schemas[schema_name]
    if schema.nil?
      # note: use schema_name from config (do NOT use key - might be different)
      schema = { name: schema_name, tables: [] }
      schemas[schema_name] = schema
    end
    schema[:tables] << t
  end

  data = { schemas: [] }
  schemas.each do |k,v|
    data[:schemas] << v   ## turn schemas into an array w/ name, tables, etc.
  end
  
  data
end

#connectObject



36
37
38
39
40
41
# File 'lib/schemadoc/worker.rb', line 36

def connect
  ##@con = AbstractModel.connection_for( @db_config )

  ActiveRecord::Base.establish_connection( @db_config )
  @con = ActiveRecord::Base.connection
end

#dump_modelsObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/schemadoc/worker.rb', line 102

def dump_models
  @models.each do |model|
    ## pp model

    puts model.name
    puts "=" * model.name.size
    puts "  table_name: #{model.table_name}"
    pp model.columns

    ## check assocs
    assocs = model.reflections.values
    ## pp assocs
    assocs.each do |assoc|
      puts "#{assoc.macro} #{assoc.name}"
      puts "options:"
      pp assoc.options
    end
  end
end

#dump_schemaObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/schemadoc/worker.rb', line 43

def dump_schema
  @con.tables.sort.map do |name|
     puts "#{name} : #{name.class.name}"
  end
  puts ''

  @con.tables.sort.each do |name|
    puts "#{name}"
    puts "-" * name.size

    @con.columns( name ).each do |col|
      puts "  #{col.name} #{col.sql_type}, #{col.default}, #{col.null} : #{col.class.name}"
    end
    puts ''
  end
end

#models_to_yuml(models) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/schemadoc/worker.rb', line 224

def models_to_yuml( models )

  ## todo: move to script !!!
  ##   check
  ##   how to deal w/ singular,plural,
  ##   add to model
  ##    singular,plural  - to keep it simple???
  ##   use name and full_name and module ??
  ##  e.g. BeerDb::Model::Brewery ???

  buf = ''
  models.each do |model|

    assocs = model[:assocs]
    if assocs.empty?
      buf << "// skipping #{model[:name]}; no assocs found/n"
      next
    end

    assocs.each do |assoc|
      ## skip assocs w/ option through for now
      if assoc[:options][:through]
        buf << "// skipping assoc #{assoc[:macro]} #{assoc[:name]} w/ option through\n"
        next
      end

      buf << "[#{model[:name]}] - [#{assoc[:name]}]\n"
    end
  end

  buf
end

#run(opts = {}) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/schemadoc/worker.rb', line 258

def run( opts={} )
  connect()
  dump_schema()
  dump_models()

  models = build_models()
  pp models

  schema = build_schema()
  index  = build_index()

  ## pp schema

  File.open( 'database.json', 'w') do |f|
    f.write JSON.pretty_generate( schema )
  end

  File.open( 'models.json', 'w') do |f|
    f.write JSON.pretty_generate( models )
  end
  
  ### fix: move to script !!!
  File.open( 'models.yuml', 'w' ) do |f|
    f.write models_to_yuml( models )
  end

  File.open( 'symbols.json', 'w') do |f|
    f.write JSON.pretty_generate( index )
  end
end