simonmenke-mr_seed 0.0.1

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.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 FIX_ME:author
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
File without changes
@@ -0,0 +1,7 @@
1
+ name: mr_seed
2
+ github: simonmenke
3
+ description: Wrapper for seed fu
4
+ author: Simon Menke
5
+ email: simon.menke@gmail.com
6
+ dependencies:
7
+ - simonmenke-diamonds >= 0.0.1
File without changes
@@ -0,0 +1,7 @@
1
+
2
+ require 'diamonds'
3
+
4
+ # Add other depending gems here
5
+
6
+ module MrSeed
7
+ end
@@ -0,0 +1,10 @@
1
+
2
+ module MrSeed
3
+ module VERSION #:nodoc:
4
+ MAJOR = 0
5
+ MINOR = 0
6
+ TINY = 1
7
+
8
+ STRING = [MAJOR, MINOR, TINY].join('.')
9
+ end
10
+ end
File without changes
@@ -0,0 +1,6 @@
1
+
2
+ setup do
3
+
4
+ # remeber_to_migrate!
5
+
6
+ end
@@ -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
@@ -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
@@ -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
@@ -0,0 +1,96 @@
1
+ module SeedFu
2
+ mattr_accessor :fixture_paths
3
+ self.fixture_paths = [
4
+ ENV["FIXTURE_PATH"],
5
+ File.join(RAILS_ROOT, "db/fixtures")
6
+ ].compact
7
+
8
+ class Seeder
9
+ def self.plant(model_class, *constraints, &block)
10
+ constraints = [:id] if constraints.empty?
11
+ seed = Seeder.new(model_class)
12
+ insert_only = constraints.last.is_a? TrueClass
13
+ constraints.delete_at(*constraints.length-1) if (constraints.last.is_a? TrueClass or constraints.last.is_a? FalseClass)
14
+ seed.set_constraints(*constraints)
15
+ yield seed
16
+ seed.plant!(insert_only)
17
+ end
18
+
19
+ def initialize(model_class)
20
+ @model_class = model_class
21
+ @constraints = [:id]
22
+ @data = {}
23
+ end
24
+
25
+ def set_constraints(*constraints)
26
+ raise "You must set at least one constraint." if constraints.empty?
27
+ @constraints = []
28
+ constraints.each do |constraint|
29
+ 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)
30
+ @constraints << constraint.to_sym
31
+ end
32
+ end
33
+
34
+ def set_attribute(name, value)
35
+ @data[name.to_sym] = value
36
+ end
37
+
38
+ def plant! insert_only=false
39
+ record = get
40
+ return if !record.new_record? and insert_only
41
+ @data.each do |k, v|
42
+ record.send("#{k}=", v)
43
+ end
44
+ record.save!
45
+ puts " - #{@model_class} #{condition_hash.inspect}"
46
+ record
47
+ end
48
+
49
+ def method_missing(method_name, *args) #:nodoc:
50
+ if (match = method_name.to_s.match(/(.*)=$/)) && args.size == 1
51
+ set_attribute(match[1], args.first)
52
+ else
53
+ super
54
+ end
55
+ end
56
+
57
+ protected
58
+
59
+ def get
60
+ records = @model_class.find(:all, :conditions => condition_hash)
61
+ raise "Given constraints yielded multiple records." unless records.size < 2
62
+ if records.any?
63
+ return records.first
64
+ else
65
+ return @model_class.new
66
+ end
67
+ end
68
+
69
+ def condition_hash
70
+ @data.reject{|a,v| !@constraints.include?(a)}
71
+ end
72
+ end
73
+ end
74
+
75
+
76
+ class ActiveRecord::Base
77
+ # Creates a single record of seed data for use
78
+ # with the db:seed rake task.
79
+ #
80
+ # === Parameters
81
+ # constraints :: Immutable reference attributes. Defaults to :id
82
+ def self.seed(*constraints, &block)
83
+ SeedFu::Seeder.plant(self, *constraints, &block)
84
+ end
85
+
86
+ def self.seed_many(*constraints)
87
+ seeds = constraints.pop
88
+ seeds.each do |seed_data|
89
+ seed(*constraints) do |s|
90
+ seed_data.each_pair do |k,v|
91
+ s.send "#{k}=", v
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1 @@
1
+ require 'seed-fu'
@@ -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
@@ -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,29 @@
1
+ namespace :db do
2
+ desc "Loads seed data from db/fixtures for the current environment."
3
+ task :seed => :environment do
4
+
5
+ require File.join(File.dirname(__FILE__), '../lib/seed-fu')
6
+
7
+ if File.file? "config/diamonds.yml"
8
+ diamonds ||= YAML.load_file("config/diamonds.yml")
9
+ diamonds.each do |diamond|
10
+ spec = Gem.source_index.find_name(diamond[:name], diamond[:version]).first
11
+ SeedFu.fixture_paths << "#{spec.full_gem_path}/db/fixtures"
12
+ end
13
+ end
14
+
15
+ SeedFu.fixture_paths.each do |path|
16
+ next unless File.directory? path
17
+ Dir[File.join(path, '*.rb')].sort.each { |fixture|
18
+ puts "\n== Seeding from #{File.split(fixture).last} " + ("=" * (60 - (17 + File.split(fixture).last.length)))
19
+ load fixture
20
+ puts "=" * 60 + "\n"
21
+ }
22
+ Dir[File.join(path, RAILS_ENV, '*.rb')].sort.each { |fixture|
23
+ puts "\n== [#{RAILS_ENV}] Seeding from #{File.split(fixture).last} " + ("=" * (60 - (20 + File.split(fixture).last.length + RAILS_ENV.length)))
24
+ load fixture
25
+ puts "=" * 60 + "\n"
26
+ }
27
+ end
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simonmenke-mr_seed
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Simon Menke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-03 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: simonmenke-diamonds
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.0.1
23
+ version:
24
+ description:
25
+ email: simon.menke@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - lib/mr_seed/version.rb
32
+ - lib/mr_seed.rb
33
+ - README
34
+ - License.txt
35
+ files:
36
+ - config/routes.rb
37
+ - lib/mr_seed
38
+ - lib/mr_seed/version.rb
39
+ - lib/mr_seed.rb
40
+ - rails/init.rb
41
+ - vendor/plugins
42
+ - vendor/plugins/seed-fu
43
+ - vendor/plugins/seed-fu/init.rb
44
+ - vendor/plugins/seed-fu/lib
45
+ - vendor/plugins/seed-fu/lib/autotest
46
+ - vendor/plugins/seed-fu/lib/autotest/discover.rb
47
+ - vendor/plugins/seed-fu/lib/seed-fu.rb
48
+ - vendor/plugins/seed-fu/rails
49
+ - vendor/plugins/seed-fu/rails/init.rb
50
+ - vendor/plugins/seed-fu/Rakefile
51
+ - vendor/plugins/seed-fu/README
52
+ - vendor/plugins/seed-fu/seed-fu.gemspec
53
+ - vendor/plugins/seed-fu/spec
54
+ - vendor/plugins/seed-fu/spec/schema.rb
55
+ - vendor/plugins/seed-fu/spec/seed_fu_spec.rb
56
+ - vendor/plugins/seed-fu/spec/spec_helper.rb
57
+ - vendor/plugins/seed-fu/tasks
58
+ - vendor/plugins/seed-fu/tasks/seed_fu_tasks.rake
59
+ - setup/setup.rb
60
+ - README
61
+ - about.yml
62
+ - License.txt
63
+ has_rdoc: true
64
+ homepage:
65
+ post_install_message:
66
+ rdoc_options:
67
+ - --main
68
+ - README
69
+ - --line-numbers
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ version:
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ version:
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.2.0
88
+ signing_key:
89
+ specification_version: 2
90
+ summary: Wrapper for seed fu
91
+ test_files: []
92
+