From 505d8b7b465be98e6c92518165cc47e73acc5602 Mon Sep 17 00:00:00 2001 From: Nate Kidwell Date: Thu, 23 Jul 2009 05:44:06 -0400 Subject: [PATCH] moved serializer into separate file --- lib/csv_db.rb | 24 +++++++++ lib/serialization_helper.rb | 83 +++++++++++++++++++++---------- lib/yaml_db.rb | 29 ++--------- spec/serialization_helper_spec.rb | 42 ++++++++++++++++ spec/yaml_dump_spec.rb | 21 -------- tasks/yaml_db_tasks.rake | 6 +-- 6 files changed, 130 insertions(+), 75 deletions(-) create mode 100644 lib/csv_db.rb create mode 100644 spec/serialization_helper_spec.rb diff --git a/lib/csv_db.rb b/lib/csv_db.rb new file mode 100644 index 0000000..604bdf6 --- /dev/null +++ b/lib/csv_db.rb @@ -0,0 +1,24 @@ +module CsvDb + module Helper + def self.loader + Load + end + + def self.dumper + Dump + end + + def self.extension + "csv" + end + end + + module Load + + end + + module Dump + end + + +end \ No newline at end of file diff --git a/lib/serialization_helper.rb b/lib/serialization_helper.rb index 37b1bad..90959e8 100644 --- a/lib/serialization_helper.rb +++ b/lib/serialization_helper.rb @@ -1,39 +1,68 @@ class SerializationHelper - attr_reader :extension + attr_reader :extension - def initialize(helper) - @dumper = helper.dumper - @loader = helper.loader - @extension = helper.extension + def initialize(helper) + @dumper = helper.dumper + @loader = helper.loader + @extension = helper.extension + end + + def dump(filename) + disable_logger + @dumper.dump(File.new(filename, "w")) + reenable_logger + end + + def dump_to_dir(dirname) + Dir.mkdir(dirname) + tables = @dumper.tables + tables.each do |table| + file = File.new "#{dirname}/#{table}.#{@extension}", "w" + @dumper.dump_table file, table end + end + + def load(filename) + disable_logger + @loader.load(File.new(filename, "r")) + reenable_logger + end - def dump(filename) - disable_logger - @dumper.dump(File.new(filename, "w")) - reenable_logger - end + def disable_logger + @@old_logger = ActiveRecord::Base.logger + ActiveRecord::Base.logger = nil + end - def dump_to_dir(dirname) - Dir.mkdir(dirname) - tables = @dumper.tables + def reenable_logger + ActiveRecord::Base.logger = @@old_logger + end + + class Load + + end + + class Dump + def self.dump(io) tables.each do |table| - file = File.new "#{dirname}/#{table}.#{@extension}", "w" - @dumper.dump_table file, table + dump_table(io, table) end end - def load(filename) - disable_logger - @loader.load(File.new(filename, "r")) - reenable_logger - end + def self.tables + ActiveRecord::Base.connection.tables.reject { |table| ['schema_info', 'schema_migrations'].include?(table) } + end + + def self.dump_table(io, table) + return if table_record_count(table).zero? + + dump_table_columns(io, table) + dump_table_records(io, table) + end + + def self.table_column_names(table) + ActiveRecord::Base.connection.columns(table).map { |c| c.name } + end - def disable_logger - @@old_logger = ActiveRecord::Base.logger - ActiveRecord::Base.logger = nil - end + end - def reenable_logger - ActiveRecord::Base.logger = @@old_logger - end end \ No newline at end of file diff --git a/lib/yaml_db.rb b/lib/yaml_db.rb index 3d0f950..9effe58 100644 --- a/lib/yaml_db.rb +++ b/lib/yaml_db.rb @@ -1,9 +1,10 @@ require 'rubygems' require 'yaml' require 'active_record' +require 'serialization_helper' module YamlDb - module SerializationHelper + module Helper def self.loader YamlDb::Load end @@ -62,24 +63,8 @@ def self.quote_table(table) end end - module Dump - def self.dump(io) - tables.each do |table| - dump_table(io, table) - end - end - - def self.tables - ActiveRecord::Base.connection.tables.reject { |table| ['schema_info', 'schema_migrations'].include?(table) } - end - - def self.dump_table(io, table) - return if table_record_count(table).zero? - - dump_table_columns(io, table) - dump_table_records(io, table) - end - + class Dump < SerializationHelper::Dump + def self.dump_table_columns(io, table) io.write("\n") io.write({ table => { 'columns' => table_column_names(table) } }.to_yaml) @@ -100,10 +85,6 @@ def self.table_record_header(io) io.write(" records: \n") end - def self.table_column_names(table) - ActiveRecord::Base.connection.columns(table).map { |c| c.name } - end - def self.each_table_page(table, records_per_page=1000) total_count = table_record_count(table) pages = (total_count.to_f / records_per_page).ceil - 1 @@ -126,7 +107,7 @@ def self.table_record_count(table) end end - module Load + class Load < SerializationHelper::Load def self.load(io) ActiveRecord::Base.connection.transaction do YAML.load_documents(io) do |ydoc| diff --git a/spec/serialization_helper_spec.rb b/spec/serialization_helper_spec.rb new file mode 100644 index 0000000..2b0c214 --- /dev/null +++ b/spec/serialization_helper_spec.rb @@ -0,0 +1,42 @@ +require File.dirname(__FILE__) + '/base' + +describe SerializationHelper do + + before do + ActiveRecord::Base = mock('ActiveRecord::Base', :null_object => true) + ActiveRecord::Base.connection = mock('connection') + ActiveRecord::Base.connection.stub!(:tables).and_return([ 'mytable', 'schema_info', 'schema_migrations' ]) + end + + def stub_helper! + @helper = mock("MyHelper") + @helper.stub!(:dumper).and_return(@dumper) + @helper.stub!(:loader) + @helper.stub!(:extension).and_return("yml") + @dumper.stub!(:tables).and_return([ActiveRecord::Base.connection.tables[0]]) + end + + context "for multi-file dumps" do + before do + @io = StringIO.new + File.should_receive(:new).once.with("dir_name/mytable.yml", "w").and_return(@io) + Dir.should_receive(:mkdir).once.with("dir_name") + stub_helper! + @dumper.should_receive(:dump_table).once.with(@io, "mytable") + end + + it "should create the number of files that there are tables" do + SerializationHelper.new(@helper).dump_to_dir "dir_name" + end + + end + + context "for multi-file loads" do + before do + + end + + + end + +end diff --git a/spec/yaml_dump_spec.rb b/spec/yaml_dump_spec.rb index ef3941f..289172f 100644 --- a/spec/yaml_dump_spec.rb +++ b/spec/yaml_dump_spec.rb @@ -12,9 +12,6 @@ YamlDb::Utils.stub!(:quote_table).with('mytable').and_return('mytable') end - context "for single file yaml dumps" do - - before(:each) do File.stub!(:new).with('dump.yml', 'w').and_return(StringIO.new) @io = StringIO.new @@ -95,24 +92,6 @@ YamlDb::Dump.dump_table(@io, 'mytable') end - end - - context "for multi-file yaml dumps" do - before do - @io = StringIO.new - File.should_receive(:new).once.with("dir_name/mytable.yml", "w").and_return(@io) - Dir.should_receive(:mkdir).once.with("dir_name") - YamlDb::Dump.should_receive(:dump_table).once.with(@io, "mytable") - end - - it "should create the number of files that there are tables" do - SerializationHelper.new(YamlDb::SerializationHelper).dump_to_dir "dir_name" - end - - end - - - end diff --git a/tasks/yaml_db_tasks.rake b/tasks/yaml_db_tasks.rake index 06115b2..3b581b4 100644 --- a/tasks/yaml_db_tasks.rake +++ b/tasks/yaml_db_tasks.rake @@ -16,18 +16,18 @@ namespace :db do desc "Dump contents of database to db/data.yml" task(:dump => :environment) do - SerializationHelper.new(YamlDb::SerializationHelper).dump db_dump_data_file + SerializationHelper.new(YamlDb::Helper).dump db_dump_data_file end desc "Dump contents of database to curr_dir_name/data.ym" task(:dump_dir => :environment) do time_dir = dump_dir "/#{Time.now.to_s.gsub(/ /, '_')}" - SerializationHelper.new(YamlDb::SerializationHelper).dump_to_dir time_dir + SerializationHelper.new(YamlDb::Helper).dump_to_dir time_dir end desc "Load contents of db/data.yml into database" task(:load => :environment) do - SerializationHelper.new(YamlDb::SerializationHelper).load db_dump_data_file + SerializationHelper.new(YamlDb::Helper).load db_dump_data_file end end end