Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Iain Hecker
committed
Sep 20, 2009
0 parents
commit 0d58f4c
Showing
4 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,26 @@ | |||
require 'activesupport' | |||
require 'fill/configure' | |||
require 'fill/presenter' | |||
require 'fill/procedure' | |||
|
|||
module Fill | |||
|
|||
class << self | |||
|
|||
def database | |||
db = Configure.new | |||
yield db | |||
bm = time { db.perform! } | |||
puts Presenter | |||
puts "Database filled in %.2f seconds" % bm | |||
end | |||
|
|||
def time | |||
started_at = Time.now | |||
yield | |||
Time.now - started_at | |||
end | |||
|
|||
end | |||
|
|||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,69 @@ | |||
module Fill | |||
|
|||
class Configure | |||
|
|||
def produce(*models, &block) | |||
options = models.extract_options! | |||
needs = options.delete(:needs) || [] | |||
register models, Procedure.new(models, options, &block) | |||
dependent models, needs | |||
end | |||
|
|||
def fill(model, field, *values) | |||
options = values.extract_options! | |||
self.produce model, options do | |||
values.each do |value| | |||
model.to_s.singularize.camelize.constantize.create!(field => value) | |||
end | |||
end | |||
end | |||
|
|||
def invoke(task, *models) | |||
self.produce *models do | |||
Rake::Task[task].invoke | |||
end | |||
end | |||
|
|||
def environment(env, which, options = {}) | |||
|
|||
end | |||
|
|||
def perform! | |||
registered.each_key { |model| perform(model) } | |||
end | |||
|
|||
private | |||
|
|||
def results | |||
results = registered.values.uniq.compact.map { |data| data.to_hash } | |||
end | |||
|
|||
def perform(model) | |||
raise "No fill data provided for #{model}" unless registered.has_key? model | |||
dependencies[model].each { |dep| perform(dep) } if dependencies.has_key? model | |||
registered[model].perform! | |||
end | |||
|
|||
def register(models, data) | |||
models.each do |model| | |||
registered.update model => data | |||
end | |||
end | |||
|
|||
def dependent(models, dependent) | |||
models.each do |model| | |||
dependencies.update model => [dependent].flatten | |||
end | |||
end | |||
|
|||
def registered | |||
@registered ||= {} | |||
end | |||
|
|||
def dependencies | |||
@dependencies ||= {} | |||
end | |||
|
|||
end | |||
|
|||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,52 @@ | |||
module Fill | |||
|
|||
class Presenter | |||
|
|||
def self.present(data) | |||
presenter.add(data) | |||
end | |||
|
|||
def self.presenter | |||
@presenter ||= new | |||
end | |||
|
|||
def self.to_s | |||
presenter.to_s | |||
end | |||
|
|||
def add(data) | |||
presented.push(data) if data && !presented.include?(data) | |||
end | |||
|
|||
def hirb? | |||
require 'hirb' | |||
true | |||
rescue LoadError | |||
false | |||
end | |||
|
|||
def presented | |||
@presented ||= [] | |||
end | |||
|
|||
def presentable | |||
presented.map(&:to_hash) | |||
end | |||
|
|||
def present_with_hirb | |||
Hirb::Helpers::Table.render(presentable, :description => false) | |||
end | |||
|
|||
def to_s | |||
hirb? ? present_with_hirb : present_hash.join("\n") | |||
end | |||
|
|||
def present_hash | |||
hash.map do |row| | |||
row.map { |key, value| "#{key}: #{value}" }.join(" - ") | |||
end | |||
end | |||
|
|||
end | |||
|
|||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,48 @@ | |||
module Fill | |||
|
|||
class Procedure | |||
|
|||
attr_accessor :block | |||
|
|||
def initialize(models, options = {}, &block) | |||
@block = block | |||
@options = { :delete => true }.merge(options) | |||
@models = models | |||
end | |||
|
|||
def perform! | |||
@performed ||= perform | |||
end | |||
|
|||
def to_hash | |||
{ "Models" => human_models, "Before" => @before.join(', '), "After" => @after.join(', '), "Time" => @time } | |||
end | |||
|
|||
def human_models | |||
@options[:name] || | |||
models.map { |model| model.respond_to?(:human_name) ? model.human_name : model.to_s }.join(', ') | |||
end | |||
|
|||
def delete_all | |||
models.each { |model| model.delete_all } if @options[:delete] | |||
end | |||
|
|||
def models | |||
@models.map { |model| model.to_s.singularize.camelize.constantize } | |||
end | |||
|
|||
def count | |||
models.map { |model| model.count } | |||
end | |||
|
|||
def perform | |||
@before = count | |||
@time = Fill.time { self.delete_all; block.call } | |||
@after = count | |||
Presenter.present self | |||
true | |||
end | |||
|
|||
end | |||
|
|||
end |