jcnetdev-seed-fu 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README ADDED
@@ -0,0 +1,66 @@
1
+ Seed Fu
2
+ =======
3
+
4
+ Seed Fu is an attempt to once and for all solve the problem of inserting and
5
+ maintaining seed data in a database. It uses a variety of techniques gathered
6
+ from various places around the web and combines them to create what is
7
+ hopefully the most robust seed data system around.
8
+
9
+ If you have suggestions or come across problems, please report them on
10
+ the Lighthouse project for Seed Fu:
11
+ http://mbleigh.lighthouseapp.com/projects/10223-seed-fu/overview
12
+
13
+ Usage
14
+ =======
15
+
16
+ Seed data is taken from the db/fixtures directory. Simply make descriptive .rb
17
+ files in that directory (they can be named anything, but users.rb for the User
18
+ model seed data makes sense, etc.). These scripts will be run whenever the
19
+ db:seed rake task is called. You can put arbitrary Ruby code in these files,
20
+ but remember that it will be executed every time the rake task is called, so
21
+ it needs to be runnable multiple times on the same database.
22
+
23
+ You can also have environment-specific seed data placed in
24
+ db/fixtures/ENVIRONMENT that will only be loaded if that is the current
25
+ environment.
26
+
27
+ Let's say we want to populate a few default users. In db/fixtures/users.rb we
28
+ write the following code:
29
+
30
+ User.seed(:login, :email) do |s|
31
+ s.login = "bob"
32
+ s.email = "bob@bobson.com"
33
+ s.first_name = "Bob"
34
+ s.last_name = "Bobson"
35
+ end
36
+
37
+ User.seed(:login, :email) do |s|
38
+ s.login = "bob"
39
+ s.email = "bob@stevenson.com"
40
+ s.first_name = "Bob"
41
+ s.last_name = "Stevenson"
42
+ end
43
+
44
+ That's all you have to do! You will now have two users created in the system
45
+ and you can change their first and last names in the users.rb file and it will
46
+ be updated as such.
47
+
48
+ The arguments passed to the <ActiveRecord>.seed method are the constraining
49
+ attributes: these must remain unchanged between db:seed calls to avoid data
50
+ duplication. By default, seed data will constrain to the id like so:
51
+
52
+ Category.seed do |s|
53
+ s.id = 1
54
+ s.name = "Buttons"
55
+ end
56
+
57
+ Category.seed do |s|
58
+ s.id = 2
59
+ s.name = "Bobbins"
60
+ s.parent_id = 1
61
+ end
62
+
63
+ Note that any constraints that are passed in must be present in the subsequent
64
+ seed data setting.
65
+
66
+ Copyright (c) 2008 Michael Bleigh, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run specs.'
6
+ task :default => :spec
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init"
@@ -0,0 +1,6 @@
1
+ # Need this to get picked up by autotest?
2
+ $:.push(File.join(File.dirname(__FILE__), %w[.. .. rspec]))
3
+
4
+ Autotest.add_discovery do
5
+ "rspec"
6
+ end
data/lib/seed-fu.rb ADDED
@@ -0,0 +1,90 @@
1
+ module SeedFu
2
+ class Seeder
3
+ def self.plant(model_class, *constraints, &block)
4
+ constraints = [:id] if constraints.empty?
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
+ seed.set_constraints(*constraints)
9
+ yield seed
10
+ seed.plant!(insert_only)
11
+ end
12
+
13
+ def initialize(model_class)
14
+ @model_class = model_class
15
+ @constraints = [:id]
16
+ @data = {}
17
+ end
18
+
19
+ def set_constraints(*constraints)
20
+ raise "You must set at least one constraint." if constraints.empty?
21
+ @constraints = []
22
+ constraints.each do |constraint|
23
+ raise "Your constraint `#{constraint}` is not a column in #{@model_class}. Valid columns are `#{@model_class.column_names.join("`, `")}`." unless @model_class.column_names.include?(constraint.to_s)
24
+ @constraints << constraint.to_sym
25
+ end
26
+ end
27
+
28
+ def set_attribute(name, value)
29
+ @data[name.to_sym] = value
30
+ end
31
+
32
+ def plant! insert_only=false
33
+ record = get
34
+ return if !record.new_record? and insert_only
35
+ @data.each do |k, v|
36
+ record.send("#{k}=", v)
37
+ end
38
+ record.save!
39
+ puts " - #{@model_class} #{condition_hash.inspect}"
40
+ record
41
+ end
42
+
43
+ def method_missing(method_name, *args) #:nodoc:
44
+ if (match = method_name.to_s.match(/(.*)=$/)) && args.size == 1
45
+ set_attribute(match[1], args.first)
46
+ else
47
+ super
48
+ end
49
+ end
50
+
51
+ protected
52
+
53
+ def get
54
+ records = @model_class.find(:all, :conditions => condition_hash)
55
+ raise "Given constraints yielded multiple records." unless records.size < 2
56
+ if records.any?
57
+ return records.first
58
+ else
59
+ return @model_class.new
60
+ end
61
+ end
62
+
63
+ def condition_hash
64
+ @data.reject{|a,v| !@constraints.include?(a)}
65
+ end
66
+ end
67
+ end
68
+
69
+
70
+ class ActiveRecord::Base
71
+ # Creates a single record of seed data for use
72
+ # with the db:seed rake task.
73
+ #
74
+ # === Parameters
75
+ # constraints :: Immutable reference attributes. Defaults to :id
76
+ def self.seed(*constraints, &block)
77
+ SeedFu::Seeder.plant(self, *constraints, &block)
78
+ 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
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'seed-fu'
data/seed-fu.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'seed-fu'
3
+ s.version = '0.0.3'
4
+ s.date = '2008-08-16'
5
+
6
+ s.summary = "Allows easier database seeding of tables."
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
+
9
+ s.authors = ["Michael Bleigh"]
10
+ s.email = "michael@intridea.com"
11
+ s.homepage = 'http://github.com/mbleigh/seed-fu'
12
+
13
+ s.has_rdoc = true
14
+ s.rdoc_options = ["--main", "README"]
15
+ s.extra_rdoc_files = ["README"]
16
+
17
+ s.add_dependency 'rails', ['>= 2.1']
18
+
19
+ s.files = ["README",
20
+ "Rakefile",
21
+ "init.rb",
22
+ "lib/seed-fu.rb",
23
+ "lib/autotest/discover.rb",
24
+ "rails/init.rb",
25
+ "seed-fu.gemspec",
26
+ "spec/schema.rb",
27
+ "spec/seed_fu_spec.rb",
28
+ "spec/spec_helper.rb",
29
+ "tasks/seed_fu_tasks.rake"]
30
+
31
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,73 @@
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 raise an error if constraints are not unique" do
59
+ # SeededModel.create(:login => "bob", :first_name => "Bob", :title => "Peon")
60
+ # SeededModel.create(:login => "bob", :first_name => "Robert", :title => "Manager")
61
+ #
62
+ # SeededModel.seed(:login) do |s|
63
+ # s.login = "bob"
64
+ # s.title = "Overlord"
65
+ # end
66
+ #end
67
+
68
+ it "should default to an id constraint"
69
+ it "should update, not create, if constraints are met"
70
+ it "should require that all constraints are defined"
71
+ it "should raise an error if validation fails"
72
+ it "should retain fields that aren't specifically altered in the seeding"
73
+ end
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,16 @@
1
+ namespace :db do
2
+ desc "Loads seed data from db/fixtures for the current environment."
3
+ task :seed => :environment do
4
+ fixture_path = ENV["FIXTURE_PATH"] ? ENV["FIXTURE_PATH"] : "db/fixtures"
5
+ Dir[File.join(RAILS_ROOT, fixture_path, '*.rb')].sort.each { |fixture|
6
+ puts "\n== Seeding from #{File.split(fixture).last} " + ("=" * (60 - (17 + File.split(fixture).last.length)))
7
+ load fixture
8
+ puts "=" * 60 + "\n"
9
+ }
10
+ Dir[File.join(RAILS_ROOT, fixture_path, RAILS_ENV, '*.rb')].sort.each { |fixture|
11
+ puts "\n== [#{RAILS_ENV}] Seeding from #{File.split(fixture).last} " + ("=" * (60 - (20 + File.split(fixture).last.length + RAILS_ENV.length)))
12
+ load fixture
13
+ puts "=" * 60 + "\n"
14
+ }
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jcnetdev-seed-fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Michael Bleigh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-08-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "2.1"
23
+ version:
24
+ 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.
25
+ email: michael@intridea.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README
32
+ files:
33
+ - README
34
+ - Rakefile
35
+ - init.rb
36
+ - lib/seed-fu.rb
37
+ - lib/autotest/discover.rb
38
+ - rails/init.rb
39
+ - seed-fu.gemspec
40
+ - spec/schema.rb
41
+ - spec/seed_fu_spec.rb
42
+ - spec/spec_helper.rb
43
+ - tasks/seed_fu_tasks.rake
44
+ has_rdoc: true
45
+ homepage: http://github.com/mbleigh/seed-fu
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --main
49
+ - README
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.2.0
68
+ signing_key:
69
+ specification_version: 2
70
+ summary: Allows easier database seeding of tables.
71
+ test_files: []
72
+