population 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.swp
19
+ *.swo
20
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in population.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 David Tuite
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # Population
2
+
3
+ Population is a gem which provides a simple syntax for
4
+ populating and resetting your database tables.
5
+
6
+ - [Public Development Trello](https://trello.com/board/populations/4ffc4c6d685b0d606219fec6)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'population'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install population
21
+
22
+ ## Usage
23
+
24
+ First define some population recipes in `lib/populations.rb`.
25
+
26
+ ```ruby
27
+ # lib/populations.rb
28
+ Population.define do
29
+ populate :users do
30
+ create!(name: "David Tuite", phone: '34893')
31
+ create!(name: "Peter Pan", phone: '093489')
32
+ end
33
+
34
+ populate :states do
35
+ create!(name: 'Alabama')
36
+ end
37
+ end
38
+ ```
39
+
40
+ Now you can use the `Population` class to populate and reset your
41
+ database tables.
42
+
43
+ **Populating**
44
+
45
+ ```
46
+ # From the console:
47
+ $ rails c
48
+ : Population.populate(:users)
49
+
50
+ # From a task
51
+ # lib/tasts/populate.rake
52
+ namespace :db do
53
+ desc "Populate the DB with sample data"
54
+ task :populate => :environment do
55
+ tables = [:users, :states]
56
+ Population.populate(*tables)
57
+ end
58
+ end
59
+
60
+ $ rake db:populate
61
+ ```
62
+
63
+ **Resetting**
64
+
65
+ Resetting will empty the passed in tables and reset any auto-increment
66
+ columns to 1.
67
+
68
+ ```
69
+ # From the console:
70
+ $ rails c
71
+ : Population.reset(:users)
72
+ ```
73
+
74
+ ## Contributing
75
+
76
+ 1. Fork it
77
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
78
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
79
+ 4. Push to the branch (`git push origin my-new-feature`)
80
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,9 @@
1
+ module Population
2
+ class Configuration
3
+ attr_reader :populations
4
+
5
+ def initialize
6
+ @populations = {}
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,39 @@
1
+ module Population
2
+ class Database
3
+
4
+ # Destroy all records belonging to a class and reset the
5
+ # auto-increment ID to 1.
6
+ def self.reset_table_belonging_to(klass)
7
+ klass.destroy_all
8
+ reset_command = reset_command_for(klass)
9
+ ActiveRecord::Base.connection.execute(reset_command)
10
+ end
11
+
12
+ def self.reset_command_for(klass)
13
+ table_name = klass.name.tableize
14
+
15
+ if sqlite?
16
+ "delete from sqlite_sequence where name='#{table_name}';"
17
+ elsif mysql?
18
+ "ALTER TABLE #{table_name} AUTO_INCREMENT = 1;"
19
+ elsif postgres?
20
+ "SELECT setval('public.#{table_name}_id_seq', 1, false)"
21
+ end
22
+ end
23
+
24
+ # Check if we're dealing with a SQLite database.
25
+ def self.sqlite?
26
+ ActiveRecord::Base.connection.adapter_name == 'SQLite'
27
+ end
28
+
29
+ # Check if we're dealing with a PostgreSQL database.
30
+ def self.postgres?
31
+ ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
32
+ end
33
+
34
+ # Check if we're dealing with a MySQL database.
35
+ def self.mysql?
36
+ ActiveRecord::Base.connection.adapter_name == 'MySQL'
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,20 @@
1
+ module Population
2
+ module Definition
3
+ def define(&block)
4
+ DSL.run(block)
5
+ end
6
+
7
+ class DSL
8
+ def populate(name, &block)
9
+ populate = Populate.new(name, &block)
10
+ Population.register_populate(populate)
11
+ end
12
+
13
+ def self.run(block)
14
+ new.instance_eval(&block)
15
+ end
16
+ end
17
+ end
18
+
19
+ extend Definition
20
+ end
@@ -0,0 +1,22 @@
1
+ module Population
2
+ class << self
3
+ attr_accessor :definition_file_paths
4
+ end
5
+
6
+ self.definition_file_paths = %w(populations lib/populations)
7
+
8
+ def self.find_definitions
9
+ absolute_definition_file_paths =
10
+ definition_file_paths.map {|path| File.expand_path(path) }
11
+
12
+ absolute_definition_file_paths.uniq.each do |path|
13
+ load("#{path}.rb") if File.exists?("#{path}.rb")
14
+
15
+ if File.directory? path
16
+ Dir[File.join(path, '**', '*.rb')].sort.each do |file|
17
+ load file
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ module Population
2
+ class Populate
3
+ attr_reader :name, :klass, :block
4
+
5
+ def initialize(name, &block)
6
+ @name = name
7
+ @klass = name.to_s.classify.constantize
8
+ @block = block
9
+ end
10
+
11
+ def up
12
+ @klass.instance_eval(&block)
13
+ end
14
+
15
+ def down
16
+ Population::Database.reset_table_belonging_to(klass)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ require "rails"
2
+
3
+ module Population
4
+ class Railtie < Rails::Railtie
5
+ initializer "population.set_population_paths" do
6
+ Population.definition_file_paths = [
7
+ File.join(Rails.root, 'populations'),
8
+ File.join(Rails.root, 'lib', 'populations')
9
+ ]
10
+ end
11
+
12
+ config.after_initialize do
13
+ Population.find_definitions
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module Population
2
+ VERSION = "0.0.3"
3
+ end
data/lib/population.rb ADDED
@@ -0,0 +1,66 @@
1
+ require 'active_support/core_ext/module/delegation'
2
+ require "active_record"
3
+
4
+ require "population/populate"
5
+ require "population/configuration"
6
+ require "population/database"
7
+ require "population/definition"
8
+ require "population/find_definitions"
9
+ require "population/railtie"
10
+ require "population/version"
11
+
12
+ module Population
13
+
14
+ def self.configuration
15
+ @configuration ||= Configuration.new
16
+ end
17
+
18
+ # Delegate these method to configuration so they can be used
19
+ # as if they were class methods on Population.
20
+ class << self
21
+ delegate :populations, to: :configuration
22
+ end
23
+
24
+ # Register a populate in the populations register.
25
+ def self.register_populate(populate)
26
+ # Register the populate definition in a hash of all populates.
27
+ populations[populate.name] = populate
28
+ populate
29
+ end
30
+
31
+ # Reset one or many tables specified by their class Constant.
32
+ #
33
+ # Arguments:
34
+ #
35
+ # - klasses - One or many class constants.
36
+ #
37
+ # Examples:
38
+ #
39
+ # Population.reset(User)
40
+ #
41
+ # Population.reset(User, Article)
42
+ #
43
+ def self.reset(*table_names)
44
+ table_names.each do |name|
45
+ populations[name].down
46
+ end
47
+ end
48
+
49
+ # Populate one or many tables specified by their table name.
50
+ #
51
+ # Arguments:
52
+ #
53
+ # - klasses - One or many table names.
54
+ #
55
+ # Examples:
56
+ #
57
+ # Population.populate(:users)
58
+ #
59
+ # Population.populate(:users, :articles)
60
+ #
61
+ def self.populate(*table_names)
62
+ table_names.each do |name|
63
+ populations[name].up
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/population/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["David Tuite"]
6
+ gem.email = ["dtuite@gmail.com"]
7
+ gem.description = %q{Population for rails apps.}
8
+ gem.summary = %q{Populate or reset your AcriveRecord models based on FactoryGirl like definitions.}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "population"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Population::VERSION
17
+
18
+ gem.add_dependency "activesupport"
19
+ gem.add_dependency "activerecord"
20
+ gem.add_dependency "railties", ">= 3.0.0"
21
+
22
+ gem.add_development_dependency "rspec"
23
+ gem.add_development_dependency "sqlite3"
24
+ gem.add_development_dependency "rails", "3.0.7"
25
+ end
@@ -0,0 +1,28 @@
1
+ require "spec_helper"
2
+
3
+ describe "Population" do
4
+ before :all do
5
+ Population.define do
6
+ populate :users do
7
+ create!(name: "Peter")
8
+ create!(name: "John")
9
+ end
10
+ end
11
+ end
12
+
13
+ describe "populate" do
14
+ it "should populate database tables" do
15
+ expect do
16
+ Population.populate(:users)
17
+ end.to change(User, :count).by(2)
18
+ end
19
+ end
20
+
21
+ describe "reset" do
22
+ it "should reset database tables" do
23
+ expect do
24
+ Population.reset(:users)
25
+ end.to change(User, :count).by(-2)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,70 @@
1
+ require "spec_helper"
2
+
3
+ describe Population::Database do
4
+ subject { Population::Database }
5
+
6
+ describe "reset_table_belonging_to" do
7
+ # Prevent commands from actually running on the DB.
8
+ before { ActiveRecord::Base.connection.stub(:execute) }
9
+
10
+ it "should call :destroy_all on the class" do
11
+ User.should_receive(:destroy_all)
12
+ subject.reset_table_belonging_to(User)
13
+ end
14
+
15
+ it "should delegate to reset_command_for" do
16
+ subject.should_receive(:reset_command_for).with(User)
17
+ subject.reset_table_belonging_to(User)
18
+ end
19
+
20
+ it "should execute the command" do
21
+ command = "hello"
22
+ subject.stub(:reset_command_for) { command }
23
+ ActiveRecord::Base.connection.should_receive(:execute)
24
+ .with(command)
25
+ subject.reset_table_belonging_to(User)
26
+ end
27
+ end
28
+
29
+ describe "sqlite?" do
30
+ let(:connection) { ActiveRecord::Base.connection }
31
+
32
+ it "should return true if the adapter_name is Sqlite" do
33
+ connection.stub(:adapter_name) { 'SQLite' }
34
+ subject.should be_sqlite
35
+ end
36
+
37
+ it "should return false if the adapter_name is not Sqlite" do
38
+ connection.stub(:adapter_name) { 'Grinnick' }
39
+ subject.should_not be_sqlite
40
+ end
41
+ end
42
+
43
+ describe "mysql?" do
44
+ let(:connection) { ActiveRecord::Base.connection }
45
+
46
+ it "should return true if the adapter_name is MySQL" do
47
+ connection.stub(:adapter_name) { 'MySQL' }
48
+ subject.should be_mysql
49
+ end
50
+
51
+ it "should return false if the adapter_name is not MySQL" do
52
+ connection.stub(:adapter_name) { 'Grinnick' }
53
+ subject.should_not be_mysql
54
+ end
55
+ end
56
+
57
+ describe "postgres?" do
58
+ let(:connection) { ActiveRecord::Base.connection }
59
+
60
+ it "should return true if the adapter_name is PostgreSQL" do
61
+ connection.stub(:adapter_name) { 'PostgreSQL' }
62
+ subject.should be_postgres
63
+ end
64
+
65
+ it "should return false if the adapter_name is not PostgreSQL" do
66
+ connection.stub(:adapter_name) { 'Grinnick' }
67
+ subject.should_not be_postgres
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,40 @@
1
+ require "spec_helper"
2
+
3
+ describe Population::Definition do
4
+ describe "define" do
5
+ it "should delegate to DSL.run" do
6
+ block = Proc.new { "Hello" }
7
+ Population::Definition::DSL.should_receive(:run).with(block)
8
+ Population.define(&block)
9
+ end
10
+ end
11
+
12
+ describe "populate" do
13
+ it "should create a Populate with a name" do
14
+ block = Proc.new { "Hello" }
15
+ Population.define do
16
+ populate(:users, &block)
17
+ end
18
+ Population.populations[:users].name.should == :users
19
+ end
20
+
21
+ it "should pass the block to the Populate instance" do
22
+ block = Proc.new { "Hello" }
23
+ Population.define do
24
+ populate(:users, &block)
25
+ end
26
+ Population.populations[:users].block.should == block
27
+ end
28
+
29
+ it "should register the populate" do
30
+ pop = stub
31
+ Population::Populate.stub(:new) { pop }
32
+ Population.should_receive(:register_populate).with(pop)
33
+
34
+ block = Proc.new { "Hello" }
35
+ Population.define do
36
+ populate(:users, &block)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ require "spec_helper"
2
+
3
+ describe Population::Populate do
4
+ subject { Population::Populate }
5
+
6
+ it "should have a name" do
7
+ pop = subject.new(:users)
8
+ pop.name.should == :users
9
+ end
10
+
11
+ it "should have a klass" do
12
+ pop = subject.new(:users)
13
+ pop.klass.should == User
14
+ end
15
+
16
+ it "should take a block" do
17
+ block = Proc.new { p "hello" }
18
+ pop = subject.new(:users, &block)
19
+ pop.block.should == block
20
+ end
21
+
22
+ describe "up" do
23
+ it "should call the block" do
24
+ User.should_receive(:create!).with(name: "Peter")
25
+ pop = subject.new :users do
26
+ create!(name: "Peter")
27
+ end
28
+ pop.up
29
+ end
30
+ end
31
+
32
+ describe "down" do
33
+ it "should reset the table" do
34
+ Population::Database.should_receive(:reset_table_belonging_to).with(User)
35
+ block = Proc.new { p "hello" }
36
+ pop = subject.new(:users, &block)
37
+ pop.down
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,19 @@
1
+ require "spec_helper"
2
+
3
+ describe Population do
4
+ it "should have a configuration" do
5
+ Population.configuration.should be_instance_of(Population::Configuration)
6
+ end
7
+
8
+ it "should have access to populates" do
9
+ Population.populations.should be_instance_of(Hash)
10
+ end
11
+
12
+ describe "register_populate" do
13
+ it "should register a populate" do
14
+ pop = Population::Populate.new(:users)
15
+ Population.register_populate(pop)
16
+ Population.populations[:users].should == pop
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,2 @@
1
+ require "population"
2
+ require "support/active_record"
@@ -0,0 +1,10 @@
1
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
2
+
3
+ class User < ActiveRecord::Base
4
+ attr_accessible :name
5
+ end
6
+
7
+ ActiveRecord::Migration.create_table :users do |t|
8
+ t.string :name
9
+ end
10
+
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: population
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Tuite
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &70332471984980 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70332471984980
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ requirement: &70332471983600 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70332471983600
36
+ - !ruby/object:Gem::Dependency
37
+ name: railties
38
+ requirement: &70332471981500 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 3.0.0
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70332471981500
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &70332471979140 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70332471979140
58
+ - !ruby/object:Gem::Dependency
59
+ name: sqlite3
60
+ requirement: &70332471994300 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70332471994300
69
+ - !ruby/object:Gem::Dependency
70
+ name: rails
71
+ requirement: &70332471993060 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - =
75
+ - !ruby/object:Gem::Version
76
+ version: 3.0.7
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70332471993060
80
+ description: Population for rails apps.
81
+ email:
82
+ - dtuite@gmail.com
83
+ executables: []
84
+ extensions: []
85
+ extra_rdoc_files: []
86
+ files:
87
+ - .gitignore
88
+ - .rspec
89
+ - Gemfile
90
+ - LICENSE
91
+ - README.md
92
+ - Rakefile
93
+ - lib/population.rb
94
+ - lib/population/configuration.rb
95
+ - lib/population/database.rb
96
+ - lib/population/definition.rb
97
+ - lib/population/find_definitions.rb
98
+ - lib/population/populate.rb
99
+ - lib/population/railtie.rb
100
+ - lib/population/version.rb
101
+ - population.gemspec
102
+ - spec/integration/population_spec.rb
103
+ - spec/population/database_spec.rb
104
+ - spec/population/definition_spec.rb
105
+ - spec/population/populate_spec.rb
106
+ - spec/population_spec.rb
107
+ - spec/spec_helper.rb
108
+ - spec/support/active_record.rb
109
+ homepage: ''
110
+ licenses: []
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 1.8.16
130
+ signing_key:
131
+ specification_version: 3
132
+ summary: Populate or reset your AcriveRecord models based on FactoryGirl like definitions.
133
+ test_files:
134
+ - spec/integration/population_spec.rb
135
+ - spec/population/database_spec.rb
136
+ - spec/population/definition_spec.rb
137
+ - spec/population/populate_spec.rb
138
+ - spec/population_spec.rb
139
+ - spec/spec_helper.rb
140
+ - spec/support/active_record.rb