Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
major clean up with new gemspec, Gemfile, and .rvmrc, specs are not y…
Browse files Browse the repository at this point in the history
…et working though
  • Loading branch information
ryanb committed Jan 18, 2011
1 parent 1d723e7 commit 45ce92b
Show file tree
Hide file tree
Showing 36 changed files with 168 additions and 270 deletions.
9 changes: 6 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
pkg
doc
spec/database.yml
Gemfile.lock
.bundle
*.gem
tmp/*
*.sqlite3
database.yml
nbproject
*.swp
**/*.swp
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--color
23 changes: 23 additions & 0 deletions .rvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

# adapted from: http://rvm.beginrescueend.com/workflow/rvmrc/

ruby_string="1.9.2"
gemset_name="populator"

if rvm list strings | grep -q "${ruby_string}" ; then

# Load or create the specified environment
if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
&& -s "${rvm_path:-$HOME/.rvm}/environments/${ruby_string}@${gemset_name}" ]] ; then
\. "${rvm_path:-$HOME/.rvm}/environments/${ruby_string}@${gemset_name}"
else
rvm --create "${ruby_string}@${gemset_name}"
fi

else

# Notify the user to install the desired interpreter before proceeding.
echo "${ruby_string} was not found, please run 'rvm install ${ruby_string}' and then cd back into the project directory."

fi
10 changes: 10 additions & 0 deletions CHANGELOG → CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
1.0.0 (not yet released)

* adding PostgreSQL adapter (thanks phlawski)

* adding Oracle adapter (thanks Andrew N)


0.2.4 (September 9th, 2008)

* removing echoe from gem development dependencies, which didn't seem to work in the first place.

* adding Populator.paragraphs to generate paragraphs of text


0.2.3 (September 2nd, 2008)

* support single table inhertance by setting inheritance_column to class name

* support custom primary_key in model if they don't use "id"


0.2.2 (September 1st, 2008)

* performance improvements

* improving inline documentation


0.2.1 (August 30th, 2008)

* wrap sqlite inserts in transaction to improve performance

* default created_at/on and updated_at/on columns to current time


0.2.0 (August 30th, 2008)

* adding :per_query option to limit how many inserts are made per query
Expand All @@ -34,6 +43,7 @@

* adding Populator.words to fetch some random words


0.1.0 (August 27th, 2008)

* initial release
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source "http://rubygems.org"

gemspec
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2008 Ryan Bates
Copyright (c) 2011 Ryan Bates

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
27 changes: 0 additions & 27 deletions Manifest

This file was deleted.

16 changes: 8 additions & 8 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Populate an Active Record database with mass insert.

You can find the rdocs at http://populator.rubyforge.org.

Special thanks to Zach Dennis for his ar-extensions gem which some of
Special thanks to Zach Dennis for his ar-extensions gem which some of
this code is loosely based on.


Expand All @@ -21,8 +21,8 @@ And then load it in your project:

== Usage

This gem adds a "populate" method to all Active Record models. Pass the
number of records you want to create along with a block. In the block
This gem adds a "populate" method to all Active Record models. Pass the
number of records you want to create along with a block. In the block
you can set the column values for each record.

Person.populate(3000) do |person|
Expand Down Expand Up @@ -50,10 +50,10 @@ Passing a range or array of values will randomly select one.
person.annual_income = 10000..200000
end

This will create 1000 to 5000 men or women with the annual income
This will create 1000 to 5000 men or women with the annual income
between 10,000 and 200,000.

You can pass a :per_query option to limit how many records are saved
You can pass a :per_query option to limit how many records are saved
per query. This defaults to 1000.

Person.populate(2000, :per_query => 100)
Expand All @@ -72,8 +72,8 @@ http://faker.rubyforge.org

== Important

For performance reasons, this gem does not use actual instances of the
model. This means validations and callbacks are bypassed. It is up to
For performance reasons, this gem does not use actual instances of the
model. This means validations and callbacks are bypassed. It is up to
you to ensure you're adding valid data.


Expand All @@ -87,5 +87,5 @@ http://github.com/ryanb/populator

If you find a bug, please send me a message on GitHub.

If you would like to contribute to this project, please fork the
If you would like to contribute to this project, please fork the
repository and send me a pull request.
32 changes: 20 additions & 12 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
require 'rubygems'
require 'rake'
require 'echoe'

