stepford 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ Stepford
2
+ =====
3
+
4
+ ### Setup
5
+
6
+ In your Rails 3+ project, add this to your Gemfile:
7
+
8
+ gem 'stepford', :git => 'git://github.com/garysweaver/stepford.git'
9
+
10
+ Then run:
11
+
12
+ bundle install
13
+
14
+ ### Usage
15
+
16
+ #### Factory Girl
17
+
18
+ To automatically generate factories for [Factory Girl][factory_girl] from models, type this at command-line:
19
+
20
+ bundle exec stepford factories
21
+
22
+ That will create a `test/factories` directory and put a `some_model.rb` for each model into it with a starter FactoryGirl factory definition that may or may not work for you.
23
+
24
+ Or, to generate a single file with all factories in `spec/factories.rb`, you'd use:
25
+
26
+ bundle exec stepford factories --single --path spec
27
+
28
+ Or it will figure it out yourself that you want a single file if the path ends in `.rb`:
29
+
30
+ bundle exec stepford factories --path spec/support/factories.rb
31
+
32
+ ### License
33
+
34
+ Copyright (c) 2012 Gary S. Weaver, released under the [MIT license][lic].
35
+
36
+ [factory_girl]: https://github.com/thoughtbot/factory_girl/
37
+ [lic]: http://github.com/garysweaver/stepford/blob/master/LICENSE
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'rake/testtask'
2
+
3
+ #http://nicksda.apotomo.de/2010/10/testing-your-rails-3-engine-sitting-in-a-gem/
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList['test/**/*_test.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ desc "Run tests"
11
+ task :default => :test
data/bin/stepford ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'stepford'
3
+ require 'stepford/cli'
@@ -0,0 +1,18 @@
1
+ require 'thor'
2
+
3
+ module Stepford
4
+ class CLI < Thor
5
+ desc "factories", "create FactoryGirl factories"
6
+ method_option :single, :desc => "Put all factories into a single file"
7
+ method_option :path, :desc => "Pathname of file to contain factories or path of directory to contain factory/factories"
8
+ def factories()
9
+ # load Rails environment
10
+ require './config/environment'
11
+ # load FactoryGirl and generate factories
12
+ require 'stepford/factory_girl'
13
+ exit Stepford::FactoryGirl.generate_factories(options) ? 0 : 1
14
+ end
15
+ end
16
+ end
17
+
18
+ Stepford::CLI.start
@@ -0,0 +1,28 @@
1
+ module Stepford
2
+ class Common
3
+ def self.value_for(column_name, type)
4
+ case type
5
+ when :string
6
+ "'Test #{column_name.titleize}'"
7
+ when :integer
8
+ '123'
9
+ when :decimal
10
+ '1.23'
11
+ when :datetime
12
+ '{ 2.weeks.ago }'
13
+ when :timestamp
14
+ '{ 2.weeks.ago }'
15
+ when :binary
16
+ '0b010101'
17
+ when :boolean
18
+ 'true'
19
+ when :xml
20
+ '<test>Test #{column_name.titleize}</test>'
21
+ when :ts_vector
22
+ 'nil'
23
+ else
24
+ 'nil'
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,106 @@
1
+ require 'factory_girl'
2
+ require 'stepford/common'
3
+
4
+ module Stepford
5
+ class FactoryGirl
6
+ def self.generate_factories(options={})
7
+ factories = {}
8
+
9
+ expected = {}
10
+ Dir[File.join('app','models','*.rb').to_s].each do |filename|
11
+ model_name = File.basename(filename).sub(/.rb$/, '')
12
+ load File.join('app','models',"#{model_name}.rb")
13
+ model_class = model_name.camelize.constantize
14
+ next unless model_class.ancestors.include?(ActiveRecord::Base)
15
+ factory = (factories[model_name.to_sym] ||= [])
16
+ foreign_keys = []
17
+ model_class.reflections.collect {|a,b| (expected[b.class_name.underscore.to_sym] ||= []) << model_name; foreign_keys << b.foreign_key.to_sym; "association #{b.name.to_sym.inspect}, factory: #{b.class_name.underscore.to_sym.inspect}"}.sort.each {|l|factory << l}
18
+ model_class.columns.collect {|c| "#{c.name.to_sym} #{Stepford::Common.value_for(c.name, c.type)}" unless foreign_keys.include?(c.name.to_sym)}.compact.sort.each {|l|factory << l}
19
+ end
20
+
21
+ failed = false
22
+ model_to_fixes_required = {}
23
+ expected.keys.sort.each do |factory_name|
24
+ unless factories[factory_name.to_sym]
25
+ puts "#{File.join('app','models',"#{factory_name}.rb")} missing. Model(s) with associations to it: #{expected[factory_name].sort.join(', ')}"
26
+ expected[factory_name].each do |model_name|
27
+ (model_to_fixes_required[model_name.to_sym] ||= []) << factory_name.to_sym
28
+ end
29
+ failed = true
30
+ end
31
+ end
32
+ model_to_fixes_required.keys.each do |model_to_fix|
33
+ puts ""
34
+ puts "In #{model_to_fix}:"
35
+ model_to_fixes_required[model_to_fix].each do |fix|
36
+ puts "- comment/remove/fix broken association to #{fix}"
37
+ end
38
+ end
39
+ return false if failed
40
+
41
+ path = File.join('test','factories.rb')
42
+ if options[:path]
43
+ if options[:path].end_with?('.rb')
44
+ path = options[:path]
45
+ else
46
+ if options[:single]
47
+ path = File.join(options[:path],'factories.rb')
48
+ else
49
+ path = options[:path]
50
+ end
51
+ end
52
+ end
53
+
54
+ if path.end_with?('.rb')
55
+ dirpath = File.dirname(path)
56
+ unless File.directory?(dirpath)
57
+ puts "Please create this directory first: #{dirpath}"
58
+ return false
59
+ end
60
+
61
+ File.open(path, "w") do |f|
62
+ f.puts 'require \'factory_girl_rails\''
63
+ f.puts ''
64
+ f.puts 'FactoryGirl.define do'
65
+ f.puts ' '
66
+ factories.keys.sort.each do |factory_name|
67
+ factory = factories[factory_name]
68
+ write_factory(factory_name, factory, f)
69
+ f.puts ' '
70
+ end
71
+ f.puts "end"
72
+ end
73
+ else
74
+ unless File.directory?(path)
75
+ puts "Please create this directory first: #{path}"
76
+ return false
77
+ end
78
+
79
+ factories.keys.sort.each do |factory_name|
80
+ factory = factories[factory_name]
81
+ File.open(File.join(path,"#{factory_name}.rb"), "w") do |f|
82
+ f.puts 'require \'factory_girl_rails\''
83
+ f.puts ''
84
+ f.puts 'FactoryGirl.define do'
85
+ f.puts ' '
86
+ write_factory(factory_name, factory, f)
87
+ f.puts ' '
88
+ f.puts "end"
89
+ end
90
+ end
91
+ end
92
+
93
+ return true
94
+ end
95
+
96
+ private
97
+
98
+ def self.write_factory(factory_name, factory, f)
99
+ f.puts " factory #{factory_name.inspect} do"
100
+ factory.each do |line|
101
+ f.puts " #{line}"
102
+ end
103
+ f.puts " end"
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,3 @@
1
+ module Stepford
2
+ VERSION = '0.0.2'
3
+ end
data/lib/stepford.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'stepford/version'
2
+ require 'stepford/common'
3
+
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stepford
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gary S. Weaver
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: thor
16
+ requirement: !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: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rails
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: factory_girl
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Stepford helps you with your tests. See README.
63
+ email:
64
+ - garysweaver@gmail.com
65
+ executables:
66
+ - stepford
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - lib/stepford/cli.rb
71
+ - lib/stepford/common.rb
72
+ - lib/stepford/factory_girl.rb
73
+ - lib/stepford/version.rb
74
+ - lib/stepford.rb
75
+ - Rakefile
76
+ - README.md
77
+ - bin/stepford
78
+ homepage: https://github.com/garysweaver/stepford
79
+ licenses:
80
+ - MIT
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.24
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: A utility to assist with Ruby tests.
103
+ test_files: []