Class: SchemaDoc::Worker
- Inherits:
-
Object
- Object
- SchemaDoc::Worker
- Defined in:
- lib/schemadoc/worker.rb
Defined Under Namespace
Classes: AbstractModel
Instance Method Summary collapse
- #build_index ⇒ Object
- #build_schema ⇒ Object
- #connect ⇒ Object
- #dump_schema ⇒ Object
-
#initialize(config) ⇒ Worker
constructor
A new instance of Worker.
- #run(opts = {}) ⇒ Object
Constructor Details
#initialize(config) ⇒ 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 |
# File 'lib/schemadoc/worker.rb', line 14 def initialize( config ) ## 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 end |
Instance Method Details
#build_index ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 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 |
# File 'lib/schemadoc/worker.rb', line 96 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_schema ⇒ Object
55 56 57 58 59 60 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 |
# File 'lib/schemadoc/worker.rb', line 55 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 |
#connect ⇒ Object
34 35 36 |
# File 'lib/schemadoc/worker.rb', line 34 def connect @con = AbstractModel.connection_for( @db_config ) end |
#dump_schema ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/schemadoc/worker.rb', line 38 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 |
#run(opts = {}) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/schemadoc/worker.rb', line 151 def run( opts={} ) connect() dump_schema() schema = build_schema() index = build_index() ## pp schema File.open( 'database.json', 'w') do |f| f.write JSON.pretty_generate( schema ) end File.open( 'symbols.json', 'w') do |f| f.write JSON.pretty_generate( index ) end end |