Module: FactoryBotCache

Defined in:
lib/factory_bot_cache.rb,
lib/factory_bot_cache/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.cachesObject



16
17
18
# File 'lib/factory_bot_cache.rb', line 16

def caches
  @caches ||= {}
end

.clearObject



8
9
10
# File 'lib/factory_bot_cache.rb', line 8

def clear
  @caches = nil
end

.id_map_of(base_name) ⇒ Proc

Returns a Proc which returns mapped id for given argument.

Returns:

  • (Proc)

    a Proc which returns mapped id for given argument



27
28
29
# File 'lib/factory_bot_cache.rb', line 27

def id_map_of(base_name)
  lambda{|key| of(base_name)[key].id }
end

.name_for(base_name, key) ⇒ Object



12
13
14
# File 'lib/factory_bot_cache.rb', line 12

def name_for(base_name, key)
  "#{base_name}_#{key}"
end

.of(base_name) ⇒ Object



20
21
22
23
24
# File 'lib/factory_bot_cache.rb', line 20

def of(base_name)
  caches[base_name.to_sym] ||= Hash.new do |hash, key|
    hash[key] = FactoryBot.create( name_for(base_name, key) )
  end
end