# use the regex-based implementation require 'json' class Pretender def self.parse(filename) require filename ActiveRecord::Schema.schema end end module ActiveRecord class Schema def create_table(table_name, options={}, &block) table = Table.new(table_name) yield table if block_given? self.class.add_table(table.name, table.columns) end def method_missing(*args) # ignore add_index, etc. end def self.define(*args, &block) new.instance_eval(&block) end def self.add_table(table_name, columns) @schema ||= {} @schema[table_name] = columns end def self.schema @schema || {} end end class Table attr_reader :name, :columns def initialize(name) @name = name @columns = [] end def method_missing(data_type, *column_names_and_options) column_names_and_options.pop if column_names_and_options.last.is_a?(Hash) @columns += column_names_and_options end end end def init_schema path = "#{@root}/db/schema.rb" object = Pretender.parse("#{path}") generate_folders generate_routes(object) generate_controllers(object) generate_actions(object) generate_models(object) generate_definitor(object) generate_projects(object) end def generate_folders system("mkdir -p #{@root}/config/routes/test") system("mkdir -p #{@root}/app/models/test") system("mkdir -p #{@root}/app/controllers/api/test") system("mkdir -p #{@root}/app/controllers/action/test") system("mkdir -p #{@root}/app/controllers/concerns/project_module/test") system("mkdir -p #{@root}/app/controllers/concerns/definitor_module/test") system("mkdir -p #{@root}/app/controllers/concerns/serializer_module/test") puts "DONE MAKING FOLDERS...." end def generate_routes(object) routes = object.keys path = File.dirname(__FILE__)+"/recips/route.rb" routes.each do |route| system("cp #{path} #{@root}/config/routes/test/#{route}.rb") end puts "DONE MAKING ROUTES...." end def generate_controllers(object) routes = object.keys path = File.dirname(__FILE__)+"/recips/controller.rb" tmp_path = File.dirname(__FILE__)+"/recips/tmp/controller.rb" routes.each do |route| text = File.read(path) new_contents = text.gsub("VARIABLE", "#{route.classify.pluralize}") File.open(tmp_path, "w") {|file| file.puts new_contents } system("cp #{tmp_path} #{@root}/app/controllers/api/test/#{route}_controller.rb") end puts "DONE MAKING API CONTROLLERS...." end def generate_actions(object) routes = object.keys path = File.dirname(__FILE__)+"/recips/action.rb" tmp_path = File.dirname(__FILE__)+"/recips/tmp/action.rb" routes.each do |route| text = File.read(path) new_contents = text.gsub("VARIABLE", "#{route.classify.pluralize}") new_contents = new_contents.gsub("METODO", "#{route}") File.open(tmp_path, "w") {|file| file.puts new_contents } system("cp #{tmp_path} #{@root}/app/controllers/action/test/#{route}_controller.rb") end puts "DONE MAKING ACTION CONTROLLERS...." routes_generate_action(routes) end def routes_generate_action(routes) line = "" routes.each do |route| line += "post '/#{route}' => '#{route}##{route}'\n" end path = "#{@root}/config/routes_action_parse.txt" File.write(path,line) puts "DONE MAKING ACTION ROUTES...." end def generate_definitor(object) routes = object.keys path = File.dirname(__FILE__)+"/recips/definitor.rb" tmp_path = File.dirname(__FILE__)+"/recips/tmp/definitor.rb" routes.each do |route| text = File.read(path) new_contents = text.gsub("VARIABLE", "#{route.classify.pluralize}") File.open(tmp_path, "w") {|file| file.puts new_contents } system("cp #{tmp_path} #{@root}/app/controllers/concerns/definitor_module/test/definitor_#{route}.rb") end puts "DONE MAKING DEFINITORS...." end def generate_projects(object) routes = object.keys path = File.dirname(__FILE__)+"/recips/project.rb" tmp_path = File.dirname(__FILE__)+"/recips/tmp/project.rb" routes.each do |route| text = File.read(path) new_contents = text.gsub("VARIABLE", "#{route.classify.pluralize}") File.open(tmp_path, "w") {|file| file.puts new_contents } system("cp #{tmp_path} #{@root}/app/controllers/concerns/project_module/test/project_#{route}.rb") end puts "DONE MAKING PROJECTS...." end def generate_models(object) routes = object.keys path = File.dirname(__FILE__)+"/recips/model.rb" @array_has_many = [] routes.each do |route| attributes = object[route] route = route.singularize tmp_path = File.dirname(__FILE__)+"/recips/tmp/model/#{route}.rb" text = File.read(path) new_contents = text.gsub("VARIABLE", "#{route.capitalize.classify}") system("touch #{tmp_path}") File.open(tmp_path, "w") {|file| file.puts new_contents } size = attributes.size.to_i - 1 relations = size - attributes.index("updated_at").to_i relations = attributes.pop(relations) relations.each_with_index do |relation,index| write_belongs_to(tmp_path,route,relation.to_s,index) end end write_has_many cp_models(routes) routes_schema_parse(object) puts "DONE MAKING MODELS...." end def write_at(fname, at_line, sdat) open(fname, 'r+') do |f| while (at_line-=1) > 0 # read up to the line you want to write after f.readline end pos = f.pos # save your position in the file rest = f.read # save the rest of the file f.seek pos # go back to the old position f.write sdat # write new data f.write rest # write rest of file end end def write_belongs_to(tmp_path,origin,relation,index) relation = relation.gsub("_id","").gsub("[","").gsub("]","").gsub('"',"") write_at(tmp_path,index.to_i+3," belongs_to :#{relation}\n") @array_has_many.push({:relation => relation, :origin => origin}) end def write_has_many @array_has_many.each do |has| relation = has[:relation] origin = has[:origin] tmp_path = File.dirname(__FILE__)+"/recips/tmp/model/#{relation}.rb" write_at(tmp_path,2," has_many :#{origin}\n") end end def cp_models(files) files.each do |file| file = file.singularize tmp_path = File.dirname(__FILE__)+"/recips/tmp/model/#{file}.rb" system("cp #{tmp_path} #{@root}/app/models/test/#{file}.rb") system("rm #{tmp_path}") end end def routes_schema_parse(object) object = complete_object(object,@array_has_many) structure = create_draw_structure(object) #puts "STRUCTURED" #puts structure line = "GENERAL ROUTES SCHEMA\n" line += draw_complex_structure(structure) #puts line path = "#{@root}/config/routes_schema_parse.txt" File.write(path,line) puts "DONE MAKING DRAW ROUTES...." end def complete_object(object,array) new_array = [] object.keys.each do |obj| array.push({:relation => obj.singularize, :origin => ''}) end # array.push({:relation => 'polling', :origin => 'administrador'}) # array.push({:relation => 'usuario', :origin => 'recinto'}) # array.push({:relation => 'recinto', :origin => 'casa'}) # array.push({:relation => 'recinto', :origin => ''}) array.map { |e| e[:origin] = {:relation => e[:origin], :has_many => []} } keys = object.keys #keys = object.keys.push('recintos') keys.each do |key| key = key.singularize merge = array.select {|e| e[:relation] == key} ar = {:relation => key, :has_many => []} merge.each do |me| #next if me[:origin][:relation].blank? ar[:has_many].push(me[:origin]) end new_array.push(ar) end #puts new_array return new_array end def create_draw_structure(array) #puts "create_draw_structure" array_init = array new_array = array_init.each do |rel| deep_draw_structure(array_init,rel) end return new_array end def deep_draw_structure(array,rel) return if array.empty? array.each do |arr| next if arr[:relation] == rel[:relation] arr[:has_many].each do |e| if e[:relation] == "" arr[:has_many].delete(e) end if e[:relation] == [rel[:relation]] || e[:relation] == rel[:relation] e[:has_many] = rel[:has_many] end end array = arr[:has_many].select {|e| e[:relation] != ""} deep_draw_structure(array,rel) end end def draw_method_action(action,space) #puts 'draw_method_action' line = space+"draw :#{action.pluralize} do\n" return line end def draw_method_end(space) line = space+"end\n" return line end def draw_simple_structure(space = "",object) line = "" object.keys.each_with_index do |key| line += draw_method_action(key,space) space = space+" " end object.keys.each_with_index do |key| space = space[0...-2] line += draw_method_end(space) end return line end def draw_complex_structure(line = "",space = "",object) object.each_with_index do |key| line = draw_deep_complex_structure(line,[key],space) end return line end def draw_deep_complex_structure(line,array,space_int) return if array.empty? #puts 'draw_deep_complex_structure' array.each do |key| line += draw_method_action(key[:relation],space_int) space_int = space_int+" " line = draw_draw_deep_complex_structure(line,key[:has_many],space_int) space_end = space_int[0...-2] line += draw_method_end(space_end) unless key[:has_many].empty? end return line end def draw_draw_deep_complex_structure(relation = '',line,array,space_int) if array.empty? space_int = space_int[0...-2] line += draw_method_end(space_int) return line end #puts 'draw_draw_deep_complex_structure' array.each do |has_many| line += draw_method_action(has_many[:relation],space_int) space = space_int+" " line = draw_draw_draw_deep_complex_structure(line,has_many[:has_many],space) end return line end def draw_draw_draw_deep_complex_structure(relation = '',line,array,space_int) if array.empty? space_int = space_int[0...-2] line += draw_method_end(space_int) return line end #puts 'draw_draw_draw_deep_complex_structure' array.each do |has_many| line += draw_method_action(has_many[:relation],space_int) space = space_int+" " line = draw_draw_draw_deep_complex_structure(line,has_many[:has_many],space) space_end = space_int[0...-2] line += draw_method_end(space_end) end return line end