Skip to content

Instantly share code, notes, and snippets.

@patmaddox
Created March 21, 2009 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patmaddox/82981 to your computer and use it in GitHub Desktop.
Save patmaddox/82981 to your computer and use it in GitHub Desktop.
ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES << "X"
class FooParser
def self.from_tabs(stream)
stream.each do |line|
while line.split("\t").size < FIELDS.size do
line += stream.readline
end
parts = line.split "\t"
next if parts.all? {|p| p.to_s.strip.blank?}
params = {}
FIELDS.each_with_index do |field, i|
params[field] = parts[i].strip
end
yield BarRegistration.new(params)
end
end
FIELDS = %w(
foo_id
foo_db_key
name
...75 more
)
end
describe FooParser do
describe "from_tabs" do
before(:each) do
@file = File.open support_file("data_files/foodatadec08tab_first_10.txt")
@registrations = []
FooParser.from_tabs(@file) {|reg| @registrations << reg}
end
it "should yield BarRegistration objects for each row" do
@registrations.should have(10).items
lambda { @registrations.each(&:save) }.
should change(BarRegistration, :count).by(10)
end
it "should set the fields in the db" do
reg = @registrations.first
reg.foo_id.should include("280039")
reg.cod_expire_date.should == Date.parse("03/31/2009")
end
it "should work even as it gets past a random line break" do
reg = @registrations[9]
reg.name.should == "VALKYRIE"
end
it "should strip surrounding white space" do
@registrations.first.foo_id.should == "280039"
end
it "should correctly handle X as a boolean true" do
@registrations[0].should be_recreation
@registrations[1].should_not be_recreation
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment