seed-fu 1.2.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile DELETED
@@ -1,34 +0,0 @@
1
- require 'rake'
2
- require 'rake/testtask'
3
- require 'rake/rdoctask'
4
-
5
- begin
6
- require 'jeweler'
7
- Jeweler::Tasks.new do |gemspec|
8
- gemspec.name = "seed-fu"
9
- gemspec.summary = "Allows easier database seeding of tables in Rails."
10
- gemspec.email = "michael@intridea.com"
11
- gemspec.homepage = "http://github.com/mblegih/seed-fu"
12
- gemspec.description = "Seed Fu is an attempt to once and for all solve the problem of inserting and maintaining seed data in a database. It uses a variety of techniques gathered from various places around the web and combines them to create what is hopefully the most robust seed data system around."
13
- gemspec.authors = ["Michael Bleigh"]
14
- gemspec.add_dependency 'rails', '>= 2.1'
15
- gemspec.files = FileList["[A-Z]*", "{lib,spec,rails}/**/*"] - FileList["**/*.log"]
16
- end
17
- Jeweler::GemcutterTasks.new
18
- rescue LoadError
19
- puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
20
- end
21
-
22
- begin
23
- require 'spec/rake/spectask'
24
-
25
- Spec::Rake::SpecTask.new do |t|
26
- t.spec_files = FileList['spec/**/*_spec.rb']
27
- end
28
- rescue LoadError
29
- puts "You need RSpec to run the spec suite."
30
- end
31
-
32
-
33
- desc 'Default: run specs.'
34
- task :default => :spec
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 1.2.3
@@ -1,58 +0,0 @@
1
- module SeedFu
2
-
3
- module Writer
4
-
5
- class Abstract
6
- attr_accessor :seed_handle, :config, :number_of_seeds
7
-
8
- def initialize(options={})
9
- self.config = options
10
- self.number_of_seeds = 0
11
-
12
- self.seed_handle = File.new(self.config[:seed_file], 'w')
13
-
14
- write_header
15
- end
16
-
17
- def header
18
- <<-END
19
- # DO NOT MODIFY THIS FILE, it was auto-generated.
20
- #
21
- # Date: #{DateTime.now}
22
- # Using #{self.class} to seed #{config[:seed_model]}
23
- # Written with the command:
24
- #
25
- # #{$0} #{$*.join}
26
- #
27
- END
28
- end
29
-
30
- def footer
31
- <<-END
32
- # End auto-generated file.
33
- END
34
- end
35
-
36
- def add_seed(hash)
37
- $stdout.puts "Added #{hash.inspect}" unless config[:quiet]
38
- self.number_of_seeds += 1
39
- end
40
-
41
- def write_header
42
- seed_handle.syswrite header
43
- end
44
-
45
- def write_footer
46
- seed_handle.syswrite footer
47
- end
48
-
49
- def finish
50
- write_footer
51
- seed_handle.close
52
- end
53
-
54
- end
55
-
56
- end
57
-
58
- end
@@ -1,26 +0,0 @@
1
- module SeedFu
2
-
3
- module Writer
4
-
5
- class Seed < Abstract
6
-
7
- # This method uses the :seed_by set earlier.
8
- #
9
- def add_seed(hash, seed_by=nil)
10
- seed_by ||= config[:seed_by]
11
- seed_handle.syswrite( <<-END
12
- #{config[:seed_model]}.seed(#{seed_by.collect{|s| ":#{s}"}.join(',')}) { |s|
13
- #{hash.collect{|k,v| " s.#{k} = '#{v.to_s.gsub("'", "\'")}'\n"}.join}}
14
- END
15
- )
16
- super(hash)
17
- if chunk_this_seed?
18
- seed_handle.syswrite "# BREAK EVAL\n"
19
- end
20
- end
21
-
22
- end
23
-
24
- end
25
-
26
- end
@@ -1,52 +0,0 @@
1
- module SeedFu
2
-
3
- module Writer
4
-
5
- class SeedMany < Abstract
6
-
7
- def seed_many_header
8
- "#{config[:seed_model]}.seed_many(#{config[:seed_by].collect{|s| ":#{s}"}.join(',')},["
9
- end
10
-
11
- def seed_many_footer
12
- "\n])\n"
13
- end
14
-
15
- # Chunk in groups of 100 for performance
16
- #
17
- def chunk_this_seed?
18
- 0 == (self.number_of_seeds % (config[:chunk_size] || 100))
19
- end
20
-
21
- def add_seed(hash)
22
- seed_handle.syswrite( (<<-END
23
- #{',' unless self.number_of_seeds == 0 or chunk_this_seed?}
24
- { #{hash.collect{|k,v| ":#{k} => '#{v.to_s.gsub("'", "\'")}'"}.join(', ')} }
25
- END
26
- ).chomp )
27
- super(hash)
28
-
29
- if chunk_this_seed?
30
- seed_handle.syswrite(
31
- self.seed_many_footer +
32
- "# BREAK EVAL\n" +
33
- self.seed_many_header
34
- )
35
- end
36
- end
37
-
38
- def write_header
39
- super
40
- seed_handle.syswrite self.seed_many_header
41
- end
42
-
43
- def write_footer
44
- seed_handle.syswrite self.seed_many_footer
45
- super
46
- end
47
-
48
- end
49
-
50
- end
51
-
52
- end
data/rails/init.rb DELETED
@@ -1 +0,0 @@
1
- require 'seed-fu'
data/spec/schema.rb DELETED
@@ -1,8 +0,0 @@
1
- ActiveRecord::Schema.define :version => 0 do
2
- create_table :seeded_models, :force => true do |t|
3
- t.column :login, :string
4
- t.column :first_name, :string
5
- t.column :last_name, :string
6
- t.column :title, :string
7
- end
8
- end
data/spec/seed_fu_spec.rb DELETED
@@ -1,116 +0,0 @@
1
- require File.dirname(__FILE__) + '/spec_helper'
2
-
3
- load(File.dirname(__FILE__) + '/schema.rb')
4
-
5
- describe SeedFu::Seeder do
6
- it "should create a model if one doesn't exist" do
7
- SeededModel.seed(:id) do |s|
8
- s.id = 1
9
- s.login = "bob"
10
- s.first_name = "Bob"
11
- s.last_name = "Bobson"
12
- s.title = "Peon"
13
- end
14
-
15
- bob = SeededModel.find_by_id(1)
16
- bob.first_name.should == "Bob"
17
- bob.last_name.should == "Bobson"
18
- end
19
-
20
- it "should be able to handle multiple constraints" do
21
- SeededModel.seed(:title, :login) do |s|
22
- s.login = "bob"
23
- s.title = "Peon"
24
- s.first_name = "Bob"
25
- end
26
-
27
- SeededModel.count.should == 1
28
-
29
- SeededModel.seed(:title, :login) do |s|
30
- s.login = "frank"
31
- s.title = "Peon"
32
- s.first_name = "Frank"
33
- end
34
-
35
- SeededModel.count.should == 2
36
-
37
- SeededModel.find_by_login("bob").first_name.should == "Bob"
38
- SeededModel.seed(:title, :login) do |s|
39
- s.login = "bob"
40
- s.title = "Peon"
41
- s.first_name = "Steve"
42
- end
43
- SeededModel.find_by_login("bob").first_name.should == "Steve"
44
- end
45
-
46
- it "should be able to create models from an array of seed attributes" do
47
- SeededModel.seed_many(:title, :login, [
48
- {:login => "bob", :title => "Peon", :first_name => "Steve"},
49
- {:login => "frank", :title => "Peasant", :first_name => "Francis"},
50
- {:login => "harry", :title => "Noble", :first_name => "Harry"}
51
- ])
52
-
53
- SeededModel.find_by_login("bob").first_name.should == "Steve"
54
- SeededModel.find_by_login("frank").first_name.should == "Francis"
55
- SeededModel.find_by_login("harry").first_name.should == "Harry"
56
- end
57
-
58
- it "should update, not create, if constraints are met" do
59
- SeededModel.seed(:id) do |s|
60
- s.id = 1
61
- s.login = "bob"
62
- s.first_name = "Bob"
63
- s.last_name = "Bobson"
64
- s.title = "Peon"
65
- end
66
-
67
- SeededModel.seed(:id) do |s|
68
- s.id = 1
69
- s.login = "bob"
70
- s.first_name = "Robert"
71
- s.last_name = "Bobson"
72
- s.title = "Peon"
73
- end
74
-
75
- bob = SeededModel.find_by_id(1)
76
- bob.first_name.should == "Robert"
77
- bob.last_name.should == "Bobson"
78
- end
79
-
80
- it "should create but not update with seed_once" do
81
- SeededModel.seed_once(:id) do |s|
82
- s.id = 1
83
- s.login = "bob"
84
- s.first_name = "Bob"
85
- s.last_name = "Bobson"
86
- s.title = "Peon"
87
- end
88
-
89
- SeededModel.seed_once(:id) do |s|
90
- s.id = 1
91
- s.login = "bob"
92
- s.first_name = "Robert"
93
- s.last_name = "Bobson"
94
- s.title = "Peon"
95
- end
96
-
97
- bob = SeededModel.find_by_id(1)
98
- bob.first_name.should == "Bob"
99
- bob.last_name.should == "Bobson"
100
- end
101
-
102
- #it "should raise an error if constraints are not unique" do
103
- # SeededModel.create(:login => "bob", :first_name => "Bob", :title => "Peon")
104
- # SeededModel.create(:login => "bob", :first_name => "Robert", :title => "Manager")
105
- #
106
- # SeededModel.seed(:login) do |s|
107
- # s.login = "bob"
108
- # s.title = "Overlord"
109
- # end
110
- #end
111
-
112
- it "should default to an id constraint"
113
- it "should require that all constraints are defined"
114
- it "should raise an error if validation fails"
115
- it "should retain fields that aren't specifically altered in the seeding"
116
- end
data/spec/spec_helper.rb DELETED
@@ -1,8 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
2
-
3
- plugin_spec_dir = File.dirname(__FILE__)
4
- ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
5
-
6
- class SeededModel < ActiveRecord::Base
7
- validates_presence_of :title
8
- end