Echoe.new('populator', '0.2.4') do |p|
p.summary = "Mass populate an Active Record database."
p.description = "Mass populate an Active Record database."
p.url = "http://github.com/ryanb/populator"
p.author = 'Ryan Bates'
p.email = "ryan (at) railscasts (dot) com"
p.ignore_pattern = ["script/*", "**/*.sqlite3", "tmp/*"]
p.development_dependencies = []
end
require 'rspec/core/rake_task'

ADAPTERS = YAML.load(File.read(File.dirname(__FILE__) + "/../spec/database.yml")).keys

desc "Run specs under all supported databases"
task :spec => ADAPTERS.map { |a| "spec:#{a}" }

Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
namespace :spec do
ADAPTERS.each do |adapter|
namespace :prepare do
task adapter do
ENV["POPULATOR_ADAPTER"] = adapter
end
end

desc "Run specs under #{adapter}"
RSpec::Core::RakeTask.new(adapter => "spec:prepare:#{adapter}") do |t|
t.verbose = false
end
end
end
11 changes: 0 additions & 11 deletions TODO

This file was deleted.

2 changes: 1 addition & 1 deletion lib/populator/adapters/abstract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Abstract
def execute_batch(sql, name = nil)
raise NotImplementedError, "execute_batch is an abstract method"
end

def populate(table, columns, rows, name = nil)
execute("INSERT INTO #{table} #{columns} VALUES #{rows.join(', ')}", name)
end
Expand Down
5 changes: 2 additions & 3 deletions lib/populator/adapters/oracle.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Populator
module Adapters
module Oracle
module Oracle

# Executes SQL statements one at a time.
# Executes SQL statements one at a time.

def populate(table, columns, rows, name = nil)
rows.each do |row|
Expand All @@ -24,4 +24,3 @@ class OracleAdapter < ActiveRecord::ConnectionAdapters::AbstractAdapter
end
end
end

2 changes: 1 addition & 1 deletion lib/populator/adapters/postgresql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ class PostgreSQLAdapter < ActiveRecord::ConnectionAdapters::AbstractAdapter
include Populator::Adapters::Postgresql
end
end
end
end
2 changes: 1 addition & 1 deletion lib/populator/adapters/sqlite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def execute_batch(sql, name = nil)
end
end
end

def populate(table, columns, rows, name = nil)
queries = []
rows.each do |row|
Expand Down
26 changes: 13 additions & 13 deletions lib/populator/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ module Populator
# Builds multiple Populator::Record instances and saves them to the database
class Factory
DEFAULT_RECORDS_PER_QUERY = 1000

@factories = {}
@depth = 0

# Fetches the factory dedicated to a given model class. You should always use this
# method instead of instatiating a factory directly so that a single factory is
# shared on multiple calls.
def self.for_model(model_class)
@factories[model_class] ||= new(model_class)
end

# Find all remaining factories and call save_records on them.
def self.save_remaining_records
@factories.values.each do |factory|
factory.save_records
end
@factories = {}
end

# Keep track of nested factory calls so we can save the remaining records once we
# are done with the base factory. This makes Populator more efficient when nesting
# factories.
Expand All @@ -30,20 +30,20 @@ def self.remember_depth
@depth -= 1
save_remaining_records if @depth.zero?
end

# Use for_model instead of instatiating a record directly.
def initialize(model_class)
@model_class = model_class
@records = []
end

# Entry method for building records. Delegates to build_records after remember_depth.
def populate(amount, options = {}, &block)
self.class.remember_depth do
build_records(Populator.interpret_value(amount), options[:per_query] || DEFAULT_RECORDS_PER_QUERY, &block)
end
end

# Builds multiple Populator::Record instances and calls save_records them when
# :per_query limit option is reached.
def build_records(amount, per_query, &block)
Expand All @@ -54,7 +54,7 @@ def build_records(amount, per_query, &block)
save_records if @records.size >= per_query
end
end

# Saves the records to the database by calling populate on the current database adapter.
def save_records
unless @records.empty?
Expand All @@ -63,23 +63,23 @@ def save_records
@records.clear
end
end

private

def quoted_column_names
@model_class.column_names.map do |column_name|
@model_class.connection.quote_column_name(column_name)
end
end

def last_id_in_database
@last_id_in_database ||= @model_class.connection.select_value("SELECT id FROM #{@model_class.quoted_table_name} ORDER BY id DESC", "#{@model_class.name} Last ID").to_i
end

def columns_sql
"(#{quoted_column_names.join(', ')})"
end

def rows_sql_arr
@records.map do |record|
quoted_attributes = record.attribute_values.map { |v| @model_class.sanitize(v) }
Expand Down
Loading

0 comments on commit 45ce92b

Please sign in to comment.