diff --git a/lib/seed-fu/writer.rb b/lib/seed-fu/writer.rb index 44740be..b99b076 100644 --- a/lib/seed-fu/writer.rb +++ b/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 @@ -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 diff --git a/spec/writer_spec.rb b/spec/writer_spec.rb index c552039..1a57753 100644 --- a/spec/writer_spec.rb +++ b/spec/writer_spec.rb @@ -6,7 +6,7 @@ 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 @@ -14,7 +14,6 @@ writer << { :id => 1, :title => "Mr" } writer << { :id => 2, :title => "Dr" } end - load @file_name SeededModel.find(1).title.should == "Mr" @@ -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