Skip to content

Commit

Permalink
Merged all branches together again.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bleigh committed Aug 16, 2008
2 parents 21bb9a6 + 43b1cf0 commit 8fb5725
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 92 deletions.
4 changes: 3 additions & 1 deletion .gitignore
@@ -1 +1,3 @@
*.log
*.log
*.gem
.DS_Store
6 changes: 6 additions & 0 deletions Rakefile
@@ -0,0 +1,6 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run specs.'
task :default => :spec
2 changes: 1 addition & 1 deletion init.rb
@@ -1 +1 @@
require 'seeder'
require File.dirname(__FILE__) + "/rails/init"
1 change: 0 additions & 1 deletion install.rb

This file was deleted.

6 changes: 6 additions & 0 deletions lib/autotest/discover.rb
@@ -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
87 changes: 87 additions & 0 deletions lib/seed-fu.rb
@@ -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
87 changes: 0 additions & 87 deletions lib/seeder.rb

This file was deleted.

1 change: 1 addition & 0 deletions rails/init.rb
@@ -0,0 +1 @@
require 'seed-fu'
31 changes: 31 additions & 0 deletions seed-fu.gemspec
@@ -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
2 changes: 1 addition & 1 deletion spec/seed_fu_spec.rb
Expand Up @@ -2,7 +2,7 @@

load(File.dirname(__FILE__) + '/schema.rb')

describe Seeder do
describe SeedFu::Seeder do
it "should create a model if one doesn't exist" do
SeededModel.seed(:id) do |s|
s.id = 1
Expand Down
1 change: 0 additions & 1 deletion uninstall.rb

This file was deleted.

0 comments on commit 8fb5725

Please sign in to comment.