industrial_girl 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'industrial_girl/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "industrial_girl"
8
+ spec.version = IndustrialGirl::VERSION
9
+ spec.authors = ["Dan Bickford"]
10
+ spec.email = ["danbickford007@yahoo.com"]
11
+ spec.description = %q{Factory Girl factory creater}
12
+ spec.summary = %q{Industrial Girl creates your factories quickly for Factory Girl, either
13
+ in a single file or multiple file while using faker}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_dependency "faker"
26
+ spec.add_dependency "rails"
27
+ end
data/lib/database.rb ADDED
@@ -0,0 +1,25 @@
1
+ class Database
2
+
3
+ attr_accessor :rails_root, :generator
4
+
5
+ def initialize(rails_root, single, force)
6
+ @single = single
7
+ @rails_root = rails_root
8
+ @generator = Generator.new(@rails_root, single, force)
9
+ end
10
+
11
+ def load
12
+ file = File.join(@rails_root, 'config', 'database.yml')
13
+ yaml = YAML.load_file(file)
14
+ connect(yaml)
15
+ end
16
+
17
+ def connect(yaml)
18
+ host = yaml['development']['host']
19
+ host ||= 'localhost'
20
+ ActiveRecord::Base.establish_connection(:adapter => "mysql2",
21
+ :database => yaml['development']['database'])
22
+ @generator.create_factories
23
+ end
24
+
25
+ end
data/lib/generator.rb ADDED
@@ -0,0 +1,82 @@
1
+ class Generator
2
+
3
+ attr_accessor :rails_root, :single, :force
4
+
5
+ def initialize(rails_root, single, force)
6
+ @rails_root = rails_root
7
+ @single = single
8
+ @force = force
9
+ end
10
+
11
+ def create_factories
12
+ @rails_root = rails_root
13
+ if !@single
14
+ create_multiple
15
+ else
16
+ create_single
17
+ end
18
+ end
19
+
20
+ def file?(path)
21
+ File.exists?("#{@rails_root}/#{path}")
22
+ end
23
+
24
+ def directory?(path)
25
+ File.directory?("#{@rails_root}/#{path}")
26
+ end
27
+
28
+ def to_boolean(str)
29
+ str == "true"
30
+ end
31
+
32
+ def create_single
33
+ library = Library.new
34
+ return if file?("spec/factories.rb") and !@force
35
+ file = File.join(@rails_root, 'spec', 'factories.rb')
36
+ File.open(file, 'w') do |f|
37
+ f.puts "require 'factory_girl'"
38
+ f.puts "\n"
39
+ f.puts "FactoryGirl.define do"
40
+ ActiveRecord::Base.connection.tables.each do |table_name|
41
+ next if table_name == 'schema_migrations'
42
+ p "Creating factory #{table_name}"
43
+ f.puts "\n"
44
+ f.puts " factory :#{table_name} do"
45
+ ActiveRecord::Base.connection.columns(table_name).each do |c|
46
+ f.puts " #{c.name} #{library.check(c.name, c.type, c.limit)}"
47
+ end
48
+ f.puts " end"
49
+ end
50
+ f.puts "\n"
51
+ f.puts "end"
52
+ end
53
+ end
54
+
55
+ def create_multiple
56
+ if !directory?("spec/factories")
57
+ FileUtils.mkdir_p("#{@rails_root}/spec/factories")
58
+ end
59
+ library = Library.new
60
+ ActiveRecord::Base.connection.tables.each do |table_name|
61
+ next if table_name == 'schema_migrations'
62
+ next if file?("spec/factories/#{table_name}.rb") and !@force
63
+ file = File.join(@rails_root, 'spec', 'factories', "#{table_name}.rb")
64
+ File.open(file, 'w') do |f|
65
+ f.puts "require 'factory_girl'"
66
+ f.puts "\n"
67
+ f.puts "FactoryGirl.define do"
68
+ p "Creating factory #{table_name}"
69
+ f.puts "\n"
70
+ f.puts " factory :#{table_name} do"
71
+ ActiveRecord::Base.connection.columns(table_name).each do |c|
72
+ f.puts " #{c.name} #{library.check(c.name, c.type, c.limit)}"
73
+ end
74
+ f.puts " end"
75
+ f.puts "\n"
76
+ f.puts "end"
77
+ end
78
+ end
79
+
80
+ end
81
+
82
+ end
@@ -0,0 +1,3 @@
1
+ module IndustrialGirl
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ require "industrial_girl/version"
2
+ require "root"
3
+ require "rubygems"
4
+ require "active_record"
5
+ require "library"
6
+ require "generator"
7
+ require "database"
8
+ require "faker"
9
+ require 'fileutils'
10
+ module IndustrialGirl
11
+ end
data/lib/library.rb ADDED
@@ -0,0 +1,43 @@
1
+ class Library
2
+
3
+ attr_accessor :column_name
4
+
5
+ def check(column_name, type, limit)
6
+ @column_name = column_name
7
+ call = case
8
+ when type == :string then :name
9
+ when type == :integer then :int
10
+ when type == :datetime then :date
11
+ else :name
12
+ end
13
+ self.send(call)
14
+ end
15
+
16
+ def name
17
+ case
18
+ when @column_name.include?('name') then "'#{Faker::Name.name}'"
19
+ when @column_name.include?('mail') then "'#{Faker::Internet.email}'"
20
+ when @column_name.include?('address') then "'#{Faker::Address.street_address}'"
21
+ when @column_name.include?('city') then "'#{Faker::Address.city}'"
22
+ when @column_name.include?('state') then "'#{Faker::Address.state_abbr}'"
23
+ when @column_name.include?('zip') then "'#{Faker::Address.zip_code}'"
24
+ else "'#{Faker::Company.catch_phrase}'"
25
+ end
26
+
27
+ end
28
+
29
+ def date
30
+ num = Random.new.rand(1..10)
31
+ "{ #{num}.days.ago }"
32
+ end
33
+
34
+ def int
35
+ if @column_name.include?('zip')
36
+ "'#{Faker::Address.zip_code}'"
37
+ else
38
+ Random.new.rand(0..1000)
39
+ end
40
+ end
41
+
42
+
43
+ end
data/lib/root.rb ADDED
@@ -0,0 +1,24 @@
1
+ class Root
2
+
3
+ attr_accessor :rails_root, :db
4
+
5
+ def initialize(single, force)
6
+ find_rails_root(single, force)
7
+ end
8
+
9
+ def find_rails_root(single, force)
10
+ @rails_root = `pwd`.sub(/\n/, '')
11
+ if File.directory? "#{@rails_root}/config" and File.directory? "#{@rails_root}/app"
12
+ @db = Database.new(@rails_root, single, force)
13
+ database
14
+ else
15
+ p 'Either this is not a rails app or you are not in the root of your rails app'
16
+ end
17
+ end
18
+
19
+ def database
20
+ @db.load
21
+ end
22
+
23
+
24
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe Database do
4
+
5
+ let(:root) { `pwd`.sub(/\n/, '') }
6
+ let(:db) { Database.new(root) }
7
+
8
+ describe '#load' do
9
+
10
+ context 'loading config/database.yml from rails app' do
11
+
12
+ it 'should call join on file for yaml retreival' do
13
+ File.should_receive(:join)
14
+ YAML.should_receive(:load_file)
15
+ Database.any_instance.should_receive(:connect)
16
+ db.load
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+
23
+ describe '#connect' do
24
+
25
+ context 'establishing connection' do
26
+
27
+ it 'should call establish connection on active record with proper args' do
28
+ file = File.join(root, 'spec', 'database.yml')
29
+ yaml = YAML.load_file(file)
30
+ ActiveRecord::Base.should_receive(:establish_connection)
31
+ .with({:adapter=>"mysql2", :database=>yaml['development']['database']})
32
+ Generator.stub(:create_factories)
33
+ db.connect(yaml)
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+
40
+ end
data/spec/database.yml ADDED
@@ -0,0 +1,42 @@
1
+ # MySQL. Versions 4.1 and 5.0 are recommended.
2
+ #
3
+ # Install the MYSQL driver
4
+ # gem install mysql2
5
+ #
6
+ # Ensure the MySQL gem is defined in your Gemfile
7
+ # gem 'mysql2'
8
+ #
9
+ # And be sure to use new-style password hashing:
10
+ # http://dev.mysql.com/doc/refman/5.0/en/old-client.html
11
+ development:
12
+ adapter: mysql2
13
+ encoding: utf8
14
+ reconnect: false
15
+ database: gem_tester_development
16
+ pool: 5
17
+ username: root
18
+ password:
19
+ socket: /tmp/mysql.sock
20
+
21
+ # Warning: The database defined as "test" will be erased and
22
+ # re-generated from your development database when you run "rake".
23
+ # Do not set this db to the same as development or production.
24
+ test:
25
+ adapter: mysql2
26
+ encoding: utf8
27
+ reconnect: false
28
+ database: gem_tester_test
29
+ pool: 5
30
+ username: root
31
+ password:
32
+ socket: /tmp/mysql.sock
33
+
34
+ production:
35
+ adapter: mysql2
36
+ encoding: utf8
37
+ reconnect: false
38
+ database: gem_tester_production
39
+ pool: 5
40
+ username: root
41
+ password:
42
+ socket: /tmp/mysql.sock
data/spec/factories.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'factory_girl'
2
+
3
+ FactoryGirl.define do
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ describe Library do
4
+
5
+ let(:library) { Library.new }
6
+
7
+ describe '#check' do
8
+
9
+ it 'should determine data type of string' do
10
+ Library.any_instance.should_receive(:send).with(:name)
11
+ library.check('test', :string, 'tester')
12
+ end
13
+
14
+ it 'should determine data type of int' do
15
+ Library.any_instance.should_receive(:send).with(:int)
16
+ library.check('test', :integer, 'tester')
17
+ end
18
+
19
+ it 'should determine data type of date' do
20
+ Library.any_instance.should_receive(:send).with(:date)
21
+ library.check('test', :datetime, 'tester')
22
+ end
23
+
24
+ end
25
+
26
+ describe "#name" do
27
+
28
+ context 'determining which faker to use' do
29
+
30
+ it 'should use faker name if includes name' do
31
+ library.column_name = 'username'
32
+ Faker::Name.should_receive(:name)
33
+ library.name
34
+ end
35
+
36
+ it 'should use faker email if includes mail' do
37
+ library.column_name = 'theemail'
38
+ Faker::Internet.should_receive(:email)
39
+ library.name
40
+ end
41
+
42
+ it 'should use faker address if includes address' do
43
+ library.column_name = 'theaddress'
44
+ Faker::Address.should_receive(:street_address)
45
+ library.name
46
+ end
47
+
48
+ it 'should use faker city if includes city' do
49
+ library.column_name = 'thecity'
50
+ Faker::Address.should_receive(:city)
51
+ library.name
52
+ end
53
+
54
+ it 'should use faker state if includes state' do
55
+ library.column_name = 'thestate'
56
+ Faker::Address.should_receive(:state_abbr)
57
+ library.name
58
+ end
59
+
60
+ it 'should use faker zip if includes zip' do
61
+ library.column_name = 'thezip'
62
+ Faker::Address.should_receive(:zip_code)
63
+ library.name
64
+ end
65
+
66
+ it 'should use faker company catch pharse if no match found' do
67
+ library.column_name = 'xxxxxxx'
68
+ Faker::Company.should_receive(:catch_phrase)
69
+ library.name
70
+ end
71
+
72
+ end
73
+
74
+ end
75
+
76
+ end
data/spec/root_spec.rb ADDED
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe Root do
4
+
5
+ let(:root) { Root.new(true, true) }
6
+ let(:pwd) { `pwd`.sub(/\n/, '') }
7
+
8
+ describe '#rails_root' do
9
+
10
+ context 'determining the root of rails project' do
11
+
12
+ it 'should find the pwd' do
13
+ root.find_rails_root(true, true)
14
+ root.rails_root.should == pwd
15
+ end
16
+
17
+ end
18
+
19
+ context 'determining if rails project and at root' do
20
+
21
+ it 'should call database if rails app' do
22
+ File.stub(:directory?).with("#{pwd}/config").and_return(true)
23
+ File.stub(:directory?).with("#{pwd}/app").and_return(true)
24
+ Root.any_instance.stub(:database)
25
+ root.should_not_receive(:p)
26
+ root.find_rails_root(true, true)
27
+ end
28
+
29
+ it 'should warn if not a rails app' do
30
+ root.should_receive(:p)
31
+ root.find_rails_root(true, true)
32
+ end
33
+
34
+ end
35
+
36
+ end
37
+
38
+ describe "#database" do
39
+
40
+ context "calling database class to load db info" do
41
+
42
+ it 'should instaniate database class and call load' do
43
+ root.db = Database.new(pwd, true, true)
44
+ Database.any_instance.should_receive(:load)
45
+ root.database
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+
52
+ end
@@ -0,0 +1,6 @@
1
+ require 'root'
2
+ require 'database'
3
+ require 'active_record'
4
+ require 'generator'
5
+ require 'library'
6
+ require 'faker'
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: industrial_girl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dan Bickford
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
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: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
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
+ - !ruby/object:Gem::Dependency
63
+ name: faker
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rails
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Factory Girl factory creater
95
+ email:
96
+ - danbickford007@yahoo.com
97
+ executables:
98
+ - industrial_girl
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - .gitignore
103
+ - .travis.yml
104
+ - Gemfile
105
+ - LICENSE.txt
106
+ - README.md
107
+ - Rakefile
108
+ - bin/industrial_girl
109
+ - cert_file
110
+ - industrial_girl.gemspec
111
+ - lib/database.rb
112
+ - lib/generator.rb
113
+ - lib/industrial_girl.rb
114
+ - lib/industrial_girl/version.rb
115
+ - lib/library.rb
116
+ - lib/root.rb
117
+ - spec/database.spec
118
+ - spec/database.yml
119
+ - spec/factories.rb
120
+ - spec/library_spec.rb
121
+ - spec/root_spec.rb
122
+ - spec/spec_helper.rb
123
+ homepage: ''
124
+ licenses:
125
+ - MIT
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ! '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 1.8.25
145
+ signing_key:
146
+ specification_version: 3
147
+ summary: Industrial Girl creates your factories quickly for Factory Girl, either in
148
+ a single file or multiple file while using faker
149
+ test_files:
150
+ - spec/database.spec
151
+ - spec/database.yml
152
+ - spec/factories.rb
153
+ - spec/library_spec.rb
154
+ - spec/root_spec.rb
155
+ - spec/spec_helper.rb