Skip to content

Commit

Permalink
Add a :seed_type option for SeedFu::Writer, so you can specify :seed …
Browse files Browse the repository at this point in the history
…or :seed_once depending on what you want in the output
  • Loading branch information
jonleighton committed Nov 4, 2010
1 parent 9ac539a commit 843a3d5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
14 changes: 9 additions & 5 deletions lib/seed-fu/writer.rb
@@ -1,10 +1,14 @@
module SeedFu
class Writer
def initialize(options = {})
@options = options
@options[:chunk_size] ||= 100
@options[:constraints] ||= [:id]
cattr_accessor :default_options
@@default_options = {
:chunk_size => 100,
:constraints => [:id],
:seed_type => :seed
}

def initialize(options = {})
@options = self.class.default_options.merge(options)
raise ArgumentError, "missing option :class_name" unless @options[:class_name]
end

Expand Down Expand Up @@ -76,7 +80,7 @@ def file_footer

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

def seed_footer
Expand Down
15 changes: 12 additions & 3 deletions spec/writer_spec.rb
Expand Up @@ -6,15 +6,14 @@
end

after do
FileUtils.rm(@file_name)
#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"
Expand All @@ -27,10 +26,20 @@
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

it "should support specifying the output to use 'seed_once' rather than 'seed'" do
SeededModel.seed(:id => 1, :title => "Dr")

SeedFu::Writer.write(@file_name, :class_name => 'SeededModel', :seed_type => :seed_once) do |writer|
writer << { :id => 1, :title => "Mr" }
end
load @file_name

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

0 comments on commit 843a3d5

Please sign in to comment.