Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

first commit

  • Loading branch information...
commit 04e86c9a40ac094e3346824d733d86c36d1c7f3c 0 parents
@garysweaver authored
35 .gitignore
@@ -0,0 +1,35 @@
+# See http://help.github.com/ignore-files/ for more about ignoring files.
+#
+# If you find yourself ignoring temporary files generated by your text editor
+# or operating system, you probably want to add a global ignore instead:
+# git config --global core.excludesfile ~/.gitignore_global
+
+# Ignore bundler config
+/.bundle
+
+# Ignore the default SQLite database.
+/db/*.sqlite3
+
+# Ignore all logfiles and tempfiles.
+/log/*.log
+/tmp
+
+# IDea
+*.iml
+*.ipr
+*.iws
+*.ids
+*.idea
+.idea/
+
+# OS X
+.DS_Store
+
+# Ignore RVM config
+.rvmrc
+
+# Ignore mistakenly committed temp files from Sublime Text 2
+untitled
+
+# Ignore gems
+**.gem
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2012 Gary S. Weaver
+
+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.
66 README.md
@@ -0,0 +1,66 @@
+Stepford
+=====
+
+*This is untested and under construction. Hoping to spend more time on it later...*
+
+### Setup
+
+In your Rails 3+ project, add this to your Gemfile:
+
+ gem 'stepford', :git => 'git://github.com/garysweaver/stepford.git'
+
+Then run:
+
+ bundle install
+
+### Usage
+
+#### Factory Girl
+
+To automatically create factories for [Factory Girl][factory_girl] from models, somewhere in tests, use:
+
+ # create default factories for all models
+ Stepford::FactoryGirl.define_factories
+
+or:
+
+ Stepford::FactoryGirl.define_factories(:my_model_name_1, :my_model_name_2, :my_model_name_3)
+
+TODO: need to somehow hook in so that we aren't overwriting other factories.
+
+#### Machinist
+
+Feel free to fork and add support.
+
+### Configuration
+
+ Stepford.configure do
+ @debug = true
+ @model_attr_match_test_data = {
+ /|*.\_at,*.\_on,|/ => Time.now
+ }
+ @model_datatype_test_data = {
+ string: lambda{|c|generate(:random_string)},
+ integer: lambda{|c|Random.rand(9999)},
+ decimal: lambda{|c|Random.rand(9999.9)},
+ datetime: Time.now,
+ timestamp: Time.now,
+ time: Time.now,
+ date: Time.now,
+ binary: Random.bytes(8),
+ boolean: true,
+ xml: lambda{|c|"<test>#{generate(:random_string)}</test>"},
+ ts_vector: nil
+ }
+ end
+
+* debug is for debugging.
+* attr_match_test_data is hash of regexp to match model column names.
+* model_datatype_test_data is hash of regexp to match model column types.
+
+### License
+
+Copyright (c) 2012 Gary S. Weaver, released under the [MIT license][lic].
+
+[factory_girl]: https://github.com/thoughtbot/factory_girl/
+[lic]: http://github.com/garysweaver/restful_json/blob/master/LICENSE
11 Rakefile
@@ -0,0 +1,11 @@
+require 'rake/testtask'
+
+#http://nicksda.apotomo.de/2010/10/testing-your-rails-3-engine-sitting-in-a-gem/
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'test'
+ t.test_files = FileList['test/**/*_test.rb']
+ t.verbose = true
+end
+
+desc "Run tests"
+task :default => :test
1  lib/stepford.rb
@@ -0,0 +1 @@
+require 'stepford/version'
48 lib/stepford/factory_girl.rb
@@ -0,0 +1,48 @@
+require 'factory_girl'
+
+module Stepford
+ class FactoryGirl
+ def self.define_factories(model_names)
+ model_names = Array.wrap(model_names)
+ if model_names.size > 0
+ # Assume the people want them all so...
+ # Load all the models and let's crap out if fails to load any models, as that is probably not a good thing
+ Dir.glob(File.join(Rails.root, 'app', 'models', '*.rb').each { |file| require file }
+ models = ActiveRecord::Base.descendants.collect { |type| type.name }.sort
+ end
+
+ puts "#{self} models: #{model_names.collect{|model_name|model_name.to_sym.inspect}.join(', ')}" if Stepford.debug
+ model_names.each do |model_name|
+ model_class = model_name.constantize
+ FactoryGirl.define do
+ factory model_name.to_sym do
+ # auto-create associations to other factories
+ model_class.reflect_on_all_associations.each do |a|
+ association a.name.to_sym, factory: a.model_class.underscore.to_sym
+ end
+
+ # set all attributes with sample data
+ model_class.columns.each do |c|
+ send(c.name.to_sym, sample_data(c))
+ end
+ end
+ end
+ end
+ end
+
+ private
+
+ def sample_data(column)
+ Stepford.model_attr_match_test_data.keys.each do |rexp|
+ puts "#{self} checking regexp #{rexp.to_s} to attempt match against column.name #{column.name}" if Stepford.debug
+ if rexp ~= column.name
+ puts "#{self} regexp #{rexp.to_s} matched column.name #{column.name}" if Stepford.debug
+ return Stepford.attr_match_test_data[rexp]
+ end
+ end
+
+ result = Stepford.model_datatype_test_data[c.type.to_sym]
+ puts "#{self} using Stepford.datatype_test_data for type #{column.type.to_sym.inspect}: #{result}" if Stepford.debug
+ result
+ end
+end
26 lib/stepford/options.rb
@@ -0,0 +1,26 @@
+module Stepford
+ class << self
+ attr_accessor :debug, :attr_match_test_data, :datatype_test_data
+ def configure(&blk); class_eval(&blk); end
+ end
+end
+
+Stepford.configure do
+ @debug = true
+ @attr_match_test_data = {
+ /|*.\_at,*.\_on,|/ => Time.now
+ }
+ @datatype_test_data = {
+ string: lambda{|c|generate(:random_string)},
+ integer: lambda{|c|Random.rand(9999)},
+ decimal: lambda{|c|Random.rand(9999.9)},
+ datetime: Time.now,
+ timestamp: Time.now,
+ time: Time.now,
+ date: Time.now,
+ binary: Random.bytes(8),
+ boolean: true,
+ xml: lambda{|c|"<test>#{generate(:random_string)}</test>"},
+ ts_vector: nil
+ }
+end
0  lib/stepford/railtie.rb
No changes.
3  lib/stepford/version.rb
@@ -0,0 +1,3 @@
+module Stepford
+ VERSION = '0.0.1'
+end
17 stepford.gemspec
@@ -0,0 +1,17 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "stepford/version"
+
+Gem::Specification.new do |s|
+ s.name = 'stepford'
+ s.version = Stepford::VERSION
+ s.authors = ['Gary S. Weaver']
+ s.email = ['garysweaver@gmail.com']
+ s.homepage = 'https://github.com/garysweaver/stepford'
+ s.summary = %q{The land of Stepford comes to your Ruby testing.}
+ s.description = %q{See the README. It's more up-to-date.}
+ s.files = Dir['lib/**/*'] + ['Rakefile', 'README.md']
+ s.license = 'MIT'
+ s.add_runtime_dependency 'activerecord'
+ s.add_runtime_dependency 'factory_girl'
+end

0 comments on commit 04e86c9

Please sign in to comment.
Something went wrong with that request. Please try again.