Module: TestProf::FactoryDefault
- Extended by:
- Logging
- Defined in:
- lib/test_prof/factory_default.rb,
lib/test_prof/factory_default/factory_bot_patch.rb
Overview
Defined Under Namespace
Modules: DefaultSyntax, RunnerExt, StrategyExt
Classes: Configuration, NoopProfiler, Profiler
Constant Summary
Constants included
from Logging
Logging::COLORS
Class Attribute Summary collapse
Class Method Summary
collapse
Methods included from Logging
log
Class Attribute Details
.current_context ⇒ Object
Returns the value of attribute current_context.
196
197
198
|
# File 'lib/test_prof/factory_default.rb', line 196
def current_context
@current_context
end
|
.profiler ⇒ Object
Returns the value of attribute profiler.
197
198
199
|
# File 'lib/test_prof/factory_default.rb', line 197
def profiler
@profiler
end
|
.stats ⇒ Object
Returns the value of attribute stats.
197
198
199
|
# File 'lib/test_prof/factory_default.rb', line 197
def stats
@stats
end
|
Class Method Details
.config ⇒ Object
211
212
213
|
# File 'lib/test_prof/factory_default.rb', line 211
def config
@config ||= Configuration.new
end
|
215
216
217
|
# File 'lib/test_prof/factory_default.rb', line 215
def configure
yield config
end
|
.disable! ⇒ Object
301
302
303
304
305
306
307
308
|
# File 'lib/test_prof/factory_default.rb', line 301
def disable!
was_enabled = @enabled
@enabled = false
return unless block_given?
yield
ensure
@enabled = was_enabled
end
|
.enable! ⇒ Object
292
293
294
295
296
297
298
299
|
# File 'lib/test_prof/factory_default.rb', line 292
def enable!
was_enabled = @enabled
@enabled = true
return unless block_given?
yield
ensure
@enabled = was_enabled
end
|
.enabled? ⇒ Boolean
288
289
290
|
# File 'lib/test_prof/factory_default.rb', line 288
def enabled?
@enabled
end
|
.get(name, traits = nil, overrides = nil) ⇒ Object
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
# File 'lib/test_prof/factory_default.rb', line 239
def get(name, traits = nil, overrides = nil)
return unless enabled?
record = store[name]
return unless record
if traits && (trait_key = record[:traits][traits])
name = trait_key
record = store[name]
traits = nil
end
stats[name][:miss] += 1
if traits && !traits.empty? && record[:preserve_traits]
return
end
object = record[:object]
if overrides && !overrides.empty? && record[:preserve_attributes]
overrides.each do |name, value|
return unless object.respond_to?(name) return if object.public_send(name) != value end
end
stats[name][:miss] -= 1
stats[name][:hit] += 1
if record[:context] && (record[:context] != :example)
object.refind
else
object
end
end
|
.preserve_attributes=(val) ⇒ Object
224
225
226
|
# File 'lib/test_prof/factory_default.rb', line 224
def preserve_attributes=(val)
config.preserve_attributes = val
end
|
.preserve_traits=(val) ⇒ Object
220
221
222
|
# File 'lib/test_prof/factory_default.rb', line 220
def preserve_traits=(val)
config.preserve_traits = val
end
|
.print_report ⇒ Object
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
# File 'lib/test_prof/factory_default.rb', line 310
def print_report
profiler.print_report
return unless config.report_stats || config.report_summary
if stats.empty?
log :info, "FactoryDefault has not been used"
return
end
msgs = []
if config.report_stats
msgs <<
<<~MSG
FactoryDefault usage stats:
MSG
first_column = stats.keys.map(&:size).max + 2
msgs << format(
"%#{first_column}s %9s %9s",
"factory", "hit", "miss"
)
msgs << ""
end
total_hit = 0
total_miss = 0
stats.to_a.sort_by { |(_, v)| -v[:hit] }.each do |(key, record_stats)|
total_hit += record_stats[:hit]
total_miss += record_stats[:miss]
if config.report_stats
msgs << format(
"%#{first_column}s %9d %9d",
key, record_stats[:hit], record_stats[:miss]
)
end
end
msgs << "" if config.report_stats
msgs <<
<<~MSG
FactoryDefault summary: hit=#{total_hit} miss=#{total_miss}
MSG
log :info, msgs.join("\n")
end
|
.register(name, obj, **options) ⇒ Object
228
229
230
231
232
233
234
235
236
237
|
# File 'lib/test_prof/factory_default.rb', line 228
def register(name, obj, **options)
if name.is_a?(Array)
register_traited_record(*name, obj, **options)
else
register_default_record(name, obj, **options)
end
obj
end
|
.remove(name) ⇒ Object
276
277
278
|
# File 'lib/test_prof/factory_default.rb', line 276
def remove(name)
store.delete(name)
end
|
.reset(context: nil) ⇒ Object
280
281
282
283
284
285
286
|
# File 'lib/test_prof/factory_default.rb', line 280
def reset(context: nil)
return store.clear unless context
store.delete_if do |_name, metadata|
metadata[:context] == context
end
end
|