fixture_to_factory 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7e85f9085537b219a8ecf40c45db1a2959c8ef99
4
+ data.tar.gz: a5800dd7eda1f30f9da27591cbb3a18cf9a281c4
5
+ SHA512:
6
+ metadata.gz: cdf7a6365ad37a08c95ba2533c8fd3ab9abf5d9b61a48721d07547061a954fb365a7d3e730cc9aafb340c8d6362f37295b1e51e509820967f3950b75dc9adc51
7
+ data.tar.gz: e167073205bdd2a3ba875183e23698ab7e4c4e5937881d0e78bfd00649cfd8c0092ef9b42f87fea3f8ab3ae07dcb87321f25f7f8fe6caef5f8c7c947552c8e7a
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ Gemfile.lock
30
+ .ruby-version
31
+ .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,5 @@
1
+ --color
2
+ --profile
3
+ --format documentation
4
+ --backtrace
5
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fixture_to_factory.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 SixiS
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Matthew Hirst
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ # FixtureToFactory
2
+
3
+ Quick and dirty conversion from fixtures to factory girl factories.
4
+ It's just to save time if you have a bunch of fixtures you need to make factories out of.
5
+ The conversion is dumb so it just makes basic factories from the fixtures - basic value attributes. You can then copy/paste/alter the dumb factories into real ones!
6
+
7
+ Looks in ./test/fixtures and ./spec/fixtures
8
+
9
+ Output goes into ./test/factories or ./spec/factories
10
+ Output files look like generated_#{fixutre_name}.rb
11
+
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'fixture_to_factory'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install fixture_to_factory
26
+
27
+ ## Usage
28
+
29
+ Navigage to the base of your project directory and then run `fixture_to_factory`.
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ begin
3
+ require 'fixture_to_factory'
4
+ rescue LoadError
5
+ require File.dirname(__FILE__) + '/../lib/fixture_to_factory'
6
+ end
7
+
8
+ options = {}
9
+
10
+ FixtureToFactory.execute(options)
@@ -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 'fixture_to_factory/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fixture_to_factory"
8
+ spec.version = FixtureToFactory::VERSION
9
+ spec.authors = ["Matthew Hirst"]
10
+ spec.email = ["matt@devicemagic.com"]
11
+ spec.description = %q{Fix to factory is for quick and dirty conversion of fixtures in your test or spec suite into factory girl factories.}
12
+ spec.summary = %q{Fix to factory is for quick and dirty conversion of fixtures in your test or spec suite into factory girl factories.}
13
+ spec.homepage = "https://github.com/SixiS/fixture_to_factory"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "pry"
24
+ spec.add_development_dependency "rspec", ">= 2.0.0"
25
+ spec.add_development_dependency "activesupport"
26
+
27
+ end
@@ -0,0 +1,22 @@
1
+ require "active_support"
2
+ require "fixture_to_factory/version"
3
+ require "fixture_to_factory/fixture_file_parser"
4
+ require "fixture_to_factory/factory_writer"
5
+
6
+ require 'pry'
7
+
8
+ module FixtureToFactory
9
+ def self.execute(options)
10
+ ['./test', './spec'].each do |folder|
11
+ prefix = options.delete(:prefix) || ''
12
+ folder = "#{prefix}#{folder}"
13
+
14
+ # This looks in a directory given and returns the fixtures in a hash form
15
+ # filename => [fixture1, fixture2]
16
+ parsed_fixtures = FixtureFileParser.parse_files("#{folder}/fixtures")
17
+
18
+ # Then we pass the hashes to the writer to convert them into factory files
19
+ FactoryWriter.write_files("#{folder}/factories", parsed_fixtures)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,61 @@
1
+ module FixtureToFactory
2
+ class FactoryWriter
3
+ def self.write_files(folder, file_hashes)
4
+ file_hashes.each do |fixture_path, fixtures|
5
+ write_fixtures_to_file(folder, fixture_path, fixtures)
6
+ end
7
+ end
8
+
9
+ private
10
+
11
+ class << self
12
+ def write_fixtures_to_file(folder, fixture_path, fixtures)
13
+ fixture_file_name = fixture_path.split('/').last.split('.')[0..-2].join('.')
14
+ class_name = ActiveSupport::Inflector.singularize(fixture_file_name).capitalize
15
+ factory_file_name = "generated_#{fixture_file_name}.rb"
16
+
17
+ puts "Writing #{fixture_file_name} fixtures to factory file #{factory_file_name}."
18
+
19
+ FileUtils.mkdir_p(folder)
20
+ file = File.new("#{folder}/#{factory_file_name}", 'w')
21
+ file.write(convert_yaml_hashes_to_factory_file(class_name, fixtures))
22
+ end
23
+
24
+ def convert_yaml_hashes_to_factory_file(class_name, fixture_yaml_hash)
25
+ output = "FactoryGirl.define do\n"
26
+ fixture_yaml_hash.each do |fixture, values|
27
+ output += "\n factory :#{fixture}, class: #{class_name}\n"
28
+ values.each do |key, value|
29
+ output += " #{attribute_to_factory_line(key, value)}\n"
30
+ end
31
+ output += " end\n"
32
+ end
33
+ output += "\nend"
34
+ end
35
+
36
+ def attribute_to_factory_line(key, value)
37
+ result = ''
38
+ if key != 'id'
39
+ result = "#{key} { "
40
+ result += value_in_factory_format(value)
41
+ result += " }"
42
+ end
43
+ result
44
+ end
45
+
46
+ def value_in_factory_format(value)
47
+ value_class = value.class
48
+ case value_class.to_s
49
+ when 'TrueClass'
50
+ true
51
+ when 'FalseClass'
52
+ false
53
+ when 'String'
54
+ "%|#{value.gsub('|', '\|')}|"
55
+ else
56
+ "#{value}"
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,36 @@
1
+ require 'yaml'
2
+
3
+ module FixtureToFactory
4
+ class FixtureFileParser
5
+ def self.parse_files(folder)
6
+ file_names = get_file_names_in(folder)
7
+ file_names = file_names.select{|file_name| file_name =~ /(\w+).yml/ }
8
+ file_paths = file_names.map{|file_name| "#{folder}/#{file_name}"}
9
+
10
+ map_fixtures_to_hash(file_paths)
11
+ end
12
+
13
+ private
14
+
15
+ class << self
16
+ def get_file_names_in(folder)
17
+ file_names = []
18
+ if Dir.exists?(folder)
19
+ Dir.foreach(folder) do |file_name|
20
+ file_names << file_name
21
+ end
22
+ end
23
+ file_names
24
+ end
25
+
26
+ def map_fixtures_to_hash(file_paths)
27
+ result = {}
28
+ file_paths.each do |file_path|
29
+ fixtures_to_yaml = YAML.load_file(file_path)
30
+ result[file_path] = fixtures_to_yaml
31
+ end
32
+ result
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module FixtureToFactory
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,18 @@
1
+ FactoryGirl.define do
2
+
3
+ factory :cheese_burger, class: Burger
4
+ name { %|Cheese burger| }
5
+ description { %|Burger of cheese| }
6
+ end
7
+
8
+ factory :hamburger, class: Burger
9
+ name { %|Hamburger| }
10
+ sold { 50 }
11
+ end
12
+
13
+ factory :brie_burger, class: Burger
14
+ name { %|Burger of brie| }
15
+ description { %|Gross cheese and stuff| }
16
+ end
17
+
18
+ end
@@ -0,0 +1,18 @@
1
+ FactoryGirl.define do
2
+
3
+ factory :strawberry, class: Milkshake
4
+ name { %|Strawberry \| milkshake| }
5
+ ingrdients { %|Strawberries I think| }
6
+ end
7
+
8
+ factory :chocolage, class: Milkshake
9
+ name { %|Chocolate milkshake| }
10
+ organization { %|LOTS OF CHOCS| }
11
+ end
12
+
13
+ factory :meat, class: Milkshake
14
+ name { %|MAN SHAKE| }
15
+ organization { %|Steak and babies| }
16
+ end
17
+
18
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+ describe FixtureToFactory do
3
+ describe '.execute' do
4
+ let(:execute){FixtureToFactory.execute(prefix: '../')}
5
+ before(:each) do
6
+ FileUtils.rm_rf('./spec/factories/')
7
+ end
8
+
9
+ it{execute; expect(Dir.exists?('./spec/factories/generated_burgers.rb'))}
10
+ it{execute; expect(Dir.exists?('./spec/factories/generated_milkshakes.rb'))}
11
+
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ cheese_burger:
2
+ name: 'Cheese burger'
3
+ description: 'Burger of cheese'
4
+ hamburger:
5
+ name: 'Hamburger'
6
+ sold: 50
7
+ brie_burger:
8
+ name: 'Burger of brie'
9
+ description: 'Gross cheese and stuff'
@@ -0,0 +1,9 @@
1
+ strawberry:
2
+ name: 'Strawberry | milkshake'
3
+ ingrdients: 'Strawberries I think'
4
+ chocolage:
5
+ name: 'Chocolate milkshake'
6
+ organization: 'LOTS OF CHOCS'
7
+ meat:
8
+ name: 'MAN SHAKE'
9
+ organization: 'Steak and babies'
@@ -0,0 +1,11 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+
7
+ require 'fixture_to_factory'
8
+
9
+ RSpec.configure do |config|
10
+ # some (optional) config here
11
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fixture_to_factory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Hirst
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 2.0.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 2.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Fix to factory is for quick and dirty conversion of fixtures in your
84
+ test or spec suite into factory girl factories.
85
+ email:
86
+ - matt@devicemagic.com
87
+ executables:
88
+ - fixture_to_factory
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - .gitignore
93
+ - .rspec
94
+ - .ruby-version
95
+ - Gemfile
96
+ - LICENSE
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - bin/fixture_to_factory
101
+ - fixture_to_factory.gemspec
102
+ - lib/fixture_to_factory.rb
103
+ - lib/fixture_to_factory/factory_writer.rb
104
+ - lib/fixture_to_factory/fixture_file_parser.rb
105
+ - lib/fixture_to_factory/version.rb
106
+ - spec/factories/generated_burgers.rb
107
+ - spec/factories/generated_milkshakes.rb
108
+ - spec/fixture_to_factory_spec.rb
109
+ - spec/fixtures/burgers.yml
110
+ - spec/fixtures/milkshakes.yml
111
+ - spec/spec_helper.rb
112
+ homepage: https://github.com/SixiS/fixture_to_factory
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.0.14
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Fix to factory is for quick and dirty conversion of fixtures in your test
136
+ or spec suite into factory girl factories.
137
+ test_files:
138
+ - spec/factories/generated_burgers.rb
139
+ - spec/factories/generated_milkshakes.rb
140
+ - spec/fixture_to_factory_spec.rb
141
+ - spec/fixtures/burgers.yml
142
+ - spec/fixtures/milkshakes.yml
143
+ - spec/spec_helper.rb