Class: ActiveRecordDataLoader::ActiveRecord::PerRowValueCache

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_data_loader/active_record/per_row_value_cache.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePerRowValueCache

Returns a new instance of PerRowValueCache.



22
23
24
# File 'lib/active_record_data_loader/active_record/per_row_value_cache.rb', line 22

def initialize
  @row_caches = Hash.new { |hash, key| hash[key] = {} }
end

Class Method Details

.[](key) ⇒ Object



7
8
9
# File 'lib/active_record_data_loader/active_record/per_row_value_cache.rb', line 7

def [](key)
  caches[key] ||= new
end

.clearObject



11
12
13
# File 'lib/active_record_data_loader/active_record/per_row_value_cache.rb', line 11

def clear
  @caches = {}
end

Instance Method Details

#get_or_set(model:, row:) ⇒ Object



26
27
28
29
30
# File 'lib/active_record_data_loader/active_record/per_row_value_cache.rb', line 26

def get_or_set(model:, row:)
  @row_caches[model.name].shift if @row_caches[model.name].size > 1

  @row_caches[model.name][row] ||= yield
end