-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
And when it's a parser, call it a parser!
- Loading branch information
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |