This repository has been archived by the owner on Jun 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Zack Hobson
committed
May 31, 2008
0 parents
commit 2080d4c
Showing
21 changed files
with
2,276 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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.DS_Store | ||
*~ | ||
website |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
== 0.0.1 2008-01-25 | ||
|
||
* 1 major enhancement: | ||
* Initial release |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2008 Zack Hobson and Justin Balthrop, Geni.com | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
History.txt | ||
License.txt | ||
Manifest.txt | ||
README.txt | ||
Rakefile | ||
config/hoe.rb | ||
config/requirements.rb | ||
lib/model_factory.rb | ||
lib/model_factory/version.rb | ||
log/debug.log | ||
script/destroy | ||
script/generate | ||
script/txt2html | ||
setup.rb | ||
tasks/deployment.rake | ||
tasks/environment.rake | ||
tasks/website.rake | ||
test/test_helper.rb | ||
test/test_model_factory.rb | ||
website/index.html | ||
website/index.txt | ||
website/javascripts/rounded_corners_lite.inc.js | ||
website/stylesheets/screen.css | ||
website/template.rhtml |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
This is ModelFactory, an implementation of the factory concept as proposed by Dan Manges. | ||
|
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require 'config/requirements' | ||
require 'config/hoe' # setup Hoe + all gem configuration | ||
|
||
Dir['tasks/**/*.rake'].each { |rake| load rake } |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
require 'model_factory/version' | ||
|
||
AUTHOR = ['Justin Balthrop', 'Zack Hobson'] # can also be an array of Authors | ||
EMAIL = "justin@geni.com" | ||
DESCRIPTION = "a replacement for fixtures" | ||
GEM_NAME = 'model_factory' # what ppl will type to install your gem | ||
RUBYFORGE_PROJECT = 'model_factory' # The unix name for your project | ||
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org" | ||
DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}" | ||
|
||
@config_file = "~/.rubyforge/user-config.yml" | ||
@config = nil | ||
RUBYFORGE_USERNAME = "unknown" | ||
def rubyforge_username | ||
unless @config | ||
begin | ||
@config = YAML.load(File.read(File.expand_path(@config_file))) | ||
rescue | ||
puts <<-EOS | ||
ERROR: No rubyforge config file found: #{@config_file} | ||
Run 'rubyforge setup' to prepare your env for access to Rubyforge | ||
- See http://newgem.rubyforge.org/rubyforge.html for more details | ||
EOS | ||
exit | ||
end | ||
end | ||
RUBYFORGE_USERNAME.replace @config["username"] | ||
end | ||
|
||
|
||
REV = nil | ||
# UNCOMMENT IF REQUIRED: | ||
# REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil | ||
VERS = ModelFactory::VERSION::STRING + (REV ? ".#{REV}" : "") | ||
RDOC_OPTS = ['--quiet', '--title', 'model_factory documentation', | ||
"--opname", "index.html", | ||
"--line-numbers", | ||
"--main", "README", | ||
"--inline-source"] | ||
|
||
class Hoe | ||
warn_level = $VERBOSE | ||
$VERBOSE = nil | ||
RUBY_FLAGS = "-I#{%w(lib ext bin test).join(File::PATH_SEPARATOR)}" + (RUBY_DEBUG ? " #{RUBY_DEBUG}" : '') | ||
$VERBOSE = warn_level | ||
|
||
def extra_deps | ||
@extra_deps.reject! { |x| Array(x).first == 'hoe' } | ||
@extra_deps | ||
end | ||
end | ||
|
||
# Generate all the Rake tasks | ||
# Run 'rake -T' to see list of generated tasks (from gem root directory) | ||
hoe = Hoe.new(GEM_NAME, VERS) do |p| | ||
p.author = AUTHOR | ||
p.description = DESCRIPTION | ||
p.email = EMAIL | ||
p.summary = DESCRIPTION | ||
p.url = HOMEPATH | ||
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT | ||
p.test_globs = ["test/**/test_*.rb"] | ||
p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean. | ||
|
||
# == Optional | ||
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n") | ||
#p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ] | ||
|
||
#p.spec_extras = {} # A hash of extra values to set in the gemspec. | ||
|
||
end | ||
|
||
CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n") | ||
PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}" | ||
hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc') | ||
hoe.rsync_args = '-av --delete --ignore-errors' |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'fileutils' | ||
include FileUtils | ||
|
||
require 'rubygems' | ||
%w[rake hoe newgem rubigen].each do |req_gem| | ||
begin | ||
require req_gem | ||
rescue LoadError | ||
puts "This Rakefile requires the '#{req_gem}' RubyGem." | ||
puts "Installation: gem install #{req_gem} -y" | ||
exit | ||
end | ||
end | ||
|
||
$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib])) | ||
|
||
require 'model_factory' |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
class FixtureConverter | ||
def initialize(opts = {}) | ||
@body = [] | ||
@header = [] | ||
@indent_depth = opts[:indent_depth] || 0 | ||
@output_style = opts[:output_style] | ||
end | ||
|
||
def powder? | ||
@output_style == :clay | ||
end | ||
|
||
def convert_fixture(path) | ||
fixture = YAML.load_file(path) | ||
return if not fixture | ||
|
||
plural_model_name = path.basename.to_s.split('.').first | ||
model_name = plural_model_name.singularize | ||
header do | ||
"#{plural_model_name} = {}" | ||
end | ||
|
||
body 'before(:all) do' if powder? | ||
indent(powder?) do | ||
body "# Setup #{plural_model_name}" | ||
body "# Generated from fixture in #{path.dirname.basename}/#{path.basename}" | ||
body '#' | ||
body '' | ||
fixture.each do |name, record| | ||
max_length = record.keys.collect {|key| key.length}.max | ||
|
||
body "#{plural_model_name}[:#{name}] = Factory.create_#{model_name}(" | ||
indent do | ||
body do | ||
record.collect do |key,value| | ||
value = "\"#{value}\"" if value.kind_of?(String) | ||
key = key.ljust(max_length) | ||
":#{key} => #{value}" | ||
end | ||
end | ||
end | ||
body ')' | ||
end | ||
end | ||
body 'end' if powder? | ||
body '' | ||
end | ||
|
||
def convert_scenario(path) | ||
path.each_entry do |file| | ||
next if file.extname != '.yml' | ||
next if file.basename.to_s =~ /relationships/ | ||
convert_fixture( path + file ) | ||
end | ||
end | ||
|
||
def out | ||
puts @header.join("\n") | ||
puts "\n" | ||
puts @body.join("\n") | ||
end | ||
|
||
private | ||
INDENT = ' ' | ||
|
||
def indent(enabled = true) | ||
@indent_depth += 1 if enabled | ||
yield | ||
@indent_depth -= 1 if enabled | ||
end | ||
|
||
def body(*lines) | ||
lines = yield if block_given? | ||
lines = [lines].flatten | ||
|
||
lines.each do |line| | ||
@body << indent_line(line, @indent_depth) | ||
end | ||
end | ||
|
||
def header(*lines) | ||
lines = yield if block_given? | ||
lines = [lines].flatten | ||
|
||
lines.each do |line| | ||
@header << line | ||
end | ||
end | ||
|
||
def indent_line(line, indent_depth) | ||
ws = '' | ||
indent_depth.times do | ||
ws += INDENT | ||
end | ||
ws + line | ||
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
require 'rubygems' | ||
require 'active_record' | ||
|
||
module ModelFactory | ||
def next_local_id | ||
@max_id ||= 0 | ||
return @max_id += 1 | ||
end | ||
|
||
def default(class_type, defaults={}) | ||
class_name = class_type.name.underscore | ||
|
||
(class << self; self; end).module_eval do | ||
define_method "create_#{class_name}" do |*args| | ||
attributes = args.first || {} | ||
create_instance(class_type, attributes, defaults) | ||
end | ||
|
||
define_method "new_#{class_name}" do |*args| | ||
attributes = args.first || {} | ||
new_instance(class_type, attributes, defaults) | ||
end | ||
|
||
define_method "default_#{class_name}" do |*args| | ||
attributes = args.first || {} | ||
default_closure(class_type, attributes, defaults) | ||
end | ||
end | ||
end | ||
|
||
def create_instance(class_type, attributes, defaults = {}) | ||
attributes = instantiate_defaults(:create, defaults.merge(attributes)) | ||
instance = class_type.create!(attributes) | ||
if update_protected_attributes(instance, attributes) | ||
instance.save | ||
end | ||
instance | ||
end | ||
|
||
def new_instance(class_type, attributes, defaults = {}) | ||
attributes = instantiate_defaults(:new, defaults.merge(attributes)) | ||
instance = class_type.new(attributes) | ||
instance.id = next_local_id | ||
update_protected_attributes(instance, attributes) | ||
instance | ||
end | ||
|
||
def default_closure(class_type, attributes, defaults = {}) | ||
lambda do |create_or_new| | ||
case create_or_new | ||
when :new : new_instance(class_type, attributes, defaults) | ||
when :create : create_instance(class_type, attributes, defaults) | ||
end | ||
end | ||
end | ||
|
||
def instantiate_defaults(create_or_new, attributes) | ||
attributes.each do |key, value| | ||
if value.is_a?(Proc) | ||
attributes[key] = value.arity == 0 ? value.call : value.call(create_or_new) | ||
end | ||
end | ||
attributes | ||
end | ||
|
||
def update_protected_attributes(instance, attributes) | ||
modified = false | ||
protected_attrs = instance.class.protected_attributes | ||
protected_attrs = protected_attrs.to_set if protected_attrs | ||
accessible_attrs = instance.class.accessible_attributes | ||
accessible_attrs = accessible_attrs.to_set if accessible_attrs | ||
|
||
if protected_attrs or accessible_attrs | ||
attributes.each do |key, value| | ||
# Support symbols and strings. | ||
[key, key.to_s].each do |attr| | ||
next if protected_attrs and not protected_attrs.include?(attr) | ||
next if accessible_attrs and accessible_attrs.include?(attr) | ||
end | ||
modified = true | ||
instance.send("#{key}=", value) | ||
end | ||
end | ||
return modified | ||
end | ||
|
||
# Any class methods of the form "new_some_type(attrs)" or "create_some_type(attrs)" will be converted to | ||
# "SomeType.new(attrs)" and "SomeType.create!(attrs)" respectively. | ||
# These basically function as though you'd used the 'default' directive with empty defaults. | ||
def method_missing(missing_method, attributes = {}) | ||
if missing_method.to_s.match(/^(new|create|default)_([a-z][\w_]+)$/) | ||
method, class_name = $1, $2 | ||
class_type = class_name.camelize.constantize | ||
case method | ||
when 'create' | ||
create_instance(class_type, attributes) | ||
when 'new' | ||
new_instance(class_type, attributes) | ||
when 'default' | ||
default_closure(class_type, attributes) | ||
end | ||
else | ||
raise NoMethodError, "no such method '#{missing_method}'" | ||
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module ModelFactory #:nodoc: | ||
module VERSION #:nodoc: | ||
MAJOR = 0 | ||
MINOR = 7 | ||
TINY = 0 | ||
|
||
STRING = [MAJOR, MINOR, TINY].join('.') | ||
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env ruby | ||
APP_ROOT = File.join(File.dirname(__FILE__), '..') | ||
|
||
begin | ||
require 'rubigen' | ||
rescue LoadError | ||
require 'rubygems' | ||
require 'rubigen' | ||
end | ||
require 'rubigen/scripts/destroy' | ||
|
||
ARGV.shift if ['--help', '-h'].include?(ARGV[0]) | ||
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit] | ||
RubiGen::Scripts::Destroy.new.run(ARGV) |
Oops, something went wrong.