Class: FactoryBotCaching::FactoryRecordCache
- Inherits:
-
Object
- Object
- FactoryBotCaching::FactoryRecordCache
- Defined in:
- lib/factory_bot_caching/factory_record_cache.rb
Defined Under Namespace
Classes: CacheEntry
Instance Method Summary collapse
- #fetch(overrides:, traits:, &block) ⇒ Object
-
#initialize(build_class:) ⇒ FactoryRecordCache
constructor
A new instance of FactoryRecordCache.
- #reset_counters ⇒ Object
Constructor Details
#initialize(build_class:) ⇒ FactoryRecordCache
Returns a new instance of FactoryRecordCache.
32 33 34 35 36 37 38 |
# File 'lib/factory_bot_caching/factory_record_cache.rb', line 32 def initialize(build_class:) @build_class = build_class @cache = Hash.new do |factory_hash, key| factory_hash[key] = [] end reset_counters end |
Instance Method Details
#fetch(overrides:, traits:, &block) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/factory_bot_caching/factory_record_cache.rb', line 40 def fetch(overrides:, traits:, &block) key = {overrides: overrides, traits: traits} now = Time.now enumerator = enumerator_for(key, at: now) enumerator.until_end do |entry| # Entries are sorted by created at, so we can break as soon as we see an entry created_at after now break if entry.created_at > now record = build_class.find_by(build_class.primary_key => entry.identifier) return record unless record.nil? end cache_new_record(key, &block) end |
#reset_counters ⇒ Object
55 56 57 58 59 |
# File 'lib/factory_bot_caching/factory_record_cache.rb', line 55 def reset_counters @enumerator_cache = Hash.new do |enumerator_hash, key| enumerator_hash[key] = ImmutableIterator.new(cache[key]) end end |