action_factory 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/action_factory/active_record.rb +2 -2
- data/lib/action_factory/assignments/attribute.rb +6 -0
- data/lib/action_factory/assignments/sequence.rb +7 -2
- data/lib/action_factory/assignments/trait.rb +6 -0
- data/lib/action_factory/base.rb +19 -13
- data/lib/action_factory/factory_finder.rb +1 -1
- data/lib/action_factory/registry.rb +50 -9
- data/lib/action_factory/version.rb +1 -1
- data/spec/action_factory/base_spec.rb +281 -0
- data/spec/action_factory/registry_spec.rb +125 -17
- data/spec/action_factory_spec.rb +1 -1
- data/spec/dummy/log/test.log +0 -0
- data/spec/dummy/tmp/development_secret.txt +1 -0
- metadata +9 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a1c5d74820ed98b22f20bc0a5e067d1c44e699c5cd409072413ff06cd947ab1
|
4
|
+
data.tar.gz: f543f0cb2ec678381a734d4e1424fb4afd5bfd7a3a00ada461c3072bb1a2c9ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8cbd4f57421075eea4cbed63fb4c690868c600ff0eaa61ceeae44923f3dd05554e3f5995c682c711a441eea0707793a53077f6078c02a4ecfb9226a4a9acf688
|
7
|
+
data.tar.gz: 2c1f35b170f5c75530536de81bb1e55de0f3d6ed39a6d490bc698c1e842575da02adc1d6ca1c02e021cc19d35503c7cfe276a385f51e658d04d7593311a11390
|
@@ -27,7 +27,7 @@ module ActionFactory
|
|
27
27
|
def self.included(base)
|
28
28
|
base.extend ClassMethods
|
29
29
|
base.class_exec do
|
30
|
-
|
30
|
+
before_assign_attributes :build_associations
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -46,7 +46,7 @@ module ActionFactory
|
|
46
46
|
def build_associations
|
47
47
|
self.class.associations.except(*@attributes.keys).each do |name, association|
|
48
48
|
associated_record = association.generate(@strategy)
|
49
|
-
@instance.association(name).writer(associated_record)
|
49
|
+
@instance.association(name.to_sym).writer(associated_record)
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
@@ -3,6 +3,8 @@
|
|
3
3
|
module ActionFactory
|
4
4
|
module Assignments
|
5
5
|
class Attribute
|
6
|
+
attr_reader :block
|
7
|
+
|
6
8
|
def initialize(block)
|
7
9
|
@block = block
|
8
10
|
end
|
@@ -10,6 +12,10 @@ module ActionFactory
|
|
10
12
|
def compile(factory)
|
11
13
|
factory.instance_exec(&@block)
|
12
14
|
end
|
15
|
+
|
16
|
+
def ==(other)
|
17
|
+
other.is_a?(self.class) && other.block == @block
|
18
|
+
end
|
13
19
|
end
|
14
20
|
end
|
15
21
|
end
|
@@ -3,15 +3,20 @@
|
|
3
3
|
module ActionFactory
|
4
4
|
module Assignments
|
5
5
|
class Sequence
|
6
|
+
attr_reader :block, :count
|
7
|
+
|
6
8
|
def initialize(block)
|
7
|
-
@count = 0
|
8
9
|
@block = block
|
10
|
+
@count = 0
|
9
11
|
end
|
10
12
|
|
11
13
|
def compile(factory)
|
12
|
-
# @count += 1
|
13
14
|
factory.instance_exec(@count += 1, &@block)
|
14
15
|
end
|
16
|
+
|
17
|
+
def ==(other)
|
18
|
+
other.is_a?(self.class) && other.block == @block && other.count == @count
|
19
|
+
end
|
15
20
|
end
|
16
21
|
end
|
17
22
|
end
|
@@ -3,6 +3,8 @@
|
|
3
3
|
module ActionFactory
|
4
4
|
module Assignments
|
5
5
|
class Trait
|
6
|
+
attr_reader :block
|
7
|
+
|
6
8
|
def initialize(block)
|
7
9
|
@block = block
|
8
10
|
end
|
@@ -10,6 +12,10 @@ module ActionFactory
|
|
10
12
|
def compile(factory)
|
11
13
|
factory.instance_exec(factory.instance, &@block)
|
12
14
|
end
|
15
|
+
|
16
|
+
def ==(other)
|
17
|
+
other.is_a?(self.class) && other.block == @block
|
18
|
+
end
|
13
19
|
end
|
14
20
|
end
|
15
21
|
end
|
data/lib/action_factory/base.rb
CHANGED
@@ -22,22 +22,22 @@ module ActionFactory
|
|
22
22
|
class << self
|
23
23
|
attr_reader :initializer, :creator
|
24
24
|
|
25
|
-
def
|
26
|
-
|
27
|
-
end
|
25
|
+
def register(factory_name: nil, class_name: nil)
|
26
|
+
raise ArgumentError, 'factory_name or class_name required' unless factory_name || class_name
|
28
27
|
|
29
|
-
|
30
|
-
@klass_name = name
|
28
|
+
Registry.register(factory_class_name: self.name, factory_name: factory_name, class_name: class_name)
|
31
29
|
end
|
32
30
|
|
33
|
-
def
|
34
|
-
|
31
|
+
def class_name
|
32
|
+
Registry.class_name_for(self.name)
|
35
33
|
end
|
36
34
|
|
37
35
|
def klass
|
38
|
-
@klass ||=
|
36
|
+
@klass ||= class_name.constantize
|
39
37
|
rescue NameError
|
40
|
-
|
38
|
+
message = "Class with name #{class_name.inspect} not found, " \
|
39
|
+
"please use `register` if you need to configure a custom class name"
|
40
|
+
raise ClassNotFound, message
|
41
41
|
end
|
42
42
|
|
43
43
|
def to_initialize(&block)
|
@@ -75,12 +75,13 @@ module ActionFactory
|
|
75
75
|
|
76
76
|
delegate :initializer, :creator, :klass, :assignments, to: :class
|
77
77
|
|
78
|
-
attr_reader :traits, :attributes
|
78
|
+
attr_reader :traits, :attributes
|
79
79
|
|
80
80
|
def initialize(*traits, **attributes)
|
81
|
+
raise "cannot initialize base class" if self.class == ActionFactory::Base
|
82
|
+
|
81
83
|
@traits = traits
|
82
84
|
@attributes = attributes
|
83
|
-
@instance = build_instance_with_callbacks
|
84
85
|
end
|
85
86
|
|
86
87
|
def run(strategy)
|
@@ -89,6 +90,7 @@ module ActionFactory
|
|
89
90
|
end
|
90
91
|
|
91
92
|
def build
|
93
|
+
instance
|
92
94
|
run_callbacks :assign_attributes do
|
93
95
|
assign_attributes
|
94
96
|
end
|
@@ -107,6 +109,10 @@ module ActionFactory
|
|
107
109
|
@factory_attributes ||= assignment_compiler.compile(assignments[:attributes], except: @attributes.keys)
|
108
110
|
end
|
109
111
|
|
112
|
+
def instance
|
113
|
+
@instance ||= build_instance_with_callbacks
|
114
|
+
end
|
115
|
+
|
110
116
|
private
|
111
117
|
|
112
118
|
def build_instance_with_callbacks
|
@@ -122,9 +128,9 @@ module ActionFactory
|
|
122
128
|
end
|
123
129
|
|
124
130
|
def persist_instance
|
125
|
-
return
|
131
|
+
return instance.instance_exec(self, &creator) if creator
|
126
132
|
|
127
|
-
|
133
|
+
instance.public_send(ActionFactory.configuration.persist_method)
|
128
134
|
end
|
129
135
|
|
130
136
|
def assign_attributes
|
@@ -17,7 +17,7 @@ module ActionFactory
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def factory_class
|
20
|
-
factory_class_name = Registry.
|
20
|
+
factory_class_name = Registry.factory_class_name_for(@name)
|
21
21
|
factory_class_name.constantize
|
22
22
|
rescue NameError
|
23
23
|
raise FactoryNotFound, "Factory with class name #{factory_class_name.inspect} not found"
|
@@ -3,23 +3,64 @@
|
|
3
3
|
require "active_support/core_ext/string/inflections"
|
4
4
|
|
5
5
|
module ActionFactory
|
6
|
-
|
6
|
+
class Registry
|
7
7
|
class << self
|
8
|
-
|
9
|
-
|
8
|
+
delegate :register, :factory_class_name_for, :class_name_for, to: :instance
|
9
|
+
|
10
|
+
def instance
|
11
|
+
@instance ||= new
|
10
12
|
end
|
11
13
|
|
12
|
-
def
|
13
|
-
@
|
14
|
+
def clear
|
15
|
+
@instance = nil
|
14
16
|
end
|
17
|
+
end
|
15
18
|
|
16
|
-
|
17
|
-
|
19
|
+
def initialize
|
20
|
+
@class_name_by_factory_class_name = Hash.new do |class_names, factory_class_name|
|
21
|
+
class_names[factory_class_name.to_s] = factory_class_name.delete_suffix("Factory")
|
22
|
+
end
|
23
|
+
@class_name_by_symbol = Hash.new do |class_names, symbol|
|
24
|
+
class_names[symbol.to_sym] = symbol.to_s.classify
|
18
25
|
end
|
26
|
+
@factories_by_class_name = Hash.new do |factories, class_name|
|
27
|
+
factories[class_name.to_s] = "#{class_name}Factory"
|
28
|
+
end
|
29
|
+
end
|
19
30
|
|
20
|
-
|
21
|
-
|
31
|
+
def register(factory_class_name:, factory_name: nil, class_name: nil)
|
32
|
+
class_name ||= factory_class_name.to_s.chomp("Factory")
|
33
|
+
factory_name ||= class_name.underscore
|
34
|
+
@class_name_by_factory_class_name[factory_class_name.to_s] = class_name
|
35
|
+
@class_name_by_symbol[factory_name.to_sym] = class_name
|
36
|
+
@factories_by_class_name[class_name.to_s] = factory_class_name
|
37
|
+
end
|
38
|
+
|
39
|
+
def factory_class_name_for(name)
|
40
|
+
case name
|
41
|
+
when Symbol
|
42
|
+
# Presume that the symbol is a factory name
|
43
|
+
@factories_by_class_name[class_name_for(name)]
|
44
|
+
when Class, String
|
45
|
+
# Presume that classes and strings are a model classes
|
46
|
+
@factories_by_class_name[name.to_s]
|
47
|
+
else
|
48
|
+
raise ArgumentError, "Invalid argument type #{name.class}"
|
22
49
|
end
|
23
50
|
end
|
51
|
+
|
52
|
+
def class_name_for(name)
|
53
|
+
case name
|
54
|
+
when Symbol
|
55
|
+
# Presume that symbols are factory names
|
56
|
+
@class_name_by_symbol[name]
|
57
|
+
when String, Class
|
58
|
+
# Presume that strings and classes are for factory class names
|
59
|
+
@class_name_by_factory_class_name[name.to_s]
|
60
|
+
else
|
61
|
+
raise ArgumentError, "Invalid argument type #{name.class}"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
24
65
|
end
|
25
66
|
end
|
@@ -0,0 +1,281 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "ostruct"
|
3
|
+
|
4
|
+
RSpec.describe ActionFactory::Base do
|
5
|
+
let(:factory_class_name) { "FooFactory" }
|
6
|
+
let(:class_name) { "Foo" }
|
7
|
+
let(:factory_class) { Class.new(described_class) }
|
8
|
+
let(:factory_instance) { factory_class.new(*traits, **attributes) }
|
9
|
+
let(:traits) { [] }
|
10
|
+
let(:attributes) { {} }
|
11
|
+
let(:factory_class_with_attributes) do
|
12
|
+
Class.new(described_class) do
|
13
|
+
attribute(:foo) { "foo" }
|
14
|
+
sequence(:bar) { |i| "bar-#{i}" }
|
15
|
+
trait(:baz) { self.baz = "baz" }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
before do
|
20
|
+
stub_const(factory_class_name, factory_class)
|
21
|
+
end
|
22
|
+
|
23
|
+
after do
|
24
|
+
ActionFactory::Registry.clear
|
25
|
+
end
|
26
|
+
|
27
|
+
describe ".register" do
|
28
|
+
context "when no arguments are provided" do
|
29
|
+
it "raises an error" do
|
30
|
+
expect { factory_class.register }.to raise_error(ArgumentError)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "when factory_name is provided" do
|
35
|
+
let(:factory_name) { :foobar }
|
36
|
+
|
37
|
+
it "registers the factory" do
|
38
|
+
expect { factory_class.register(factory_name: :foobar) }.to(
|
39
|
+
change { ActionFactory::Registry.factory_class_name_for(factory_name) }.from("FoobarFactory").to(factory_class_name)
|
40
|
+
)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "when class_name is provided" do
|
45
|
+
let(:class_name) { "BarFoo" }
|
46
|
+
|
47
|
+
it "registers the factory" do
|
48
|
+
expect { factory_class.register(class_name: class_name) }.to change {
|
49
|
+
ActionFactory::Registry.factory_class_name_for(class_name)
|
50
|
+
}.from("BarFooFactory").to(factory_class_name)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "when factory_name and class_name are provided" do
|
55
|
+
let(:factory_name) { :baz }
|
56
|
+
let(:class_name) { "Bar" }
|
57
|
+
it "registers the factory" do
|
58
|
+
expect { factory_class.register(factory_name: :baz, class_name: "Bar") }.to(
|
59
|
+
change { ActionFactory::Registry.factory_class_name_for(factory_name) }.from("BazFactory").to(factory_class_name).and(
|
60
|
+
change { ActionFactory::Registry.factory_class_name_for(class_name) }.from("BarFactory").to(factory_class_name)
|
61
|
+
)
|
62
|
+
)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe ".klass" do
|
68
|
+
context "when the model class does not exist" do
|
69
|
+
it "raises an error" do
|
70
|
+
expect { factory_class.klass }.to raise_error(ActionFactory::Base::ClassNotFound)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "when the model class exists" do
|
75
|
+
let(:klass) { Class.new }
|
76
|
+
|
77
|
+
before do
|
78
|
+
stub_const(class_name, klass)
|
79
|
+
end
|
80
|
+
|
81
|
+
it "returns the class" do
|
82
|
+
expect(factory_class.klass).to eq(klass)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe ".to_initialize" do
|
88
|
+
context "when no block is provided" do
|
89
|
+
it "raises an error" do
|
90
|
+
expect { factory_class.to_initialize }.to raise_error(ArgumentError)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
context "when a block is provided" do
|
95
|
+
it "sets the initializer" do
|
96
|
+
proc = Proc.new { }
|
97
|
+
factory_class.to_initialize(&proc)
|
98
|
+
expect(factory_class.initializer).to eq(proc)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe ".to_create" do
|
104
|
+
context "when no block is provided" do
|
105
|
+
it "raises an error" do
|
106
|
+
expect { factory_class.to_create }.to raise_error(ArgumentError)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context "when a block is provided" do
|
111
|
+
it "sets the creator" do
|
112
|
+
proc = Proc.new { }
|
113
|
+
factory_class.to_create(&proc)
|
114
|
+
expect(factory_class.creator).to eq(proc)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe ".attribute" do
|
120
|
+
subject(:attribute_assignment) { factory_class.attribute(name, &block) }
|
121
|
+
let(:name) { :foo }
|
122
|
+
let(:block) { Proc.new { "They're taking the hobbits to Isengard!" } }
|
123
|
+
|
124
|
+
it "adds the attribute to the assignments" do
|
125
|
+
expect { attribute_assignment }.to(
|
126
|
+
change { factory_class.assignments[:attributes] }.from({}).to(
|
127
|
+
{ name => ActionFactory::Assignments::Attribute.new(block) }
|
128
|
+
)
|
129
|
+
)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
describe ".sequence" do
|
134
|
+
subject(:sequence_assignment) { factory_class.sequence(name, &block) }
|
135
|
+
let(:name) { :bar }
|
136
|
+
let(:block) { Proc.new { "Tis but a scratch" } }
|
137
|
+
|
138
|
+
it "adds the sequence to the assignments" do
|
139
|
+
expect { sequence_assignment }.to(
|
140
|
+
change { factory_class.assignments[:attributes] }.from({}).to(
|
141
|
+
{ name => ActionFactory::Assignments::Sequence.new(block) }
|
142
|
+
)
|
143
|
+
)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
describe ".trait" do
|
148
|
+
subject(:trait_assignment) { factory_class.trait(name, &block) }
|
149
|
+
let(:name) { :baz }
|
150
|
+
let(:block) { Proc.new { "I'm not dead yet!" } }
|
151
|
+
|
152
|
+
it "adds the trait to the assignments" do
|
153
|
+
expect { trait_assignment }.to(
|
154
|
+
change { factory_class.assignments[:traits] }.from({}).to(
|
155
|
+
{ name => ActionFactory::Assignments::Trait.new(block) }
|
156
|
+
)
|
157
|
+
)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
describe ".assignments" do
|
162
|
+
subject(:assignments) { factory_class.assignments }
|
163
|
+
|
164
|
+
context "when no assignments have been made" do
|
165
|
+
it { is_expected.to eq({}) }
|
166
|
+
|
167
|
+
it "returns a hash with default proc" do
|
168
|
+
expect(assignments[:foo]).to be_a(ActiveSupport::HashWithIndifferentAccess)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
context "when assignments have been made" do
|
173
|
+
let!(:attribute_assignment) { factory_class.attribute(:foo) { } }
|
174
|
+
let!(:sequence_assignment) { factory_class.sequence(:bar) { } }
|
175
|
+
let!(:trait_assignment) { factory_class.trait(:baz) { } }
|
176
|
+
|
177
|
+
it "returns the assignments" do
|
178
|
+
expect(assignments).to eq(
|
179
|
+
"attributes" => {
|
180
|
+
"foo" => attribute_assignment,
|
181
|
+
"bar" => sequence_assignment
|
182
|
+
},
|
183
|
+
"traits" => {
|
184
|
+
"baz" => trait_assignment
|
185
|
+
}
|
186
|
+
)
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
context "when assignments have been made in a parent class" do
|
191
|
+
let(:parent_factory_class) do
|
192
|
+
Class.new(described_class) do
|
193
|
+
attribute(:foo) { "foo" }
|
194
|
+
sequence(:bar) { "bar" }
|
195
|
+
trait(:baz) { "baz" }
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
let(:factory_class) do
|
200
|
+
Class.new(parent_factory_class) do
|
201
|
+
attribute(:qux) { "qux" }
|
202
|
+
sequence(:bar) { "other bar" }
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
it "returns the expected assignments" do
|
207
|
+
expect(factory_class.assignments).to eq(
|
208
|
+
"attributes" => {
|
209
|
+
"foo" => parent_factory_class.assignments[:attributes][:foo],
|
210
|
+
"bar" => factory_class.assignments[:attributes][:bar],
|
211
|
+
"qux" => factory_class.assignments[:attributes][:qux]
|
212
|
+
},
|
213
|
+
"traits" => {
|
214
|
+
"baz" => parent_factory_class.assignments[:traits][:baz]
|
215
|
+
}
|
216
|
+
)
|
217
|
+
end
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
describe "#initialize" do
|
222
|
+
context "when the class is ActionFactory::Base" do
|
223
|
+
it "raises an error" do
|
224
|
+
expect { described_class.new }.to raise_error("cannot initialize base class")
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
context "when the class is not ActionFactory::Base" do
|
229
|
+
it "does not raise an error" do
|
230
|
+
expect { factory_class.new }.not_to raise_error
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
describe "#run" do
|
236
|
+
context "when strategy is build" do
|
237
|
+
let(:strategy) { :build }
|
238
|
+
|
239
|
+
it "calls build" do
|
240
|
+
expect(factory_instance).to receive(:build)
|
241
|
+
factory_instance.run(strategy)
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
context "when strategy is create" do
|
246
|
+
let(:strategy) { :create }
|
247
|
+
|
248
|
+
it "calls create" do
|
249
|
+
expect(factory_instance).to receive(:create)
|
250
|
+
factory_instance.run(strategy)
|
251
|
+
end
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
describe "#build" do
|
256
|
+
subject(:build) { factory_instance.build }
|
257
|
+
let(:model_class) { Class.new(OpenStruct) }
|
258
|
+
|
259
|
+
before do
|
260
|
+
stub_const(class_name, model_class)
|
261
|
+
end
|
262
|
+
|
263
|
+
it "returns the instance" do
|
264
|
+
expect(build).to be_an_instance_of(model_class)
|
265
|
+
end
|
266
|
+
|
267
|
+
context "when the initializer is defined" do
|
268
|
+
let(:initializer) { Proc.new { } }
|
269
|
+
let(:built_instance) { instance_double(model_class) }
|
270
|
+
|
271
|
+
before do
|
272
|
+
factory_class.to_initialize(&initializer)
|
273
|
+
end
|
274
|
+
|
275
|
+
it "calls the initializer" do
|
276
|
+
expect(model_class).to receive(:class_exec).with(factory_instance, &initializer).and_return(built_instance)
|
277
|
+
expect(build).to eq(built_instance)
|
278
|
+
end
|
279
|
+
end
|
280
|
+
end
|
281
|
+
end
|
@@ -2,44 +2,152 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
RSpec.describe ActionFactory::Registry do
|
4
4
|
let(:factory_name) { :user }
|
5
|
-
let(:
|
5
|
+
let(:default_class_name) { "User" }
|
6
|
+
let(:custom_class_name) { "CustomUser" }
|
6
7
|
let(:default_factory_class_name) { "UserFactory" }
|
7
|
-
let(:
|
8
|
+
let(:custom_factory_class_name) { "SomeCustomUserFactory" }
|
9
|
+
|
10
|
+
after do
|
11
|
+
described_class.clear
|
12
|
+
end
|
8
13
|
|
9
14
|
describe ".register" do
|
10
|
-
subject(:register)
|
15
|
+
subject(:register) do
|
16
|
+
described_class.register(
|
17
|
+
factory_class_name: custom_factory_class_name,
|
18
|
+
factory_name: factory_name,
|
19
|
+
class_name: custom_class_name
|
20
|
+
)
|
21
|
+
end
|
11
22
|
|
12
23
|
it "registers the factory class name for the given factory name" do
|
13
24
|
expect { register }.to(
|
14
|
-
change { described_class.
|
25
|
+
change { described_class.factory_class_name_for(factory_name) }.from(
|
15
26
|
default_factory_class_name
|
16
27
|
).to(
|
17
|
-
|
28
|
+
custom_factory_class_name
|
18
29
|
)
|
19
30
|
)
|
20
31
|
end
|
21
32
|
end
|
22
33
|
|
23
|
-
describe ".
|
24
|
-
subject { described_class.
|
34
|
+
describe ".factory_class_name_for" do
|
35
|
+
subject(:factory_class_name_for) { described_class.factory_class_name_for(name) }
|
36
|
+
|
37
|
+
context "when the name is registered" do
|
38
|
+
before do
|
39
|
+
described_class.register(
|
40
|
+
factory_class_name: custom_factory_class_name,
|
41
|
+
factory_name: factory_name,
|
42
|
+
class_name: custom_class_name
|
43
|
+
)
|
44
|
+
end
|
45
|
+
|
46
|
+
context "when the name is a symbol" do
|
47
|
+
let(:name) { factory_name }
|
48
|
+
|
49
|
+
it { is_expected.to eq(custom_factory_class_name) }
|
50
|
+
end
|
51
|
+
|
52
|
+
context "when the name is a string" do
|
53
|
+
let(:name) { custom_class_name }
|
25
54
|
|
26
|
-
|
55
|
+
it { is_expected.to eq(custom_factory_class_name) }
|
56
|
+
end
|
27
57
|
|
28
|
-
|
29
|
-
|
30
|
-
|
58
|
+
context "when the name is a class" do
|
59
|
+
let(:name) { custom_class_name.constantize }
|
60
|
+
|
61
|
+
before do
|
62
|
+
stub_const(custom_class_name, Class.new)
|
63
|
+
end
|
64
|
+
|
65
|
+
it { is_expected.to eq(custom_factory_class_name) }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context "when the name is not registered" do
|
70
|
+
context "when the name is a symbol" do
|
71
|
+
let(:name) { factory_name }
|
72
|
+
|
73
|
+
it { is_expected.to eq(default_factory_class_name) }
|
74
|
+
end
|
75
|
+
|
76
|
+
context "when the name is a string" do
|
77
|
+
let(:name) { default_class_name }
|
78
|
+
|
79
|
+
it { is_expected.to eq(default_factory_class_name) }
|
80
|
+
end
|
81
|
+
|
82
|
+
context "when the name is a class" do
|
83
|
+
let(:name) { default_class_name.constantize }
|
84
|
+
|
85
|
+
before do
|
86
|
+
stub_const(default_class_name, Class.new)
|
87
|
+
end
|
88
|
+
|
89
|
+
it { is_expected.to eq(default_factory_class_name) }
|
90
|
+
end
|
31
91
|
end
|
32
92
|
end
|
33
93
|
|
34
|
-
describe ".
|
35
|
-
|
36
|
-
|
94
|
+
describe ".class_name_for" do
|
95
|
+
subject(:class_name_for) { described_class.class_name_for(name) }
|
96
|
+
|
97
|
+
context "when the name is registered" do
|
98
|
+
before do
|
99
|
+
described_class.register(
|
100
|
+
factory_class_name: custom_factory_class_name,
|
101
|
+
factory_name: factory_name,
|
102
|
+
class_name: custom_class_name
|
103
|
+
)
|
104
|
+
end
|
105
|
+
|
106
|
+
context "when the name is a symbol" do
|
107
|
+
let(:name) { factory_name }
|
108
|
+
|
109
|
+
it { is_expected.to eq(custom_class_name) }
|
110
|
+
end
|
111
|
+
|
112
|
+
context "when the name is a string" do
|
113
|
+
let(:name) { custom_factory_class_name }
|
114
|
+
|
115
|
+
it { is_expected.to eq(custom_class_name) }
|
116
|
+
end
|
117
|
+
|
118
|
+
context "when the name is a class" do
|
119
|
+
let(:name) { custom_factory_class_name.constantize }
|
120
|
+
|
121
|
+
before do
|
122
|
+
stub_const(custom_factory_class_name, Class.new)
|
123
|
+
end
|
124
|
+
|
125
|
+
it { is_expected.to eq(custom_class_name) }
|
126
|
+
end
|
37
127
|
end
|
38
128
|
|
39
|
-
|
129
|
+
context "when the name is not registered" do
|
130
|
+
context "when the name is a symbol" do
|
131
|
+
let(:name) { factory_name }
|
132
|
+
|
133
|
+
it { is_expected.to eq(default_class_name) }
|
134
|
+
end
|
135
|
+
|
136
|
+
context "when the name is a string" do
|
137
|
+
let(:name) { default_factory_class_name }
|
138
|
+
|
139
|
+
it { is_expected.to eq(default_class_name) }
|
140
|
+
end
|
141
|
+
|
142
|
+
context "when the name is a class" do
|
143
|
+
let(:name) { default_factory_class_name.constantize }
|
144
|
+
|
145
|
+
before do
|
146
|
+
stub_const(default_factory_class_name, Class.new)
|
147
|
+
end
|
40
148
|
|
41
|
-
|
42
|
-
|
149
|
+
it { is_expected.to eq(default_class_name) }
|
150
|
+
end
|
43
151
|
end
|
44
152
|
end
|
45
153
|
end
|
data/spec/action_factory_spec.rb
CHANGED
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
92372a41d1c65de126fb2aff47f912ae3bc409b922905b1383518a048021d85050a3122a30078e7c72f95866f8c740d57c2a5b226a8bda3656f31024bdacf591
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: action_factory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Loyola
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -103,10 +103,13 @@ files:
|
|
103
103
|
- lib/action_factory/version.rb
|
104
104
|
- spec/action_factory/assignment_compiler_spec.rb
|
105
105
|
- spec/action_factory/attribute_assigner_spec.rb
|
106
|
+
- spec/action_factory/base_spec.rb
|
106
107
|
- spec/action_factory/factory_finder_spec.rb
|
107
108
|
- spec/action_factory/registry_spec.rb
|
108
109
|
- spec/action_factory/runner_spec.rb
|
109
110
|
- spec/action_factory_spec.rb
|
111
|
+
- spec/dummy/log/test.log
|
112
|
+
- spec/dummy/tmp/development_secret.txt
|
110
113
|
- spec/spec_helper.rb
|
111
114
|
homepage: https://github.com/b-loyola/action_factory
|
112
115
|
licenses:
|
@@ -130,15 +133,18 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
130
133
|
- !ruby/object:Gem::Version
|
131
134
|
version: '0'
|
132
135
|
requirements: []
|
133
|
-
rubygems_version: 3.
|
136
|
+
rubygems_version: 3.5.6
|
134
137
|
signing_key:
|
135
138
|
specification_version: 4
|
136
139
|
summary: A Simple OOO Factory lib for Ruby (and Rails)
|
137
140
|
test_files:
|
138
141
|
- spec/action_factory/assignment_compiler_spec.rb
|
139
142
|
- spec/action_factory/attribute_assigner_spec.rb
|
143
|
+
- spec/action_factory/base_spec.rb
|
140
144
|
- spec/action_factory/factory_finder_spec.rb
|
141
145
|
- spec/action_factory/registry_spec.rb
|
142
146
|
- spec/action_factory/runner_spec.rb
|
143
147
|
- spec/action_factory_spec.rb
|
148
|
+
- spec/dummy/log/test.log
|
149
|
+
- spec/dummy/tmp/development_secret.txt
|
144
150
|
- spec/spec_helper.rb
|