Module: TestProf::FactoryDefault

Extended by:
Logging
Defined in:
lib/test_prof/factory_default.rb,
lib/test_prof/factory_default/fabrication_patch.rb,
lib/test_prof/factory_default/factory_bot_patch.rb

Overview

:nodoc: all

Defined Under Namespace

Modules: FabricationPatch, FactoryBotPatch 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_contextObject

Returns the value of attribute current_context.



169
170
171
# File 'lib/test_prof/factory_default.rb', line 169

def current_context
  @current_context
end

.profilerObject (readonly)

Returns the value of attribute profiler.



170
171
172
# File 'lib/test_prof/factory_default.rb', line 170

def profiler
  @profiler
end

.statsObject (readonly)

Returns the value of attribute stats.



170
171
172
# File 'lib/test_prof/factory_default.rb', line 170

def stats
  @stats
end

Class Method Details

.configObject



181
182
183
# File 'lib/test_prof/factory_default.rb', line 181

def config
  @config ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields:



185
186
187
# File 'lib/test_prof/factory_default.rb', line 185

def configure
  yield config
end

.disable!Object



273
274
275
276
277
278
279
280
# File 'lib/test_prof/factory_default.rb', line 273

def disable!
  was_enabled = @enabled
  @enabled = false
  return unless block_given?
  yield
ensure
  @enabled = was_enabled
end

.enable!Object



264
265
266
267
268
269
270
271
# File 'lib/test_prof/factory_default.rb', line 264

def enable!
  was_enabled = @enabled
  @enabled = true
  return unless block_given?
  yield
ensure
  @enabled = was_enabled
end

.enabled?Boolean

Returns:

  • (Boolean)


260
261
262
# File 'lib/test_prof/factory_default.rb', line 260

def enabled?
  @enabled
end

.get(name, traits = nil, overrides = nil, skip_stats: false) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/test_prof/factory_default.rb', line 209

def get(name, traits = nil, overrides = nil, skip_stats: false)
  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 unless skip_stats

  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) # rubocop:disable Lint/NonLocalExitFromIterator
      return if object.public_send(name) != value # rubocop:disable Lint/NonLocalExitFromIterator
    end
  end

  unless skip_stats
    stats[name][:miss] -= 1
    stats[name][:hit] += 1
  end

  if record[:context] && (record[:context] != :example)
    object.refind
  else
    object
  end
end

.initObject



172
173
174
175
176
177
178
179
# File 'lib/test_prof/factory_default.rb', line 172

def init
  FactoryBotPatch.patch
  FabricationPatch.patch

  @profiler = config.profiling_enabled? ? Profiler.new : NoopProfiler.new
  @enabled = ENV["FACTORY_DEFAULT_DISABLED"] != "1"
  @stats = {}
end

.preserve_attributes=(val) ⇒ Object



194
195
196
# File 'lib/test_prof/factory_default.rb', line 194

def preserve_attributes=(val)
  config.preserve_attributes = val
end

.preserve_traits=(val) ⇒ Object

TODO(v2): drop



190
191
192
# File 'lib/test_prof/factory_default.rb', line 190

def preserve_traits=(val)
  config.preserve_traits = val
end


282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/test_prof/factory_default.rb', line 282

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



198
199
200
201
202
203
204
205
206
207
# File 'lib/test_prof/factory_default.rb', line 198

def register(name, obj, **options)
  # Name with traits
  if name.is_a?(Array)
    register_traited_record(*name, obj, **options)
  else
    register_default_record(name, obj, **options)
  end

  obj
end

.remove(name) ⇒ Object



248
249
250
# File 'lib/test_prof/factory_default.rb', line 248

def remove(name)
  store.delete(name)
end

.reset(context: nil) ⇒ Object



252
253
254
255
256
257
258
# File 'lib/test_prof/factory_default.rb', line 252

def reset(context: nil)
  return store.clear unless context

  store.delete_if do |_name, |
    [:context] == context
  end
end