Class: FactoryBotCaching::FactoryCache

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_bot_caching/factory_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factory_name:) ⇒ FactoryCache

Returns a new instance of FactoryCache.



29
30
31
32
33
34
35
# File 'lib/factory_bot_caching/factory_cache.rb', line 29

def initialize(factory_name:)
  @factory_name       = factory_name
  @build_class        = FactoryGirl.factory_by_name(factory_name).build_class
  @cache              = new_cache(@build_class)
  @cachable_overrides = []
  collect_uncachable_traits
end

Instance Attribute Details

#factory_nameObject (readonly)

Returns the value of attribute factory_name.



37
38
39
# File 'lib/factory_bot_caching/factory_cache.rb', line 37

def factory_name
  @factory_name
end

Instance Method Details

#fetch(overrides:, traits:, &block) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/factory_bot_caching/factory_cache.rb', line 39

def fetch(overrides:, traits:, &block)
  key = { overrides: overrides, traits: traits}
  if should_cache?(key)
    cache.fetch(key, &block)
  else
    block.call
  end
end

#resetObject



48
49
50
# File 'lib/factory_bot_caching/factory_cache.rb', line 48

def reset
  @cache = new_cache(build_class)
end

#reset_counterObject



52
53
54
# File 'lib/factory_bot_caching/factory_cache.rb', line 52

def reset_counter
  cache.reset_counters
end