require_relative 'builder/db_model' require_relative 'builder/url_handler' require_relative 'parser' module Shift class ShiftBuilder attr_accessor :models, :handlers, :clean def initialize() @models = [] @handlers = [] @clean = true end def get_data_from_file(file_name) parser = Shift::ShiftParser.new model = nil url_handler = nil last_statement = "" this_statement = "" url = "" line_number = 0 File.open(file_name) do |file| while line = file.gets parts = line.split("\t") num_tabs = parts.length - 1 line = line.strip line_number += 1 if line != "" begin statement = parser.parse(line) this_statement = statement.keys[0] case this_statement when :model_definition_statement if num_tabs != 0 @clean = false puts "Error at line #{line_number} character 1 : #{line}" end model = Builder::DBModel.new(statement[:model_definition_statement][:model_name]) when :model_attribute_definition_statement if num_tabs != 1 @clean = false puts "Error at line #{line_number} character 1 : #{line}" end model_attribute = statement[:model_attribute_definition_statement] model.add_attribute(model_attribute[:attribute_name], model_attribute[:attribute_type]) when :url if (last_statement == :model_attribute_definition_statement) add_model model end if num_tabs != 0 @clean = false puts "Error at line #{line_number} character 1 : #{line}" end url = statement[:url] when :url_method if url_handler add_handler url_handler end if num_tabs != 1 @clean = false puts "Error at line #{line_number} character 1 : #{line}" end url_handler = Builder::UrlHandler.new(url, statement[:url_method].to_s) else if num_tabs < 2 @clean = false puts "Error at line #{line_number} character 1 : #{line}" end stmt = { :statement => statement, :num_tabs => num_tabs } if !url_handler url_handler = Builder::UrlHandler.new("", "") end url_handler.add_statement stmt end last_statement = this_statement rescue Parslet::ParseFailed => parse_error error = parse_error.to_s error = error.split(" ") puts "Error at line #{line_number} character #{error[error.length - 1]} : #{line}" @clean = false end end end end if (last_statement == :model_attribute_definition_statement) add_model model else add_handler url_handler end self end def add_model(model) @models.push model end def add_handler(handler) @handlers.push handler end end end