jcnetdev-seed-fu 1.0.3 → 1.0.20080704
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/seed-fu.rb +11 -25
- data/seed-fu.gemspec +6 -7
- data/spec/seed_fu_spec.rb +0 -12
- data/tasks/seed_fu_tasks.rake +2 -3
- metadata +8 -10
- data/lib/autotest/discover.rb +0 -6
data/lib/seed-fu.rb
CHANGED
@@ -3,19 +3,17 @@ module SeedFu
|
|
3
3
|
def self.plant(model_class, *constraints, &block)
|
4
4
|
constraints = [:id] if constraints.empty?
|
5
5
|
seed = Seeder.new(model_class)
|
6
|
-
insert_only = constraints.last.is_a? TrueClass
|
7
|
-
constraints.delete_at(*constraints.length-1) if (constraints.last.is_a? TrueClass or constraints.last.is_a? FalseClass)
|
8
6
|
seed.set_constraints(*constraints)
|
9
7
|
yield seed
|
10
|
-
seed.plant!
|
8
|
+
seed.plant!
|
11
9
|
end
|
12
|
-
|
10
|
+
|
13
11
|
def initialize(model_class)
|
14
12
|
@model_class = model_class
|
15
13
|
@constraints = [:id]
|
16
14
|
@data = {}
|
17
15
|
end
|
18
|
-
|
16
|
+
|
19
17
|
def set_constraints(*constraints)
|
20
18
|
raise "You must set at least one constraint." if constraints.empty?
|
21
19
|
@constraints = []
|
@@ -24,22 +22,21 @@ module SeedFu
|
|
24
22
|
@constraints << constraint.to_sym
|
25
23
|
end
|
26
24
|
end
|
27
|
-
|
25
|
+
|
28
26
|
def set_attribute(name, value)
|
29
27
|
@data[name.to_sym] = value
|
30
28
|
end
|
31
|
-
|
32
|
-
def plant!
|
29
|
+
|
30
|
+
def plant!
|
33
31
|
record = get
|
34
|
-
return if !record.new_record? and insert_only
|
35
32
|
@data.each do |k, v|
|
36
33
|
record.send("#{k}=", v)
|
37
34
|
end
|
38
35
|
record.save!
|
39
|
-
puts " - #{@model_class} #{condition_hash.inspect}"
|
36
|
+
puts " - #{@model_class} #{condition_hash.inspect}"
|
40
37
|
record
|
41
38
|
end
|
42
|
-
|
39
|
+
|
43
40
|
def method_missing(method_name, *args) #:nodoc:
|
44
41
|
if (match = method_name.to_s.match(/(.*)=$/)) && args.size == 1
|
45
42
|
set_attribute(match[1], args.first)
|
@@ -47,9 +44,9 @@ module SeedFu
|
|
47
44
|
super
|
48
45
|
end
|
49
46
|
end
|
50
|
-
|
47
|
+
|
51
48
|
protected
|
52
|
-
|
49
|
+
|
53
50
|
def get
|
54
51
|
records = @model_class.find(:all, :conditions => condition_hash)
|
55
52
|
raise "Given constraints yielded multiple records." unless records.size < 2
|
@@ -59,7 +56,7 @@ module SeedFu
|
|
59
56
|
return @model_class.new
|
60
57
|
end
|
61
58
|
end
|
62
|
-
|
59
|
+
|
63
60
|
def condition_hash
|
64
61
|
@data.reject{|a,v| !@constraints.include?(a)}
|
65
62
|
end
|
@@ -76,15 +73,4 @@ class ActiveRecord::Base
|
|
76
73
|
def self.seed(*constraints, &block)
|
77
74
|
SeedFu::Seeder.plant(self, *constraints, &block)
|
78
75
|
end
|
79
|
-
|
80
|
-
def self.seed_many(*constraints)
|
81
|
-
seeds = constraints.pop
|
82
|
-
seeds.each do |seed_data|
|
83
|
-
seed(*constraints) do |s|
|
84
|
-
seed_data.each_pair do |k,v|
|
85
|
-
s.send "#{k}=", v
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
76
|
end
|
data/seed-fu.gemspec
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'seed-fu'
|
3
|
-
s.version = '1.0.
|
4
|
-
s.date = '2008-
|
3
|
+
s.version = '1.0.20080704'
|
4
|
+
s.date = '2008-07-04'
|
5
5
|
|
6
|
-
s.summary = "Allows easier database seeding of tables
|
6
|
+
s.summary = "Allows easier database seeding of tables"
|
7
7
|
s.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."
|
8
8
|
|
9
9
|
s.authors = ["Michael Bleigh"]
|
10
10
|
s.email = "michael@intridea.com"
|
11
11
|
s.homepage = 'http://github.com/mbleigh/seed-fu'
|
12
12
|
|
13
|
-
s.has_rdoc =
|
14
|
-
s.rdoc_options = ["--main", "README"]
|
15
|
-
s.extra_rdoc_files = ["README"]
|
13
|
+
s.has_rdoc = false
|
14
|
+
# s.rdoc_options = ["--main", "README"]
|
15
|
+
#s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
|
16
16
|
|
17
17
|
s.add_dependency 'rails', ['>= 2.1']
|
18
18
|
|
@@ -20,7 +20,6 @@ Gem::Specification.new do |s|
|
|
20
20
|
"Rakefile",
|
21
21
|
"init.rb",
|
22
22
|
"lib/seed-fu.rb",
|
23
|
-
"lib/autotest/discover.rb",
|
24
23
|
"rails/init.rb",
|
25
24
|
"seed-fu.gemspec",
|
26
25
|
"spec/schema.rb",
|
data/spec/seed_fu_spec.rb
CHANGED
@@ -43,18 +43,6 @@ describe SeedFu::Seeder do
|
|
43
43
|
SeededModel.find_by_login("bob").first_name.should == "Steve"
|
44
44
|
end
|
45
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
46
|
#it "should raise an error if constraints are not unique" do
|
59
47
|
# SeededModel.create(:login => "bob", :first_name => "Bob", :title => "Peon")
|
60
48
|
# SeededModel.create(:login => "bob", :first_name => "Robert", :title => "Manager")
|
data/tasks/seed_fu_tasks.rake
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
namespace :db do
|
2
2
|
desc "Loads seed data from db/fixtures for the current environment."
|
3
3
|
task :seed => :environment do
|
4
|
-
|
5
|
-
Dir[File.join(RAILS_ROOT, fixture_path, '*.rb')].sort.each { |fixture|
|
4
|
+
Dir[File.join(RAILS_ROOT, 'db', 'fixtures', '*.rb')].sort.each { |fixture|
|
6
5
|
puts "\n== Seeding from #{File.split(fixture).last} " + ("=" * (60 - (17 + File.split(fixture).last.length)))
|
7
6
|
load fixture
|
8
7
|
puts "=" * 60 + "\n"
|
9
8
|
}
|
10
|
-
Dir[File.join(RAILS_ROOT,
|
9
|
+
Dir[File.join(RAILS_ROOT, 'db', 'fixtures', RAILS_ENV, '*.rb')].sort.each { |fixture|
|
11
10
|
puts "\n== [#{RAILS_ENV}] Seeding from #{File.split(fixture).last} " + ("=" * (60 - (20 + File.split(fixture).last.length + RAILS_ENV.length)))
|
12
11
|
load fixture
|
13
12
|
puts "=" * 60 + "\n"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jcnetdev-seed-fu
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.20080704
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Bleigh
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-07-04 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -27,26 +27,24 @@ executables: []
|
|
27
27
|
|
28
28
|
extensions: []
|
29
29
|
|
30
|
-
extra_rdoc_files:
|
31
|
-
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
32
32
|
files:
|
33
33
|
- README
|
34
34
|
- Rakefile
|
35
35
|
- init.rb
|
36
36
|
- lib/seed-fu.rb
|
37
|
-
- lib/autotest/discover.rb
|
38
37
|
- rails/init.rb
|
39
38
|
- seed-fu.gemspec
|
40
39
|
- spec/schema.rb
|
41
40
|
- spec/seed_fu_spec.rb
|
42
41
|
- spec/spec_helper.rb
|
43
42
|
- tasks/seed_fu_tasks.rake
|
44
|
-
has_rdoc:
|
43
|
+
has_rdoc: false
|
45
44
|
homepage: http://github.com/mbleigh/seed-fu
|
46
45
|
post_install_message:
|
47
|
-
rdoc_options:
|
48
|
-
|
49
|
-
- README
|
46
|
+
rdoc_options: []
|
47
|
+
|
50
48
|
require_paths:
|
51
49
|
- lib
|
52
50
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -67,6 +65,6 @@ rubyforge_project:
|
|
67
65
|
rubygems_version: 1.2.0
|
68
66
|
signing_key:
|
69
67
|
specification_version: 2
|
70
|
-
summary: Allows easier database seeding of tables
|
68
|
+
summary: Allows easier database seeding of tables
|
71
69
|
test_files: []
|
72
70
|
|