Skip to content

Commit

Permalink
added csv loading functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ludicast committed Jul 29, 2009
1 parent 0d213ac commit b417c00
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
24 changes: 23 additions & 1 deletion lib/csv_db.rb
@@ -1,3 +1,4 @@
#require 'FasterCSV'
module CsvDb
module Helper
def self.loader
Expand All @@ -14,13 +15,34 @@ def self.extension
end

class Load < SerializationHelper::Load
def self.load_documents(io, truncate = true)
puts io.to_s
tables = {}
curr_table = nil
io.each do |line|
if /BEGIN_CSV_TABLE_DECLARATION(.+)END_CSV_TABLE_DECLARATION/ =~ line
curr_table = $1
tables[curr_table] = {}
else
if tables[curr_table]["columns"]
tables[curr_table]["records"] << FasterCSV.parse(line)[0]
else
tables[curr_table]["columns"] = FasterCSV.parse(line)[0]
tables[curr_table]["records"] = []
end
end
end

tables.each_pair do |table_name, contents|
load_table(table_name, contents, truncate)
end
end
end

class Dump < SerializationHelper::Dump

def self.before_table(io,table)
io.write "#{table}:"
io.write "BEGIN_CSV_TABLE_DECLARATION#{table}END_CSV_TABLE_DECLARATION\n"
end

def self.dump(io)
Expand Down
2 changes: 2 additions & 0 deletions lib/yaml_db.rb
Expand Up @@ -57,6 +57,8 @@ class Load < SerializationHelper::Load
def self.load_documents(io, truncate = true)
YAML.load_documents(io) do |ydoc|
ydoc.keys.each do |table_name|
puts table_name
puts ydoc[table_name].inspect
next if ydoc[table_name].nil?
load_table(table_name, ydoc[table_name], truncate)
end
Expand Down
3 changes: 2 additions & 1 deletion tasks/yaml_db_tasks.rake
Expand Up @@ -31,7 +31,8 @@ namespace :db do
desc "Load contents of db/data.extension (defaults to yaml) into database"
task :load => :environment do
format_class = ENV['class'] || "YamlDb::Helper"
SerializationHelper::Base.new(format_class.constantize).load db_dump_data_file
helper = format_class.constantize
SerializationHelper::Base.new(helper).load (db_dump_data_file helper.extension)
end

desc "Load contents of db/data_dir into database"
Expand Down

0 comments on commit b417c00

Please sign in to comment.