Skip to content

Commit

Permalink
Rewrite Table more as a parser
Browse files Browse the repository at this point in the history
And when it's a parser, call it a parser!
  • Loading branch information
lkdjiin committed Jun 20, 2014
1 parent d4ab311 commit f6a10c8
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/columns.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "columns/version"
require "columns/table"
require "columns/schema_parser"
require "columns/regex"
require "columns/raw_data"
require "columns/model_data"
Expand Down
67 changes: 67 additions & 0 deletions lib/columns/schema_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module Columns

# Parses a schema.rb file to find table's information.
#
# Examples
#
# parser = SchemaParser.new(File.read('schema.rb'))
# tables = parser.parse
#
# tables['products']
# #=> integer "id"
# #=> string "name"
# #=> ...
#
# tables.keys
# #=> ['commands', 'products', 'users']
class SchemaParser

# Public: Creates a new SchemaParser.
#
# file - The schema.rb as a String.
#
# Examples
#
# string = File.read(File.expand_path('db/schema.rb'))
# parser = SchemaParser.new(string)
def initialize(file)
@lines = file.split("\n")
@hash = {}
@table_found_state = false
@content = ''
end

# Public: Gets table's names and contents.
#
# Returns an Hash who's keys are table's names.
def parse
@lines.each {|line| process_line(line) }
@hash
end

private

def process_line(line)
if (name = Regex.table_name(line))
table_found(name)
elsif @table_found_state
process_content(line)
end
end

def table_found(name)
@current_table = name
@table_found_state = true
end

def process_content(line)
if line =~ /\w*end$/
@hash[@current_table] = @content
@table_found_state = false
@content = ''
else
@content << line + "\n"
end
end
end
end
51 changes: 51 additions & 0 deletions spec/schema_parser_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'spec_helper'

describe SchemaParser do

describe '#parse' do
it 'returns a hash' do
hash = SchemaParser.new(schema_file).parse
expect(hash).to be_a(Hash)
end
end

context 'with a real schema' do
before { @hash = SchemaParser.new(schema_file).parse }

it 'finds the right count of tables' do
expect(@hash.size).to eq 5
end

it "finds the right table's names" do
names = ['assignments', 'products', 'users', 'users_3', 'policies']

names.each {|name| expect(@hash).to have_key(name) }
end

it 'finds the content of a table in the schema' do
expected = " t.integer \"foo\"\n t.string \"bar\"\n"
expect(@hash['assignments']).to eq expected
end

end

context 'with a fake schema' do
before do
schema = "create_table \"foos\"\nt.integer \"foo\"\nend"
@hash = SchemaParser.new(schema).parse
end

it 'finds the right count of tables' do
expect(@hash.size).to eq 1
end

it "finds the right table's names" do
expect(@hash).to have_key('foos')
end

it 'finds the content of a table in a fake schema' do
expect(@hash['foos']).to eq "t.integer \"foo\"\n"
end

end
end

0 comments on commit f6a10c8

Please sign in to comment.