factory_bot-scoped_sequence 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: 81373b1c8f2574079f8667f95a280550764a611e678ff4bd235f2c77103d106f
4
+ data.tar.gz: e7a8364fd015eb7bf78d7f7f751d173cd32cb04b11c95ad6f2700514765c97c1
5
+ SHA512:
6
+ metadata.gz: 738e51e723ef701df94b850708d59b2061463b61e0a87a0ca276371fa1c6a3d49a3076a820278f97a4f26cb57ed181f99294961016b3b90600031e440e0b23bb
7
+ data.tar.gz: 7a1d0040b943fc805425b933763185463e989de962691022751bb63f1606c83e8c524317a5b10756755b2e52a0f37de3c5031ded4fdf62eba6d56197fa9e3d1e
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/factory_bot/scoped_sequence/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "factory_bot-scoped_sequence"
7
+ spec.version = FactoryBot::ScopedSequence::VERSION
8
+ spec.authors = ["kakubin"]
9
+ spec.email = ["wetsand.wfs@gmail.com"]
10
+
11
+ spec.summary = "Extension to add scope to factory_bot's sequence"
12
+ spec.description = "Extension to add scope to factory_bot's sequence"
13
+ spec.homepage = "https://github.com/kakubin/factory_bot-scoped_sequence"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 3.0.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = spec.homepage
21
+
22
+ spec.files = Dir.glob("lib/**/*") + [File.basename(__FILE__)]
23
+ spec.require_paths = ["lib"]
24
+
25
+ # Uncomment to register a new dependency of your gem
26
+ spec.add_dependency "factory_bot"
27
+
28
+ # For more information and examples about making a new gem, check out our
29
+ # guide at: https://bundler.io/guides/creating_gem.html
30
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "factory_bot"
4
+
5
+ module FactoryBot
6
+ class ScopedSequence
7
+ module Extension
8
+ module DefinitionProxyExtension
9
+ def sequence(name, *args, **options, &block)
10
+ if (scope_name = options.delete(:scope))
11
+ sequence = ::FactoryBot::ScopedSequence.new(name, scope_name, *args, **options, &block)
12
+ add_attribute(name) { increment_scoped_sequence(sequence) }
13
+ else
14
+ super
15
+ end
16
+ end
17
+
18
+ ::FactoryBot::DefinitionProxy.prepend self
19
+ end
20
+
21
+ module EvaluatorExtension
22
+ def increment_scoped_sequence(sequence)
23
+ scope_value = send(sequence.scope_name)
24
+ sequence.next(scope_value, self)
25
+ end
26
+
27
+ ::FactoryBot::Evaluator.include self
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FactoryBot
4
+ class ScopedSequence
5
+ class ScopedEnumeratorAdapter
6
+ attr_reader :scope_value
7
+
8
+ delegate :peek, :next, :rewind, to: :@value
9
+
10
+ def initialize(scope_value, value)
11
+ @scope_value = scope_value
12
+ @value =
13
+ if value.respond_to?(:peek)
14
+ value
15
+ else
16
+ FactoryBot::Sequence::EnumeratorAdapter.new(value)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FactoryBot
4
+ class ScopedSequence
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "scoped_sequence/version"
4
+ require_relative "scoped_sequence/extension"
5
+ require_relative "scoped_sequence/scoped_enumerator_adapter"
6
+
7
+ module FactoryBot
8
+ class ScopedSequence
9
+ attr_reader :name, :scope_name
10
+
11
+ def initialize(name, scope_name, *args, &proc)
12
+ @name = name
13
+ @scope_name = scope_name
14
+ @proc = proc
15
+
16
+ options = args.extract_options!
17
+ @default_value = args.first || 1
18
+ @aliases = options.fetch(:aliases) { [] }
19
+ @scopes = []
20
+ end
21
+
22
+ def next(scope_value, scope = nil)
23
+ value = value(scope_value)
24
+ if @proc && scope
25
+ scope.instance_exec(value, &@proc)
26
+ elsif @proc
27
+ @proc.call(value)
28
+ else
29
+ value
30
+ end
31
+ ensure
32
+ increment_value(scope_value)
33
+ end
34
+
35
+ def names
36
+ [@name] + @aliases
37
+ end
38
+
39
+ def rewind
40
+ @scopes.each(&:rewind)
41
+ end
42
+
43
+ private
44
+
45
+ def value_by_scope(scope_value)
46
+ @scopes.find { |scope| scope.scope_value == scope_value } || initialized_scope(scope_value)
47
+ end
48
+
49
+ def initialized_scope(scope_value)
50
+ @scopes << new_scope = ScopedEnumeratorAdapter.new(scope_value, @default_value.dup)
51
+ new_scope
52
+ end
53
+
54
+ def value(scope_value)
55
+ value_by_scope(scope_value).peek
56
+ end
57
+
58
+ def increment_value(scope_value)
59
+ value_by_scope(scope_value).next
60
+ end
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: factory_bot-scoped_sequence
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kakubin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-06-10 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Extension to add scope to factory_bot's sequence
28
+ email:
29
+ - wetsand.wfs@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - factory_bot-scoped_sequence.gemspec
35
+ - lib/factory_bot/scoped_sequence.rb
36
+ - lib/factory_bot/scoped_sequence/extension.rb
37
+ - lib/factory_bot/scoped_sequence/scoped_enumerator_adapter.rb
38
+ - lib/factory_bot/scoped_sequence/version.rb
39
+ homepage: https://github.com/kakubin/factory_bot-scoped_sequence
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ allowed_push_host: https://rubygems.org
44
+ homepage_uri: https://github.com/kakubin/factory_bot-scoped_sequence
45
+ source_code_uri: https://github.com/kakubin/factory_bot-scoped_sequence
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.4.19
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Extension to add scope to factory_bot's sequence
65
+ test_files: []