factory_bot_variants 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
+ SHA256:
3
+ metadata.gz: '0768f2e9df6a8788bb8a2ef27fb3fdc064b8e35addeabba424dde8fd3fe56ca7'
4
+ data.tar.gz: b50b24636ae73f91d8f1c8a8585a57de34f81c223f1d7ae71c7d4c94a86fe23c
5
+ SHA512:
6
+ metadata.gz: 728ff7c516e59fc5bb54381890fb567c4d2c59a6ba47c2ce199c5ae3763b0f2a917e973872042e1d61fedb9a21224b8ae494db967bc9a7efcf138b0efb75fed7
7
+ data.tar.gz: 724f1d76b2a24776e46708b16e068a115e2b0e221a513322ef06cc81b090296a4b190d8ae4f4b798aaade49749d10d8a9ef76e7d56619b24f4158f18e97a8b5e
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ spec/examples.txt
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 1.16.5
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,50 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ factory_bot_variants (0.1.0)
5
+ factory_bot (~> 4.8)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (5.2.1)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 0.7, < 2)
13
+ minitest (~> 5.1)
14
+ tzinfo (~> 1.1)
15
+ concurrent-ruby (1.0.5)
16
+ diff-lcs (1.3)
17
+ factory_bot (4.11.1)
18
+ activesupport (>= 3.0.0)
19
+ i18n (1.1.0)
20
+ concurrent-ruby (~> 1.0)
21
+ minitest (5.11.3)
22
+ rake (10.5.0)
23
+ rspec (3.8.0)
24
+ rspec-core (~> 3.8.0)
25
+ rspec-expectations (~> 3.8.0)
26
+ rspec-mocks (~> 3.8.0)
27
+ rspec-core (3.8.0)
28
+ rspec-support (~> 3.8.0)
29
+ rspec-expectations (3.8.1)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.8.0)
32
+ rspec-mocks (3.8.0)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.8.0)
35
+ rspec-support (3.8.0)
36
+ thread_safe (0.3.6)
37
+ tzinfo (1.2.5)
38
+ thread_safe (~> 0.1)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ bundler (~> 1.16)
45
+ factory_bot_variants!
46
+ rake (~> 10.0)
47
+ rspec (~> 3.8)
48
+
49
+ BUNDLED WITH
50
+ 1.16.5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Oliver Peate
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # FactoryBotVariants
2
+
3
+ In tests, particularly when asserting the order of entities, it's common to
4
+ create several objects which vary by only one or two attributes:
5
+
6
+ ```ruby
7
+ it "sorts dogs oldest to youngest" do
8
+ dog_1 = build(:dog, name: "Fido", age: 5)
9
+ dog_2 = build(:dog, name: "Benji", age: 10)
10
+ dog_3 = build(:dog, name: "Spot", age: 1)
11
+
12
+ expect(DogSorter.new.sort).to eq [dog_2, dog_1, dog_3]
13
+ end
14
+ ```
15
+
16
+ With `FactoryBotVariants` you could write this as:
17
+
18
+ ```ruby
19
+ dog_1, dog_2, dog_3 = build_variants(:dog, names: ["Fido", "Benji", "Spot"], ages: [5, 10, 1])
20
+ ```
21
+
22
+ You can specify attribute names in plural if you like; `names` will become
23
+ `name` as the attributes are passed to your factory.
24
+
25
+ If you have common attributes to assign to each variant, use the `:all` key:
26
+
27
+ ```ruby
28
+ build_variants(:dog, names: ["Brutus", "Avril"], all: { cute: false })
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ In your gemfile:
34
+
35
+ ```ruby
36
+ group :test do
37
+ gem "factory_bot_variants"
38
+ end
39
+ ```
40
+
41
+ Then `bundle install` and include the DSL where you'd like to use it:
42
+
43
+ ### In specific tests
44
+
45
+ ```ruby
46
+ require "factory_bot_variants/dsl"
47
+
48
+ RSpec.describe Dog do
49
+ include FactoryBotVariants::DSL
50
+
51
+ # Your spec
52
+ end
53
+ ```
54
+
55
+ ### In all specs
56
+
57
+ ```ruby
58
+ require "factory_bot_variants/dsl"
59
+
60
+ RSpec.configure do |config|
61
+ config.include FactoryBotVariants::DSL
62
+ end
63
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "factory_bot_variants"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "factory_bot_variants/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "factory_bot_variants"
8
+ spec.version = FactoryBotVariants::VERSION
9
+ spec.authors = ["Oliver Peate"]
10
+
11
+ spec.summary = ""
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "factory_bot", "~> 4.8"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.8"
29
+ end
@@ -0,0 +1,4 @@
1
+ require "factory_bot_variants/version"
2
+
3
+ module FactoryBotVariants
4
+ end
@@ -0,0 +1,24 @@
1
+ require "factory_bot_variants/variant_attributes"
2
+
3
+ module FactoryBotVariants
4
+ module DSL
5
+ STRATEGIES = [
6
+ :build,
7
+ :create,
8
+ :attributes_for,
9
+ :build_stubbed
10
+ ].freeze
11
+
12
+ STRATEGIES.each do |strategy|
13
+ define_method("#{strategy}_variants") do |factory_name, *options|
14
+ attributes = options.extract_options!
15
+
16
+ VariantAttributes.map(attributes).map do |variant_attributes|
17
+ ::FactoryBot.public_send(
18
+ strategy, factory_name, *options, variant_attributes
19
+ )
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ module FactoryBotVariants
2
+ class VariantAttributes
3
+ def self.map(attributes)
4
+ variants = []
5
+
6
+ common_attributes = attributes.delete(:all)
7
+
8
+ attributes.each do |attr_name, attr_values|
9
+ singular_attr_name = attr_name.to_s.singularize.to_sym
10
+
11
+ attr_values.each_with_index do |attr_value, index|
12
+ variants[index] ||= {}
13
+ variants[index][singular_attr_name] = attr_value
14
+ end
15
+ end
16
+
17
+ if common_attributes
18
+ variants.each { |variant| variant.merge!(common_attributes) }
19
+ end
20
+
21
+ variants
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module FactoryBotVariants
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: factory_bot_variants
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Oliver Peate
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: factory_bot
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.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: '3.8'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.8'
69
+ description:
70
+ email:
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".gitignore"
76
+ - ".rspec"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - factory_bot_variants.gemspec
86
+ - lib/factory_bot_variants.rb
87
+ - lib/factory_bot_variants/dsl.rb
88
+ - lib/factory_bot_variants/variant_attributes.rb
89
+ - lib/factory_bot_variants/version.rb
90
+ homepage: ''
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.7.7
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: ''
114
+ test_files: []