Skip to content

Commit

Permalink
Simpler SeedFu::Writer with revised API and some tests to show it works
Browse files Browse the repository at this point in the history
  • Loading branch information
jonleighton committed Nov 4, 2010
1 parent 938a077 commit 10d1790
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 165 deletions.
3 changes: 2 additions & 1 deletion lib/seed-fu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
require 'seed-fu/railtie' if defined?(Rails) && Rails.version >= "3"

module SeedFu
autoload :VERSION, 'seed-fu/version'
autoload :Seeder, 'seed-fu/seeder'
autoload :ActiveRecordExtension, 'seed-fu/active_record_extension'
autoload :BlockHash, 'seed-fu/block_hash'
autoload :VERSION, 'seed-fu/version'
autoload :Runner, 'seed-fu/runner'
autoload :Writer, 'seed-fu/writer'

# Turn off the output when seeding data
mattr_accessor :quiet
Expand Down
14 changes: 14 additions & 0 deletions lib/seed-fu/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module SeedFu
class Railtie < Rails::Railtie
rake_tasks do
load "tasks/seed_fu.rake"
end

initializer 'seed_fu.set_fixture_paths' do
SeedFu.fixture_paths = [
Rails.root.join('db/fixtures').to_s,
Rails.root.join('db/fixtures/' + Rails.env).to_s
]
end
end
end
93 changes: 90 additions & 3 deletions lib/seed-fu/writer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,90 @@
require File.join( File.dirname(__FILE__), 'writer', 'abstract' )
require File.join( File.dirname(__FILE__), 'writer', 'seed' )
require File.join( File.dirname(__FILE__), 'writer', 'seed_many' )
module SeedFu
class Writer
def initialize(options = {})
@options = options
@options[:chunk_size] ||= 100
@options[:constraints] ||= [:id]

raise ArgumentError, "missing option :class_name" unless @options[:class_name]
end

def self.write(io_or_filename, options = {}, &block)
new(options).write(io_or_filename, &block)
end

def write(io_or_filename, &block)
if io_or_filename.respond_to?(:write)
write_to_io(io_or_filename, &block)
else
File.open(io_or_filename, 'w') do |file|
write_to_io(file, &block)
end
end
end

def <<(seed)
raise "You must add seeds inside a SeedFu::Writer#write block" unless @io

buffer = ''

if chunk_this_seed?
buffer << seed_footer
buffer << "# BREAK EVAL\n"
buffer << seed_header
end

buffer << ",\n"
buffer << ' ' + seed.inspect

@io.write(buffer)

@count += 1
end
alias_method :add, :<<

private

def write_to_io(io)
@io, @count = io, 0
@io.write(file_header)
@io.write(seed_header)
yield(self)
@io.write(seed_footer)
@io.write(file_footer)
ensure
@io, @count = nil, nil
end

def file_header
<<-END
# DO NOT MODIFY THIS FILE, it was auto-generated.
#
# Date: #{Time.now}
# Seeding #{@options[:class_name]}
# Written with the command:
#
# #{$0} #{$*.join}
#
END
end

def file_footer
<<-END
# End auto-generated file.
END
end

def seed_header
constraints = @options[:constraints] && @options[:constraints].map(&:inspect).join(', ')
"#{@options[:class_name]}.seed(#{constraints}"
end

def seed_footer
"\n)\n"
end

def chunk_this_seed?
@count != 0 && (@count % @options[:chunk_size]) == 0
end
end
end
58 changes: 0 additions & 58 deletions lib/seed-fu/writer/abstract.rb

This file was deleted.

26 changes: 0 additions & 26 deletions lib/seed-fu/writer/seed.rb

This file was deleted.

52 changes: 0 additions & 52 deletions lib/seed-fu/writer/seed_many.rb

This file was deleted.

24 changes: 24 additions & 0 deletions spec/runner_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'spec_helper'

describe SeedFu::Runner do
it "should seed data from Ruby and gzipped Ruby files in the given fixtures directory" do
SeedFu.seed(File.dirname(__FILE__) + '/fixtures')

SeededModel.find(1).title.should == "Foo"
SeededModel.find(2).title.should == "Bar"
SeededModel.find(3).title.should == "Baz"
end

it "should seed only the data which matches the filter, if one is given" do
SeedFu.seed(File.dirname(__FILE__) + '/fixtures', /_2/)

SeededModel.count.should == 1
SeededModel.find(2).title.should == "Bar"
end

it "should use the SpecFu.fixtures_path variable to determine where fixtures are" do
SeedFu.fixture_paths = [File.dirname(__FILE__) + '/fixtures']
SeedFu.seed
SeededModel.count.should == 3
end
end
25 changes: 0 additions & 25 deletions spec/seed_fu_spec.rb → spec/seeder_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
require 'spec_helper'

describe SeedFu::Seeder do
before do
SeededModel.delete_all
end

it "should create a model if one doesn't exist" do
SeededModel.seed(:id) do |s|
s.id = 5
Expand Down Expand Up @@ -132,25 +128,4 @@
it "should raise an error if validation fails" do
lambda { SeededModel.seed(:id => 1) }.should raise_error(ActiveRecord::RecordInvalid)
end

it "should seed data from Ruby and gzipped Ruby files in the given fixtures directory" do
SeedFu.seed(File.dirname(__FILE__) + '/fixtures')

SeededModel.find(1).title.should == "Foo"
SeededModel.find(2).title.should == "Bar"
SeededModel.find(3).title.should == "Baz"
end

it "should seed only the data which matches the filter, if one is given" do
SeedFu.seed(File.dirname(__FILE__) + '/fixtures', /_2/)

SeededModel.count.should == 1
SeededModel.find(2).title.should == "Bar"
end

it "should use the SpecFu.fixtures_path variable to determine where fixtures are" do
SeedFu.fixture_paths = [File.dirname(__FILE__) + '/fixtures']
SeedFu.seed
SeededModel.count.should == 3
end
end
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
require 'seed-fu'
require 'logger'

RSpec.configure do |config|
config.before do
SeededModel.delete_all
end
end

SeedFu.quiet = true

ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/../debug.log")
Expand Down
36 changes: 36 additions & 0 deletions spec/writer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'spec_helper'

describe SeedFu::Writer do
before do
@file_name = File.dirname(__FILE__) + '/seeded_models.rb'
end

after do
FileUtils.rm(@file_name)
end

it "should successfully write some seeds out to a file and then import them back in" do
SeedFu::Writer.write(@file_name, :class_name => 'SeededModel') do |writer|
writer << { :id => 1, :title => "Mr" }
writer << { :id => 2, :title => "Dr" }
end

load @file_name

SeededModel.find(1).title.should == "Mr"
SeededModel.find(2).title.should == "Dr"
end

it "should support chunking" do
SeedFu::Writer.write(@file_name, :class_name => 'SeededModel', :chunk_size => 2) do |writer|
writer << { :id => 1, :title => "Mr" }
writer << { :id => 2, :title => "Dr" }
writer << { :id => 3, :title => "Dr" }
end

load @file_name

SeededModel.count.should == 3
File.read(@file_name).should include("# BREAK EVAL\n")
end
end

0 comments on commit 10d1790

Please sign in to comment.