-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simpler SeedFu::Writer with revised API and some tests to show it works
- Loading branch information
1 parent
938a077
commit 10d1790
Showing
10 changed files
with
172 additions
and
165 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,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 |
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 |
---|---|---|
@@ -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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,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 |
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
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,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 |