grimen-bootstrapper 0.1.0

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/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Sevenwire.
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.markdown ADDED
@@ -0,0 +1,95 @@
1
+ Bootstrapper
2
+ ============
3
+
4
+ A rather simple plugin that executes a block to load seed/bootstrap data
5
+ into a database.
6
+
7
+
8
+ Example
9
+ =======
10
+
11
+ Run the bootstrapper generator:
12
+
13
+ ruby script/generate bootstrapper
14
+
15
+ This will place a bootstrap.rb file in the db directory within your project.
16
+
17
+ The file will have something like this in it:
18
+
19
+ Bootstrapper.for :development do |b|
20
+ end
21
+
22
+ Bootstrapper.for :production do |b|
23
+ end
24
+
25
+ Bootstrapper.for :test do |b|
26
+ end
27
+
28
+ Bootstrapper.for :staging do |b|
29
+ end
30
+
31
+ Using things like Factory Girl and Forgery you can quickly and easily
32
+ generate fake, random, and/or seed data.
33
+
34
+ An example using Factory Girl:
35
+
36
+ Bootstrapper.for :development do |b|
37
+ b.truncate_tables :addresses
38
+ b.run :users
39
+
40
+ Factory(:us_address, :state => "ME")
41
+ Factory(:us_address, :state => "IL")
42
+ Factory(:us_address, :state => "CA")
43
+ end
44
+
45
+ Bootstrapper.for :production do |b|
46
+ end
47
+
48
+ Bootstrapper.for :test do |b|
49
+ end
50
+
51
+ Bootstrapper.for :staging do |b|
52
+ end
53
+
54
+ Bootstrapper.for :users do |b|
55
+ 3.times{ Factory(:user) }
56
+ Factory(:user, :login => "admin",
57
+ :password => "sekret",
58
+ :password_confirmation => "sekret")
59
+ end
60
+
61
+ With that file, you could run:
62
+
63
+ rake db:bootstrap
64
+
65
+ Which will run the development (default) bootstrap. You can specify which
66
+ bootstrap task to run by specifying:
67
+
68
+ rake db:bootstrap BOOTSTRAP=users
69
+
70
+ You can also run bootstrap for another environment:
71
+
72
+ rake db:bootstrap RAILS_ENV=production
73
+
74
+ You can even run a specific bootstrap for another environment:
75
+
76
+ rake db:bootstrap BOOTSTRAP=users RAILS_ENV=production
77
+
78
+ The variable passed into a Bootstrapper block is actually Bootstrapper
79
+ itself, so you can use any methods it has.
80
+
81
+ You can delete all records from tables using 'truncate_tables':
82
+
83
+ b.truncate_tables :users
84
+ b.truncate_tables :users, :addresses
85
+
86
+ You can run other bootstrap tasks using 'run':
87
+
88
+ b.run :users
89
+ b.run :production
90
+
91
+ FactoryGirl [http://github.com/thoughtbot/factory_girl/tree/master](http://github.com/thoughtbot/factory_girl/tree/master)
92
+
93
+ Forgery [http://github.com/sevenwire/forgery/tree/master](http://github.com/sevenwire/forgery/tree/master)
94
+
95
+ Copyright (c) 2008 Sevenwire, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ NAME = "bootstrapper"
6
+ SUMMARY = %Q{A Rails plugin to assist in bootstrapping and seeding your database.}
7
+ HOMEPAGE = "http://github.com/grimen/#{NAME}/tree/master"
8
+ AUTHOR = "Sevenwire"
9
+ EMAIL = "git@sevenwire.com"
10
+ SUPPORT_FILES = %w(README.markdown)
11
+
12
+ begin
13
+ require 'jeweler'
14
+ Jeweler::Tasks.new do |gem|
15
+ gem.name = NAME
16
+ gem.summary = SUMMARY
17
+ gem.description = SUMMARY
18
+ gem.homepage = HOMEPAGE
19
+ gem.author = AUTHOR
20
+ gem.email = EMAIL
21
+
22
+ gem.require_paths = %w{lib}
23
+ gem.files = SUPPORT_FILES + %w(MIT-LICENSE Rakefile) + Dir.glob(File.join('{generators,lib,test,tasks}', '**', '*'))
24
+ gem.executables = %w()
25
+ gem.extra_rdoc_files = SUPPORT_FILES
26
+ end
27
+ rescue LoadError
28
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
29
+ end
30
+
31
+ desc %Q{Run unit tests for "#{NAME}".}
32
+ task :default => :test
33
+
34
+ desc %Q{Run unit tests for "#{NAME}".}
35
+ Rake::TestTask.new(:test) do |test|
36
+ test.libs << 'lib'
37
+ test.pattern = File.join('test', '**', '*_test.rb')
38
+ test.verbose = true
39
+ end
40
+
41
+ desc %Q{Generate documentation for "#{NAME}".}
42
+ Rake::RDocTask.new(:rdoc) do |rdoc|
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = NAME
45
+ rdoc.options << %w(--line-numbers --inline-source --charset=UTF-8)
46
+ rdoc.rdoc_files.include(SUPPORT_FILES)
47
+ rdoc.rdoc_files.include(File.join('lib', '**', '*.rb'))
48
+ end
@@ -0,0 +1,8 @@
1
+ class BootstrapperGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.directory 'db'
5
+ m.template 'bootstrap.erb', 'db/bootstrap.rb'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ <%- ActiveRecord::Base.configurations.keys.each do |env| -%>
2
+ Bootstrapper.for :<%= env %> do |b|
3
+ end
4
+
5
+ <%- end -%>
@@ -0,0 +1,39 @@
1
+ class Bootstrapper
2
+ class_inheritable_accessor :tasks
3
+ write_inheritable_attribute :tasks, HashWithIndifferentAccess.new
4
+
5
+ def self.for(key, &block)
6
+ tasks[key] = block
7
+ end
8
+
9
+ def self.run(key)
10
+ puts ">> Started executing bootstrap for #{key}"
11
+ tasks[key].call(self)
12
+ puts ">> Finished executing bootstrap for #{key}"
13
+ end
14
+
15
+ def self.truncate_tables(*tables)
16
+ options = tables.last.is_a?(Hash) ? tables.pop : {}
17
+ if tables == [:all]
18
+ except = options[:except] || []
19
+ except = except.is_a?(Array) ? except.collect { |x| x.to_s } : [except.to_s]
20
+
21
+ tables = ActiveRecord::Base.connection.tables.select do |table|
22
+ table !~ /schema_(info|migrations)/ && !except.include?(table)
23
+ end
24
+ end
25
+
26
+ tables.each do |table|
27
+ ActiveRecord::Base.connection.truncate_table(table)
28
+ end
29
+ end
30
+
31
+ def self.fixtures(*fixtures)
32
+ Fixtures.create_fixtures(File.join(RAILS_ROOT, 'db', 'populate'), fixtures)
33
+ Fixtures.create_fixtures(File.join(RAILS_ROOT, 'db', 'populate', RAILS_ENV), fixtures)
34
+ end
35
+
36
+ def self.sql(sql)
37
+ ActiveRecord::Base.connection.execute(sql)
38
+ end
39
+ end
@@ -0,0 +1,15 @@
1
+ module ActiveRecord
2
+
3
+ module ConnectionAdapters
4
+
5
+ class AbstractAdapter
6
+
7
+ def truncate_table(table_name)
8
+ execute "DELETE FROM #{table_name}"
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,15 @@
1
+ module ActiveRecord
2
+
3
+ module ConnectionAdapters
4
+
5
+ class MysqlAdapter
6
+
7
+ def truncate_table(table_name)
8
+ execute "TRUNCATE TABLE #{table_name}"
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,28 @@
1
+ namespace :db do
2
+ desc "Bootstraps the database for the given environment. BOOTSTRAP option lets you run a specific bootstrap task in the given environment."
3
+ task :bootstrap => :environment do
4
+ require File.join(RAILS_ROOT, 'db', 'bootstrap')
5
+ Bootstrapper.run(ENV['BOOTSTRAP'] || RAILS_ENV)
6
+ end
7
+
8
+ namespace :bootstrap do
9
+ desc "Resets the database and bootstraps it for the given environment. BOOTSTRAP option lets you run a specific bootstrap task in the given environment."
10
+ task :reset => ['db:migrate:reset', 'reset_environment'] do
11
+ Rake::Task['db:bootstrap'].invoke
12
+ end
13
+ end
14
+ end
15
+
16
+ desc "Resets the rails environment"
17
+ task :reset_environment do
18
+ # Ripped directly out of railties/lib/console_app.rb in rails and
19
+ # only changed slightly.
20
+ puts "Reloading environment..."
21
+ if ActionController::Dispatcher.respond_to?(:cleanup_application)
22
+ dispatcher = ActionController::Dispatcher
23
+ else
24
+ dispatcher = ActionController::Dispatcher.new($stdout)
25
+ end
26
+ dispatcher.cleanup_application
27
+ dispatcher.reload_application
28
+ end
@@ -0,0 +1,8 @@
1
+ require 'test/unit'
2
+
3
+ class BootstrapperTest < Test::Unit::TestCase
4
+ # Replace this with your real tests.
5
+ def test_this_plugin
6
+ flunk
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grimen-bootstrapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sevenwire
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-14 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A Rails plugin to assist in bootstrapping and seeding your database.
17
+ email: git@sevenwire.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.markdown
24
+ files:
25
+ - MIT-LICENSE
26
+ - README.markdown
27
+ - Rakefile
28
+ - generators/bootstrapper/bootstrapper_generator.rb
29
+ - generators/bootstrapper/templates/bootstrap.erb
30
+ - lib/bootstrapper.rb
31
+ - lib/connection_adapters/abstract_adapter.rb
32
+ - lib/connection_adapters/mysql_adapter.rb
33
+ - tasks/bootstrap.rake
34
+ - test/bootstrapper_test.rb
35
+ has_rdoc: false
36
+ homepage: http://github.com/grimen/bootstrapper/tree/master
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --charset=UTF-8
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.2.0
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: A Rails plugin to assist in bootstrapping and seeding your database.
61
+ test_files:
62
+ - test/bootstrapper_test.rb