seed 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. data/LICENSE +23 -0
  2. data/README +1 -0
  3. data/Rakefile +92 -0
  4. data/lib/seed.rb +37 -0
  5. data/spec/db/schema.rb +5 -0
  6. data/spec/seed_spec.rb +53 -0
  7. metadata +68 -0
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ LICENSE
2
+
3
+ The MIT License
4
+
5
+ Copyright (c) 2008-2009 Jeremy Durham
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1 @@
1
+ Seed - Another simple seeding library
data/Rakefile ADDED
@@ -0,0 +1,92 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rubygems/specification'
4
+ require 'rake/rdoctask'
5
+ require 'spec/rake/spectask'
6
+ require 'active_record'
7
+ require File.join(File.dirname(__FILE__), 'lib', 'seed')
8
+
9
+ NAME = "seed"
10
+ AUTHOR = "Jeremy Durham"
11
+ EMAIL = "jeremydurham@gmail.com"
12
+ HOMEPAGE = ""
13
+ SUMMARY = "Another simple seeding library"
14
+
15
+ # Used by release task
16
+ GEM_NAME = NAME
17
+ PROJECT_URL = HOMEPAGE
18
+ PROJECT_SUMMARY = SUMMARY
19
+ PROJECT_DESCRIPTION = SUMMARY
20
+
21
+ PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
22
+ GEM_VERSION = Seed::VERSION + PKG_BUILD
23
+ RELEASE_NAME = "REL #{GEM_VERSION}"
24
+ #
25
+ # ==== Gemspec and installation
26
+ #
27
+
28
+ spec = Gem::Specification.new do |s|
29
+ s.name = NAME
30
+ s.version = Seed::VERSION
31
+ s.platform = Gem::Platform::RUBY
32
+ s.has_rdoc = true
33
+ s.extra_rdoc_files = ["README", "LICENSE"]
34
+ s.summary = SUMMARY
35
+ s.description = s.summary
36
+ s.author = AUTHOR
37
+ s.email = EMAIL
38
+ s.homepage = HOMEPAGE
39
+ s.require_path = 'lib'
40
+ s.files = %w(LICENSE README Rakefile) + Dir.glob("{lib,spec}/**/*")
41
+ end
42
+
43
+ Rake::GemPackageTask.new(spec) do |pkg|
44
+ pkg.gem_spec = spec
45
+ end
46
+
47
+ desc "install the gem locally"
48
+ task :install => [:clean, :package] do
49
+ sh %{sudo gem install pkg/#{NAME}-#{Seed::VERSION} --no-update-sources}
50
+ end
51
+
52
+ desc "create a gemspec file"
53
+ task :make_spec do
54
+ File.open("#{GEM_NAME}.gemspec", "w") do |file|
55
+ file.puts spec.to_ruby
56
+ end
57
+ end
58
+
59
+ desc "Run coverage suite"
60
+ task :rcov do
61
+ require 'fileutils'
62
+ FileUtils.rm_rf("coverage") if File.directory?("coverage")
63
+ FileUtils.mkdir("coverage")
64
+ path = File.expand_path(Dir.pwd)
65
+ files = Dir["spec/**/*_spec.rb"]
66
+ files.each do |spec|
67
+ puts "Getting coverage for #{File.expand_path(spec)}"
68
+ command = %{rcov #{File.expand_path(spec)} --aggregate #{path}/coverage/data.data}
69
+ command += " --no-html" unless spec == files.last
70
+ `#{command} 2>&1`
71
+ end
72
+ end
73
+
74
+ file_list = FileList['spec/**/*_spec.rb']
75
+
76
+ desc "Run all examples"
77
+ Spec::Rake::SpecTask.new('spec') do |t|
78
+ t.spec_files = file_list
79
+ end
80
+
81
+ namespace :spec do
82
+ desc "Run all examples with RCov"
83
+ Spec::Rake::SpecTask.new('rcov') do |t|
84
+ t.spec_files = file_list
85
+ t.rcov = true
86
+ t.rcov_dir = "doc/coverage"
87
+ t.rcov_opts = ['--exclude', 'spec']
88
+ end
89
+ end
90
+
91
+ desc 'Default: run unit tests.'
92
+ task :default => 'spec'
data/lib/seed.rb ADDED
@@ -0,0 +1,37 @@
1
+ class Seed
2
+ VERSION = '1.0.1'
3
+
4
+ def self.plant(klass, name, attributes={}, &block)
5
+ raise RuntimeError, "You cannot overwrite an existing seed" if self.planted?(klass, name)
6
+
7
+ if block_given?
8
+ @seeds[klass][name] = klass.create!(&block)
9
+ else
10
+ @seeds[klass][name] = klass.create!(attributes)
11
+ end
12
+ end
13
+
14
+ def self.retrieve(klass, name)
15
+ self.planted?(klass, name) || raise(RuntimeError, "No seed #{name} for #{klass}")
16
+ end
17
+
18
+ def self.planted?(klass, name)
19
+ @seeds ||= {}
20
+ @seeds[klass] ||= {}
21
+ @seeds[klass][name]
22
+ end
23
+ end
24
+
25
+ class ActiveRecord::Base
26
+ def self.seed(*options, &block)
27
+ if block_given?
28
+ Seed.plant(self, options.first, &block)
29
+ else
30
+ if options.length > 1
31
+ Seed.plant(self, options.first, options.last)
32
+ else
33
+ Seed.retrieve(self, options.first)
34
+ end
35
+ end
36
+ end
37
+ end
data/spec/db/schema.rb ADDED
@@ -0,0 +1,5 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :users, :force => true do |t|
3
+ t.string :email, :password
4
+ end
5
+ end
data/spec/seed_spec.rb ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'active_record'
4
+ require 'seed'
5
+
6
+ class User < ActiveRecord::Base
7
+ validates_presence_of :email
8
+ end
9
+
10
+ describe Seed do
11
+ before do
12
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
13
+ load File.join(File.dirname(__FILE__), 'db', 'schema.rb')
14
+ end
15
+
16
+ describe "planting a seed" do
17
+ it "should raise a RuntimeError when trying to overwrite a seed" do
18
+ User.seed(:first, :email => 'test@testing.com')
19
+ lambda do
20
+ User.seed(:first, :email => 'testing@test.com')
21
+ end.should raise_error(RuntimeError)
22
+ end
23
+
24
+ it "should create a new seed given valid attributes" do
25
+ lambda do
26
+ User.seed(:tester, :email => 'test@testing.com')
27
+ end.should change(User, :count).by(1)
28
+ end
29
+
30
+ describe "given a block" do
31
+ it "should create a new seed" do
32
+ lambda do
33
+ User.seed(:block_user) do |user|
34
+ user.email = 'test@test.com'
35
+ end
36
+ end.should change(User, :count).by(1)
37
+ end
38
+ end
39
+ end
40
+
41
+ describe "retrieving a seed" do
42
+ it "should raise a RuntimeError when trying to retrieve an invalid seed" do
43
+ lambda do
44
+ User.seed(:invalid_user)
45
+ end.should raise_error(RuntimeError)
46
+ end
47
+
48
+ it "should return a seed" do
49
+ @user = User.seed(:new_user, :email => 'newuser@test.com')
50
+ @user.should == User.seed(:new_user)
51
+ end
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seed
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 1
9
+ version: 1.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Jeremy Durham
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-04 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Another simple seeding library
22
+ email: jeremydurham@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README
29
+ - LICENSE
30
+ files:
31
+ - LICENSE
32
+ - README
33
+ - Rakefile
34
+ - lib/seed.rb
35
+ - spec/db/schema.rb
36
+ - spec/seed_spec.rb
37
+ has_rdoc: true
38
+ homepage: ""
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.6
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Another simple seeding library
67
+ test_files: []
68
+