class SchemaParser

Constants

SCHEMA_FILE

Public Class Methods

new(models) click to toggle source
# File lib/model-visualizer/schema-parser.rb, line 9
def initialize(models)
    @models = models
end

Public Instance Methods

fix_case(string) click to toggle source
# File lib/model-visualizer/schema-parser.rb, line 90
def fix_case(string)
    ActiveSupport::Inflector.singularize(ActiveSupport::Inflector.camelize(string))
end
parse(root) click to toggle source
# File lib/model-visualizer/schema-parser.rb, line 13
def parse(root)
    file = File.join(root, SCHEMA_FILE)
    unless File.file? file
        puts 'db/schema.rb does not exist! If this is surprising, run the command again from the Rails project directory.'
        return
    end

    curr_model = nil

    IO.foreach(file) do |line|
        if line.include? 'create_table'
            name = /"([a-zA-Z_]+)"/.match(line)[1]
            fixed_name = fix_case(name)
            curr_model = @models[fixed_name]

            # Rails generated models are not in hash
            if curr_model == nil
                curr_model = Model.new(fixed_name)
                @models[fixed_name] = curr_model
            end

        elsif line.include? 't.integer'
            match = /t\.integer\s+"([[:alpha:]]+_id)"/.match(line)
            if match
                curr_model.add_foreign_key_attribute(match[1])
            else
                integer = /t\.integer\s+"([a-zA-Z_]+)"/.match(line)[1]
                curr_model.add_integer_attribute(integer)
            end

        elsif line.include? 't.string'
            string = /t\.string\s+"(\w+)"/.match(line)[1]
            curr_model.add_string_attribute(string)

        elsif line.include? 't.primary_key'
            primary_key = /t\.primary_key\s+"(\w+)"/.match(line)[1]
            curr_model.add_primary_key_attribute(primary_key)

        elsif line.include? 't.text'
            text = /t\.text\s+"(\w+)"/.match(line)[1]
            curr_model.add_text_attribute(text)

        elsif line.include? 't.float'
            float = /t\.float\s+"(\w+)"/.match(line)[1]
            curr_model.add_float_attribute(float)

        elsif line.include? 't.decimal'
            decimal = /t\.decimal\s+"(\w+)"/.match(line)[1]
            curr_model.add_decimal_attribute(decimal)

        elsif line.include? 't.datetime'
            datetime = /t\.datetime\s+"(\w+)"/.match(line)[1]
            curr_model.add_datetime_attribute(datetime)

        elsif line.include? 't.timestamp'
            timestamp = /t\.timestamp\s+"(\w+)"/.match(line)[1]
            curr_model.add_timestamp_attribute(timestamp)

        elsif line.include? 't.time'
            time = /t\.time\s+"(\w+)"/.match(line)[1]
            curr_model.add_time_attribute(time)

        elsif line.include? 't.date'
            date = /t\.date\s+"(\w+)"/.match(line)[1]
            curr_model.add_date_attribute(date)

        elsif line.include? 't.binary'
            binary = /t\.binary\s+"(\w+)"/.match(line)[1]
            curr_model.add_binary_attribute(binary)

        elsif line.include? 't.boolean'
            boolean = /t\.boolean\s+"(\w+)"/.match(line)[1]
            curr_model.add_boolean_attribute(boolean)
        end
    end
end