fix2factory 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 37eb04c5a2174a0e505c651f02ae098a6a750a62
4
+ data.tar.gz: a5a5d6db6af7828814afe080784de19ad53d1472
5
+ SHA512:
6
+ metadata.gz: 007f0270f0bf36a54d56b4403a8539b5fa56e527b977d501bd3876aea5f4661207b154767fa8a6669ebbb0e4539eae7cc50473ac02ce10efbb37992c5e249fbb
7
+ data.tar.gz: 53888b5b8fa191ed52c3e1184b646fa28b91d9b45fe09cc64fe39d8c8fd63598310391d83210a322e821f40df32de69ca3b1140417c9ed19c483ccebf2b82e4c
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ *.swp
24
+ *.swo
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fix2factory.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Teddy
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.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Fix2factory
2
+
3
+ A simple tool for converting Fixtures into Factory Girls
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'fix2factory'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install fix2factory
18
+
19
+ ## Usage
20
+
21
+ Go to your Rails app root directroy, run `fix2factory`, then it works
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/fix2factory/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/fix2factory ADDED
@@ -0,0 +1,23 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+
5
+ begin
6
+ require 'fix2factory'
7
+ rescue LoadError
8
+ require File.dirname(__FILE__) + '/../lib/fix2factory'
9
+ end
10
+
11
+ options = {}
12
+
13
+ OptionParser.new do |opt|
14
+ opt.banner = "Usage: cd /your/app/base/dir; fix2factory [OPTIONS]"
15
+
16
+ opt.on("-m", "--match FILES", "Match file pattern") do |m|
17
+ options[:matching] = m
18
+ end
19
+
20
+ opt.parse!
21
+ end
22
+
23
+ Fix2factory.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 'fix2factory/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fix2factory"
8
+ spec.version = Fix2factory::VERSION
9
+ spec.authors = ["Teddy"]
10
+ spec.email = ["ivan.wang1004@gmail.com"]
11
+ spec.summary = %q{Fixtures to Factory Girl}
12
+ spec.description = %q{A simple tool convert Fixtures into Factory Girl}
13
+ spec.homepage = "https://github.com/teddy1004/fix2factory"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = ['fix2factory']
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake", "~> 10.3"
23
+ spec.add_development_dependency "minitest", "~> 4.7"
24
+
25
+ spec.add_dependency "psych", "~> 2.0"
26
+ spec.add_dependency "activesupport", "~> 4.1"
27
+ end
@@ -0,0 +1,27 @@
1
+ require 'fix2factory/fixture_parser'
2
+ require 'fix2factory/factory_writer'
3
+ require 'fix2factory/fixture_selector'
4
+
5
+ module Fix2factory
6
+
7
+ class Converter
8
+ class << self
9
+ def parse_all!(options)
10
+ matching_options = parse_matching_options(options)
11
+ selector = FixtureSelector.new(matching_options)
12
+ selector.test_fixtures.each do |fixture_file_name|
13
+ parser = FixtureParser.new(fixture_file_name)
14
+ parser.parse_fixture
15
+ fixture_file_name.match(/(\w+)\.yml/)
16
+ FactoryWriter.write(parser.output_buffer, "#{Fix2factory::TEST_FACTORIES}#{$1}.rb")
17
+ end
18
+ end
19
+
20
+ def parse_matching_options(options)
21
+ return {} unless options[:matching]
22
+ {:matching => Regexp.new(options[:matching])}
23
+ end
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,13 @@
1
+ module Fix2factory
2
+
3
+ class FactoryWriter
4
+ class << self
5
+ def write(output_buffer, target_file)
6
+ File.open(target_file, 'a') do |f|
7
+ output_buffer.each { |output| f.puts output }
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,67 @@
1
+ require 'psych'
2
+ require 'active_support/core_ext/string'
3
+
4
+ class FileNotFoundError < IOError; end;
5
+
6
+ module Fix2factory
7
+
8
+ class FixtureParser
9
+ attr_reader :output_buffer, :factory_names, :factory_writer
10
+
11
+ def initialize(fixture_file)
12
+ raise ArgumentError if fixture_file.nil?
13
+ raise FileNotFoundError unless File.exists?(fixture_file)
14
+ @fixture_file = fixture_file
15
+ end
16
+
17
+ def model_name
18
+ @fixture_file.match(/(\w+)\.yml/)
19
+ $1.singularize
20
+ end
21
+
22
+ def parse_fixture
23
+ @current_fixture = ::Psych.load_file(@fixture_file)
24
+ @factory_names = @current_fixture.map { |k, v| k }
25
+ @output_buffer = define_factories
26
+ end
27
+
28
+ def define_factories
29
+ factory_defination = []
30
+
31
+ @factory_names.each do |name|
32
+ factory_defination << "Factory.define do"
33
+ factory_defination << " factory :#{model_name}, class: #{model_name.camelize} do"
34
+ define_attributes_for(name).each do |attr|
35
+ factory_defination << attr
36
+ end
37
+
38
+ factory_defination << " end"
39
+ factory_defination << "end"
40
+ factory_defination << ""
41
+ end
42
+
43
+ factory_defination
44
+ end
45
+
46
+ def define_attributes_for(name)
47
+ attributes = @current_fixture[name].map { |k, v| k }
48
+ # Remove newline which treated as attributes
49
+ attributes = attributes.select { |attr| !attr.nil? }
50
+ attributes.map { |attr| write_factory_attribute(name, attr) }
51
+ end
52
+
53
+ def write_factory_attribute(name, attribute)
54
+ attr_value = @current_fixture[name][attribute]
55
+ # String needs quotation marks in Factory Girl
56
+ # @fixture:
57
+ # one:
58
+ # name: Teddy
59
+ #
60
+ # @factory
61
+ # name 'Teddy'
62
+ attr_value = attr_value.is_a?(Numeric) ? "#{attr_value}" : "'#{attr_value}'"
63
+ " #{attribute} #{attr_value}"
64
+ end
65
+ end
66
+
67
+ end
@@ -0,0 +1,35 @@
1
+ module Fix2factory
2
+ class FixtureSelector
3
+ attr_reader :test_fixtures_dir
4
+
5
+ def initialize(options={})
6
+ @test_fixtures_dir = options[:test_fixtures_dir] || TEST_FIXTURES
7
+ @matching = options[:matching]
8
+ end
9
+
10
+ def test_fixtures
11
+ all_fixtures = fetch_fixtures.select { |f| f =~ /test/ }
12
+ filter(all_fixtures)
13
+ end
14
+
15
+ private
16
+ def fetch_fixtures
17
+ project_fixtures = []
18
+ project_fixtures.concat(fetch_dir_files(self.test_fixtures_dir))
19
+ project_fixtures
20
+ end
21
+
22
+ def fetch_dir_files(dir)
23
+ if Dir[dir].empty?
24
+ []
25
+ else
26
+ Dir[dir]
27
+ end
28
+ end
29
+
30
+ def filter(all_fixtures)
31
+ return all_fixtures.select { |f| f=~ @matching } if @matching
32
+ all_fixtures
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module Fix2factory
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,18 @@
1
+ require "fix2factory/version"
2
+ require "fix2factory/converter"
3
+
4
+ module Fix2factory
5
+
6
+ APP_ROOT = '.'
7
+
8
+ TEST_FIXTURES = "#{APP_ROOT}/test/fixtures/*"
9
+
10
+ TEST_FACTORIES = "#{APP_ROOT}/test/factories/"
11
+
12
+ class << self
13
+ def execute(options)
14
+ Fix2factory::Converter.parse_all!(options)
15
+ end
16
+ end
17
+
18
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fix2factory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Teddy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-16 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '4.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '4.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: psych
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '2.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: '4.1'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '4.1'
83
+ description: A simple tool convert Fixtures into Factory Girl
84
+ email:
85
+ - ivan.wang1004@gmail.com
86
+ executables:
87
+ - fix2factory
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/fix2factory
97
+ - fix2factory.gemspec
98
+ - lib/fix2factory.rb
99
+ - lib/fix2factory/converter.rb
100
+ - lib/fix2factory/factory_writer.rb
101
+ - lib/fix2factory/fixture_parser.rb
102
+ - lib/fix2factory/fixture_selector.rb
103
+ - lib/fix2factory/version.rb
104
+ homepage: https://github.com/teddy1004/fix2factory
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.2.2
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Fixtures to Factory Girl
128
+ test_files: []