Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
136 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
*.log | ||
*.log | ||
*.gem | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require 'rake' | ||
require 'rake/testtask' | ||
require 'rake/rdoctask' | ||
|
||
desc 'Default: run specs.' | ||
task :default => :spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
require 'seeder' | ||
require File.dirname(__FILE__) + "/rails/init" |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Need this to get picked up by autotest? | ||
$:.push(File.join(File.dirname(__FILE__), %w[.. .. rspec])) | ||
|
||
Autotest.add_discovery do | ||
"rspec" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
module SeedFu | ||
class Seeder | ||
def self.plant(model_class, *constraints, &block) | ||
constraints = [:id] if constraints.empty? | ||
seed = Seeder.new(model_class) | ||
insert_only = constraints.last.is_a? TrueClass | ||
constraints.delete_at(*constraints.length-1) if (constraints.last.is_a? TrueClass or constraints.last.is_a? FalseClass) | ||
seed.set_constraints(*constraints) | ||
yield seed | ||
seed.plant!(insert_only) | ||
end | ||
|
||
def initialize(model_class) | ||
@model_class = model_class | ||
@constraints = [:id] | ||
@data = {} | ||
end | ||
|
||
def set_constraints(*constraints) | ||
raise "You must set at least one constraint." if constraints.empty? | ||
@constraints = [] | ||
constraints.each do |constraint| | ||
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) | ||
@constraints << constraint.to_sym | ||
end | ||
end | ||
|
||
def set_attribute(name, value) | ||
@data[name.to_sym] = value | ||
end | ||
|
||
def plant! insert_only=false | ||
record = get | ||
return if !record.new_record? and insert_only | ||
@data.each do |k, v| | ||
record.send("#{k}=", v) | ||
end | ||
record.save! | ||
record | ||
end | ||
|
||
def method_missing(method_name, *args) #:nodoc: | ||
if (match = method_name.to_s.match(/(.*)=$/)) && args.size == 1 | ||
set_attribute(match[1], args.first) | ||
else | ||
super | ||
end | ||
end | ||
|
||
protected | ||
|
||
def get | ||
records = @model_class.find(:all, :conditions => condition_hash) | ||
raise "Given constraints yielded multiple records." unless records.size < 2 | ||
if records.any? | ||
return records.first | ||
else | ||
return @model_class.new | ||
end | ||
end | ||
|
||
def condition_hash | ||
@data.reject{|a,v| !@constraints.include?(a)} | ||
end | ||
end | ||
end | ||
|
||
|
||
# Creates a single record of seed data for use | ||
# with the db:seed rake task. | ||
# | ||
# === Parameters | ||
# constraints :: Immutable reference attributes. Defaults to :id | ||
def self.seed(*constraints, &block) | ||
Seeder.plant(self, *constraints, &block) | ||
end | ||
|
||
def self.seed_many(*constraints) | ||
seeds = constraints.pop | ||
seeds.each do |seed_data| | ||
seed(*constraints) do |s| | ||
seed_data.each_pair do |k,v| | ||
s.send "#{k}=", v | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'seed-fu' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
Gem::Specification.new do |s| | ||
s.name = 'seed-fu' | ||
s.version = '1.0.200807042' | ||
s.date = '2008-07-04' | ||
|
||
s.summary = "Allows easier database seeding of tables" | ||
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." | ||
|
||
s.authors = ["Michael Bleigh"] | ||
s.email = "michael@intridea.com" | ||
s.homepage = 'http://github.com/mbleigh/seed-fu' | ||
|
||
s.has_rdoc = true | ||
s.rdoc_options = ["--main", "README"] | ||
s.extra_rdoc_files = ["README"] | ||
|
||
s.add_dependency 'rails', ['>= 2.1'] | ||
|
||
s.files = ["README", | ||
"Rakefile", | ||
"init.rb", | ||
"lib/seed-fu.rb", | ||
"lib/autotest/discover.rb", | ||
"rails/init.rb", | ||
"seed-fu.gemspec", | ||
"spec/schema.rb", | ||
"spec/seed_fu_spec.rb", | ||
"spec/spec_helper.rb", | ||
"tasks/seed_fu_tasks.rake"] | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.