rom-factory 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.codeclimate.yml +6 -0
- data/.travis.yml +3 -2
- data/CHANGELOG.md +17 -1
- data/Gemfile +7 -16
- data/benchmarks/basic.rb +4 -4
- data/lib/rom/factory.rb +3 -1
- data/lib/rom/factory/attributes/callable.rb +1 -1
- data/lib/rom/factory/attributes/sequence.rb +6 -3
- data/lib/rom/factory/builder.rb +22 -12
- data/lib/rom/factory/constants.rb +9 -0
- data/lib/rom/factory/dsl.rb +3 -3
- data/lib/rom/factory/factories.rb +159 -126
- data/lib/rom/factory/registry.rb +34 -0
- data/lib/rom/factory/sequences.rb +35 -0
- data/lib/rom/factory/tuple_evaluator.rb +4 -2
- data/lib/rom/factory/version.rb +1 -1
- data/rom-factory.gemspec +0 -1
- metadata +7 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f5b3b45c040487ccf89d27457241ba3b117e51ed1a346e089d0da8f6bb223fb1
|
4
|
+
data.tar.gz: c56f6cac0fd002de3cb4b34e272704086cf4fd196e66e3931c82c9a17fbccfe1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: decd39e777394bdc13022f9ae17c2a340bf325d66950c7487b6ffc60505e4b1097d437fc9e0a8cda287ac2e47aef71145ea5191d26b8d8d6fa6ecb9581e0fdd9
|
7
|
+
data.tar.gz: 2c2d34cfe48bcc3909b3599ea8fa241ae974c4993e021bcfdededbdba5a5c0f62e4a91a393033173ba649fcb74e3ded0458562d426c2b3b948275b1cc5639da6
|
data/.codeclimate.yml
ADDED
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,20 @@
|
|
1
|
-
## 0.
|
1
|
+
## 0.6.0 2018-01-31
|
2
|
+
|
3
|
+
### Added
|
4
|
+
|
5
|
+
* Support for factories with custom struct namespaces (solnic)
|
6
|
+
|
7
|
+
### Changed
|
8
|
+
|
9
|
+
* Accessing a factory which is not defined will result in `FactoryNotDefinedError` exception (GustavoCaso + solnic)
|
10
|
+
|
11
|
+
### Fixed
|
12
|
+
|
13
|
+
* Using dependent attributes with sequences works correctly, ie `f.sequence(:login) { |i, name| "name-#{i}"}` (solnic)
|
14
|
+
|
15
|
+
[Compare v0.5.0...v0.6.0](https://github.com/rom-rb/rom-factory/compare/v0.5.0...v0.6.0)
|
16
|
+
|
17
|
+
## 0.5.0 2017-10-24
|
2
18
|
|
3
19
|
### Added
|
4
20
|
|
data/Gemfile
CHANGED
@@ -3,29 +3,21 @@ source 'https://rubygems.org'
|
|
3
3
|
gemspec
|
4
4
|
|
5
5
|
gem 'rake', '~> 12.0'
|
6
|
-
gem 'simplecov', require: false, platform: :mri
|
7
|
-
gem 'codeclimate-test-reporter', require: false, platform: :mri
|
8
|
-
|
9
6
|
gem 'rspec', '~> 3.0'
|
10
7
|
|
11
8
|
group :test do
|
12
9
|
gem 'rom-sql', '~> 2.1'
|
13
10
|
gem 'inflecto'
|
14
11
|
gem 'pry-byebug', platforms: :mri
|
15
|
-
gem 'pry', platforms:
|
16
|
-
gem 'codeclimate-test-reporter'
|
17
|
-
gem 'simplecov'
|
18
|
-
|
19
|
-
if RUBY_ENGINE == 'rbx'
|
20
|
-
gem 'pg', '~> 0.18.0', platforms: :rbx
|
21
|
-
else
|
22
|
-
gem 'pg', platforms: :mri
|
23
|
-
end
|
12
|
+
gem 'pry', platforms: :jruby
|
13
|
+
gem 'codeclimate-test-reporter'
|
14
|
+
gem 'simplecov'
|
24
15
|
|
25
|
-
gem '
|
16
|
+
gem 'pg', '~> 0.21', platforms: :mri
|
17
|
+
gem 'mysql2', platforms: :mri
|
26
18
|
gem 'jdbc-postgres', platforms: :jruby
|
27
19
|
gem 'jdbc-mysql', platforms: :jruby
|
28
|
-
gem 'sqlite3', '~> 1.3', platforms:
|
20
|
+
gem 'sqlite3', '~> 1.3', platforms: :mri
|
29
21
|
gem 'jdbc-sqlite3', platforms: :jruby
|
30
22
|
end
|
31
23
|
|
@@ -37,7 +29,6 @@ end
|
|
37
29
|
group :benchmarks do
|
38
30
|
gem 'activerecord'
|
39
31
|
gem 'benchmark-ips'
|
40
|
-
gem '
|
32
|
+
gem 'factory_bot'
|
41
33
|
gem 'fabrication'
|
42
|
-
gem 'pg', platforms: :mri
|
43
34
|
end
|
data/benchmarks/basic.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'rom-factory'
|
2
2
|
require 'rom-core'
|
3
3
|
require 'active_record'
|
4
|
-
require '
|
4
|
+
require 'factory_bot'
|
5
5
|
require 'fabrication'
|
6
6
|
require 'benchmark/ips'
|
7
7
|
|
@@ -33,7 +33,7 @@ end
|
|
33
33
|
|
34
34
|
ActiveRecord::Base.establish_connection(DATABASE_URL)
|
35
35
|
|
36
|
-
|
36
|
+
FactoryBot.define do
|
37
37
|
factory(:user) do
|
38
38
|
first_name "John"
|
39
39
|
last_name "Doe"
|
@@ -54,9 +54,9 @@ Benchmark.ips do |x|
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
-
x.report('
|
57
|
+
x.report('factory_bot') do
|
58
58
|
1000.times do
|
59
|
-
|
59
|
+
FactoryBot.create(:user)
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
data/lib/rom/factory.rb
CHANGED
@@ -21,9 +21,11 @@ module ROM
|
|
21
21
|
#
|
22
22
|
# @api public
|
23
23
|
def self.configure(name = DEFAULT_NAME, &block)
|
24
|
-
Dry::Core::ClassBuilder.new(name: name, parent: Factories).call do |klass|
|
24
|
+
klass = Dry::Core::ClassBuilder.new(name: name, parent: Factories).call do |klass|
|
25
25
|
klass.configure(&block)
|
26
26
|
end
|
27
|
+
|
28
|
+
klass.new(klass.config.rom)
|
27
29
|
end
|
28
30
|
end
|
29
31
|
end
|
@@ -9,9 +9,8 @@ module ROM::Factory
|
|
9
9
|
@block = block
|
10
10
|
end
|
11
11
|
|
12
|
-
def call(
|
13
|
-
|
14
|
-
block.call(increment)
|
12
|
+
def call(*args)
|
13
|
+
block.call(increment, *args)
|
15
14
|
end
|
16
15
|
|
17
16
|
def to_proc
|
@@ -25,6 +24,10 @@ module ROM::Factory
|
|
25
24
|
def dependency_names
|
26
25
|
EMPTY_ARRAY
|
27
26
|
end
|
27
|
+
|
28
|
+
def parameters
|
29
|
+
block.parameters
|
30
|
+
end
|
28
31
|
end
|
29
32
|
end
|
30
33
|
end
|
data/lib/rom/factory/builder.rb
CHANGED
@@ -1,24 +1,24 @@
|
|
1
1
|
require 'dry/core/constants'
|
2
2
|
|
3
|
+
require 'rom/struct'
|
4
|
+
require 'rom/initializer'
|
3
5
|
require 'rom/factory/tuple_evaluator'
|
4
6
|
require 'rom/factory/builder/persistable'
|
5
7
|
|
6
8
|
module ROM::Factory
|
7
9
|
# @api private
|
8
10
|
class Builder
|
9
|
-
|
11
|
+
extend ROM::Initializer
|
10
12
|
|
11
|
-
|
12
|
-
attr_reader :attributes
|
13
|
+
include Dry::Core::Constants
|
13
14
|
|
14
|
-
#
|
15
|
-
|
15
|
+
# @!attribute [r] attributes
|
16
|
+
# @return [ROM::Factory::Attributes]
|
17
|
+
param :attributes
|
16
18
|
|
17
|
-
#
|
18
|
-
|
19
|
-
|
20
|
-
@tuple_evaluator = TupleEvaluator.new(attributes, relation)
|
21
|
-
end
|
19
|
+
# @!attribute [r] relation
|
20
|
+
# @return [ROM::Relation]
|
21
|
+
option :relation, reader: false
|
22
22
|
|
23
23
|
# @api private
|
24
24
|
def tuple(attrs = EMPTY_HASH)
|
@@ -32,8 +32,18 @@ module ROM::Factory
|
|
32
32
|
alias_method :create, :struct
|
33
33
|
|
34
34
|
# @api private
|
35
|
-
def
|
36
|
-
|
35
|
+
def struct_namespace(namespace)
|
36
|
+
with(relation: relation.struct_namespace(namespace))
|
37
|
+
end
|
38
|
+
|
39
|
+
# @api private
|
40
|
+
def persistable(struct_namespace = ROM::Struct)
|
41
|
+
Persistable.new(self, relation.struct_namespace(struct_namespace))
|
42
|
+
end
|
43
|
+
|
44
|
+
# @api private
|
45
|
+
def tuple_evaluator
|
46
|
+
@__tuple_evaluator__ ||= TupleEvaluator.new(attributes, options[:relation])
|
37
47
|
end
|
38
48
|
|
39
49
|
# @api private
|
data/lib/rom/factory/dsl.rb
CHANGED
@@ -39,7 +39,7 @@ module ROM
|
|
39
39
|
|
40
40
|
# @api private
|
41
41
|
def call
|
42
|
-
::ROM::Factory::Builder.new(_attributes, _relation)
|
42
|
+
::ROM::Factory::Builder.new(_attributes, relation: _relation)
|
43
43
|
end
|
44
44
|
|
45
45
|
# Delegate to a builder and persist a struct
|
@@ -138,13 +138,13 @@ module ROM
|
|
138
138
|
|
139
139
|
# @api private
|
140
140
|
def define_sequence(name, block)
|
141
|
-
_attributes << attributes::Callable.new(name, self,
|
141
|
+
_attributes << attributes::Callable.new(name, self, attributes::Sequence.new(name, &block))
|
142
142
|
end
|
143
143
|
|
144
144
|
# @api private
|
145
145
|
def define_attr(name, *args, &block)
|
146
146
|
if block
|
147
|
-
_attributes << attributes::Callable.new(name, self,
|
147
|
+
_attributes << attributes::Callable.new(name, self, block)
|
148
148
|
else
|
149
149
|
_attributes << attributes::Value.new(name, *args)
|
150
150
|
end
|
@@ -1,7 +1,10 @@
|
|
1
1
|
require 'dry/configurable'
|
2
2
|
require 'dry/core/inflector'
|
3
3
|
|
4
|
+
require 'rom/initializer'
|
5
|
+
require 'rom/struct'
|
4
6
|
require 'rom/factory/dsl'
|
7
|
+
require 'rom/factory/registry'
|
5
8
|
|
6
9
|
module ROM::Factory
|
7
10
|
# In-memory builder API
|
@@ -12,9 +15,14 @@ module ROM::Factory
|
|
12
15
|
# @return [Hash<Symbol=>Builder>]
|
13
16
|
attr_reader :registry
|
14
17
|
|
18
|
+
# @!attribute [r] struct_namespace
|
19
|
+
# @return [Module]
|
20
|
+
attr_reader :struct_namespace
|
21
|
+
|
15
22
|
# @api private
|
16
|
-
def initialize(registry)
|
23
|
+
def initialize(registry, struct_namespace)
|
17
24
|
@registry = registry
|
25
|
+
@struct_namespace = struct_namespace
|
18
26
|
end
|
19
27
|
|
20
28
|
# Build an in-memory struct
|
@@ -32,7 +40,7 @@ module ROM::Factory
|
|
32
40
|
#
|
33
41
|
# @api public
|
34
42
|
def [](name, attrs = {})
|
35
|
-
registry[name].create(attrs)
|
43
|
+
registry[name].struct_namespace(struct_namespace).create(attrs)
|
36
44
|
end
|
37
45
|
end
|
38
46
|
|
@@ -41,144 +49,169 @@ module ROM::Factory
|
|
41
49
|
# @api public
|
42
50
|
class Factories
|
43
51
|
extend Dry::Configurable
|
52
|
+
extend ROM::Initializer
|
44
53
|
|
45
54
|
setting :rom
|
46
55
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
56
|
+
# @!attribute [r] rom
|
57
|
+
# @return [ROM::Container] configured rom container
|
58
|
+
param :rom
|
59
|
+
|
60
|
+
# @!attribute [r] struct_namespace
|
61
|
+
# @return [Structs] in-memory struct builder instance
|
62
|
+
option :struct_namespace, optional: true, default: proc { ROM::Struct }
|
63
|
+
|
64
|
+
# @!attribute [r] registry
|
65
|
+
# @return [Hash<Symbol=>Builder>] a map with defined db-backed builders
|
66
|
+
option :registry, default: proc { Registry.new }
|
67
|
+
|
68
|
+
# Define a new builder
|
69
|
+
#
|
70
|
+
# @example a simple builder
|
71
|
+
# MyFactory.define(:user) do |f|
|
72
|
+
# f.name "Jane"
|
73
|
+
# f.email "jane@doe.org"
|
74
|
+
# end
|
75
|
+
#
|
76
|
+
# @example a builder using auto-generated fake values
|
77
|
+
# MyFactory.define(:user) do |f|
|
78
|
+
# f.name { fake(:name) }
|
79
|
+
# f.email { fake(:internet, :email) }
|
80
|
+
# end
|
81
|
+
#
|
82
|
+
# @example a builder using sequenced values
|
83
|
+
# MyFactory.define(:user) do |f|
|
84
|
+
# f.sequence(:name) { |n| "user-#{n}" }
|
85
|
+
# end
|
86
|
+
#
|
87
|
+
# @example a builder using values from other attribute(s)
|
88
|
+
# MyFactory.define(:user) do |f|
|
89
|
+
# f.name "Jane"
|
90
|
+
# f.email { |name| "#{name.downcase}@rom-rb.org" }
|
91
|
+
# end
|
92
|
+
#
|
93
|
+
# @example a builder with "belongs-to" association
|
94
|
+
# MyFactory.define(:group) do |f|
|
95
|
+
# f.name "Admins"
|
96
|
+
# end
|
97
|
+
#
|
98
|
+
# MyFactory.define(:user) do |f|
|
99
|
+
# f.name "Jane"
|
100
|
+
# f.association(:group)
|
101
|
+
# end
|
102
|
+
#
|
103
|
+
# @example a builder with "has-many" association
|
104
|
+
# MyFactory.define(:group) do |f|
|
105
|
+
# f.name "Admins"
|
106
|
+
# f.association(:users, count: 2)
|
107
|
+
# end
|
108
|
+
#
|
109
|
+
# MyFactory.define(:user) do |f|
|
110
|
+
# f.sequence(:name) { |n| "user-#{n}" }
|
111
|
+
# end
|
112
|
+
#
|
113
|
+
# @example a builder which extends another builder
|
114
|
+
# MyFactory.define(:user) do |f|
|
115
|
+
# f.name "Jane"
|
116
|
+
# f.admin false
|
117
|
+
# end
|
118
|
+
#
|
119
|
+
# MyFactory.define(admin: :user) do |f|
|
120
|
+
# f.admin true
|
121
|
+
# end
|
122
|
+
#
|
123
|
+
# @param [Symbol, Hash<Symbol=>Symbol>] spec Builder identifier, can point to a parent builder too
|
124
|
+
# @param [Hash] opts Additional options
|
125
|
+
# @option opts [Symbol] relation An optional relation name (defaults to pluralized builder name)
|
126
|
+
#
|
127
|
+
# @return [ROM::Factory::Builder]
|
128
|
+
#
|
129
|
+
# @api public
|
130
|
+
def define(spec, **opts, &block)
|
131
|
+
name, parent = spec.is_a?(Hash) ? spec.flatten(1) : spec
|
132
|
+
|
133
|
+
if registry.key?(name)
|
134
|
+
raise ArgumentError, "#{name.inspect} factory has been already defined"
|
62
135
|
end
|
63
136
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
#
|
72
|
-
# @example a builder using auto-generated fake values
|
73
|
-
# MyFactory.define(:user) do |f|
|
74
|
-
# f.name { fake(:name) }
|
75
|
-
# f.email { fake(:internet, :email) }
|
76
|
-
# end
|
77
|
-
#
|
78
|
-
# @example a builder using sequenced values
|
79
|
-
# MyFactory.define(:user) do |f|
|
80
|
-
# f.sequence(:name) { |n| "user-#{n}" }
|
81
|
-
# end
|
82
|
-
#
|
83
|
-
# @example a builder using values from other attribute(s)
|
84
|
-
# MyFactory.define(:user) do |f|
|
85
|
-
# f.name "Jane"
|
86
|
-
# f.email { |name| "#{name.downcase}@rom-rb.org" }
|
87
|
-
# end
|
88
|
-
#
|
89
|
-
# @example a builder with "belongs-to" association
|
90
|
-
# MyFactory.define(:group) do |f|
|
91
|
-
# f.name "Admins"
|
92
|
-
# end
|
93
|
-
#
|
94
|
-
# MyFactory.define(:user) do |f|
|
95
|
-
# f.name "Jane"
|
96
|
-
# f.association(:group)
|
97
|
-
# end
|
98
|
-
#
|
99
|
-
# @example a builder with "has-many" association
|
100
|
-
# MyFactory.define(:group) do |f|
|
101
|
-
# f.name "Admins"
|
102
|
-
# f.association(:users, count: 2)
|
103
|
-
# end
|
104
|
-
#
|
105
|
-
# MyFactory.define(:user) do |f|
|
106
|
-
# f.sequence(:name) { |n| "user-#{n}" }
|
107
|
-
# end
|
108
|
-
#
|
109
|
-
# @example a builder which extends another builder
|
110
|
-
# MyFactory.define(:user) do |f|
|
111
|
-
# f.name "Jane"
|
112
|
-
# f.admin false
|
113
|
-
# end
|
114
|
-
#
|
115
|
-
# MyFactory.define(admin: :user) do |f|
|
116
|
-
# f.admin true
|
117
|
-
# end
|
118
|
-
#
|
119
|
-
# @param [Symbol, Hash<Symbol=>Symbol>] spec Builder identifier, can point to a parent builder too
|
120
|
-
# @param [Hash] opts Additional options
|
121
|
-
# @option opts [Symbol] relation An optional relation name (defaults to pluralized builder name)
|
122
|
-
#
|
123
|
-
# @return [ROM::Factory::Builder]
|
124
|
-
#
|
125
|
-
# @api public
|
126
|
-
def define(spec, **opts, &block)
|
127
|
-
name, parent = spec.is_a?(Hash) ? spec.flatten(1) : spec
|
128
|
-
|
129
|
-
if registry.key?(name)
|
130
|
-
raise ArgumentError, "#{name.inspect} factory has been already defined"
|
137
|
+
builder =
|
138
|
+
if parent
|
139
|
+
extend_builder(name, registry[parent], &block)
|
140
|
+
else
|
141
|
+
relation_name = opts.fetch(:relation) { infer_relation(name) }
|
142
|
+
relation = rom.relations[relation_name]
|
143
|
+
DSL.new(name, relation: relation.struct_namespace(struct_namespace), factories: self, &block).call
|
131
144
|
end
|
132
145
|
|
133
|
-
|
134
|
-
|
135
|
-
extend_builder(name, registry[parent], &block)
|
136
|
-
else
|
137
|
-
relation_name = opts.fetch(:relation) { infer_relation(name) }
|
138
|
-
relation = config.rom.relations[relation_name]
|
139
|
-
DSL.new(name, relation: relation, factories: self, &block).call
|
140
|
-
end
|
146
|
+
registry[name] = builder
|
147
|
+
end
|
141
148
|
|
142
|
-
|
143
|
-
|
149
|
+
# Create and persist a new struct
|
150
|
+
#
|
151
|
+
# @example create a struct with default attributes
|
152
|
+
# MyFactory[:user]
|
153
|
+
#
|
154
|
+
# @example create a struct with some attributes overridden
|
155
|
+
# MyFactory[:user, name: "Jane"]
|
156
|
+
#
|
157
|
+
# @param [Symbol] name The name of the registered factory
|
158
|
+
# @param [Hash] attrs An optional hash with attributes
|
159
|
+
#
|
160
|
+
# @return [ROM::Struct]
|
161
|
+
#
|
162
|
+
# @api public
|
163
|
+
def [](name, attrs = {})
|
164
|
+
registry[name].persistable(struct_namespace).create(attrs)
|
165
|
+
end
|
144
166
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
# @param [Symbol] name The name of the registered factory
|
154
|
-
# @param [Hash] attrs An optional hash with attributes
|
155
|
-
#
|
156
|
-
# @return [ROM::Struct]
|
157
|
-
#
|
158
|
-
# @api public
|
159
|
-
def [](name, attrs = {})
|
160
|
-
registry[name].persistable.create(attrs)
|
161
|
-
end
|
167
|
+
# Return in-memory struct builder
|
168
|
+
#
|
169
|
+
# @return [Structs]
|
170
|
+
#
|
171
|
+
# @api public
|
172
|
+
def structs
|
173
|
+
@__structs__ ||= Structs.new(registry, struct_namespace)
|
174
|
+
end
|
162
175
|
|
163
|
-
|
164
|
-
|
165
|
-
|
176
|
+
# Get factories with a custom struct namespace
|
177
|
+
#
|
178
|
+
# @example
|
179
|
+
# EntityFactory = MyFactory.struct_namespace(MyApp::Entities)
|
180
|
+
#
|
181
|
+
# EntityFactory[:user]
|
182
|
+
# # => #<MyApp::Entities::User id=2 ...>
|
183
|
+
#
|
184
|
+
# @param [Module] namespace
|
185
|
+
#
|
186
|
+
# @return [Factories]
|
187
|
+
#
|
188
|
+
# @api public
|
189
|
+
def struct_namespace(namespace = Undefined)
|
190
|
+
if namespace.equal?(Undefined)
|
191
|
+
options[:struct_namespace]
|
192
|
+
else
|
193
|
+
with(struct_namespace: namespace)
|
166
194
|
end
|
195
|
+
end
|
167
196
|
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
197
|
+
# @api private
|
198
|
+
def for_relation(relation)
|
199
|
+
registry[infer_factory_name(relation.name.to_sym)]
|
200
|
+
end
|
172
201
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
202
|
+
# @api private
|
203
|
+
def infer_factory_name(name)
|
204
|
+
::Dry::Core::Inflector.singularize(name).to_sym
|
205
|
+
end
|
177
206
|
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
207
|
+
# @api private
|
208
|
+
def infer_relation(name)
|
209
|
+
::Dry::Core::Inflector.pluralize(name).to_sym
|
210
|
+
end
|
211
|
+
|
212
|
+
# @api private
|
213
|
+
def extend_builder(name, parent, &block)
|
214
|
+
DSL.new(name, attributes: parent.attributes, relation: parent.relation, factories: self, &block).call
|
182
215
|
end
|
183
216
|
end
|
184
217
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'rom/factory/constants'
|
2
|
+
|
3
|
+
module ROM
|
4
|
+
module Factory
|
5
|
+
# @api private
|
6
|
+
class Registry
|
7
|
+
# @!attribute [r] elements
|
8
|
+
# @return [Hash] a hash with factory builders
|
9
|
+
attr_reader :elements
|
10
|
+
|
11
|
+
# @api private
|
12
|
+
def initialize
|
13
|
+
@elements = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
# @api private
|
17
|
+
def key?(name)
|
18
|
+
elements.key?(name)
|
19
|
+
end
|
20
|
+
|
21
|
+
# @api private
|
22
|
+
def []=(name, builder)
|
23
|
+
elements[name] = builder
|
24
|
+
end
|
25
|
+
|
26
|
+
# @api private
|
27
|
+
def [](name)
|
28
|
+
elements.fetch(name) do
|
29
|
+
raise FactoryNotDefinedError.new(name)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module ROM
|
4
|
+
module Factory
|
5
|
+
# @api private
|
6
|
+
class Sequences
|
7
|
+
include Singleton
|
8
|
+
|
9
|
+
# @api private
|
10
|
+
attr_reader :registry
|
11
|
+
|
12
|
+
# @api private
|
13
|
+
def self.[](relation)
|
14
|
+
key = :"#{relation.gateway}-#{relation.name.dataset}"
|
15
|
+
-> { instance.next(key) }
|
16
|
+
end
|
17
|
+
|
18
|
+
# @api private
|
19
|
+
def initialize
|
20
|
+
reset
|
21
|
+
end
|
22
|
+
|
23
|
+
# @api private
|
24
|
+
def next(key)
|
25
|
+
registry[key] += 1
|
26
|
+
end
|
27
|
+
|
28
|
+
# @api private
|
29
|
+
def reset
|
30
|
+
@registry = Concurrent::Map.new { |h, k| h[k] = 0 }
|
31
|
+
self
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'rom/factory/sequences'
|
2
|
+
|
1
3
|
module ROM
|
2
4
|
module Factory
|
3
5
|
# @api private
|
@@ -19,7 +21,7 @@ module ROM
|
|
19
21
|
@attributes = attributes
|
20
22
|
@relation = relation.with(auto_struct: true)
|
21
23
|
@model = @relation.combine(*assoc_names).mapper.model
|
22
|
-
@sequence =
|
24
|
+
@sequence = Sequences[relation]
|
23
25
|
end
|
24
26
|
|
25
27
|
# @api private
|
@@ -97,7 +99,7 @@ module ROM
|
|
97
99
|
|
98
100
|
# @api private
|
99
101
|
def next_id
|
100
|
-
|
102
|
+
sequence.()
|
101
103
|
end
|
102
104
|
end
|
103
105
|
end
|
data/lib/rom/factory/version.rb
CHANGED
data/rom-factory.gemspec
CHANGED
@@ -29,7 +29,6 @@ Gem::Specification.new do |spec|
|
|
29
29
|
|
30
30
|
spec.add_dependency "dry-configurable", "~> 0.7"
|
31
31
|
spec.add_dependency "dry-core", "~> 0.3", ">= 0.3.1"
|
32
|
-
spec.add_dependency "dry-struct", "~> 0.3"
|
33
32
|
spec.add_dependency "rom-core", "~> 4.0"
|
34
33
|
spec.add_dependency "faker", "~> 1.7"
|
35
34
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rom-factory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Janis Miezitis
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2018-01-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: dry-configurable
|
@@ -45,20 +45,6 @@ dependencies:
|
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: 0.3.1
|
48
|
-
- !ruby/object:Gem::Dependency
|
49
|
-
name: dry-struct
|
50
|
-
requirement: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0.3'
|
55
|
-
type: :runtime
|
56
|
-
prerelease: false
|
57
|
-
version_requirements: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - "~>"
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0.3'
|
62
48
|
- !ruby/object:Gem::Dependency
|
63
49
|
name: rom-core
|
64
50
|
requirement: !ruby/object:Gem::Requirement
|
@@ -95,6 +81,7 @@ executables: []
|
|
95
81
|
extensions: []
|
96
82
|
extra_rdoc_files: []
|
97
83
|
files:
|
84
|
+
- ".codeclimate.yml"
|
98
85
|
- ".gitignore"
|
99
86
|
- ".rspec"
|
100
87
|
- ".travis.yml"
|
@@ -116,8 +103,11 @@ files:
|
|
116
103
|
- lib/rom/factory/attributes/value.rb
|
117
104
|
- lib/rom/factory/builder.rb
|
118
105
|
- lib/rom/factory/builder/persistable.rb
|
106
|
+
- lib/rom/factory/constants.rb
|
119
107
|
- lib/rom/factory/dsl.rb
|
120
108
|
- lib/rom/factory/factories.rb
|
109
|
+
- lib/rom/factory/registry.rb
|
110
|
+
- lib/rom/factory/sequences.rb
|
121
111
|
- lib/rom/factory/tuple_evaluator.rb
|
122
112
|
- lib/rom/factory/version.rb
|
123
113
|
- rom-factory.gemspec
|
@@ -142,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
132
|
version: '0'
|
143
133
|
requirements: []
|
144
134
|
rubyforge_project:
|
145
|
-
rubygems_version: 2.
|
135
|
+
rubygems_version: 2.7.4
|
146
136
|
signing_key:
|
147
137
|
specification_version: 4
|
148
138
|
summary: ROM based builder library to make your specs awesome. DSL partially inspired
|