Class: ActiveRecordCustomPreloader::Preloader

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_custom_preloader/preloader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, name, args) ⇒ Preloader

Returns a new instance of Preloader.



7
8
9
10
11
# File 'lib/active_record_custom_preloader/preloader.rb', line 7

def initialize(klass, name, args)
  @klass = klass
  @name = name
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



5
6
7
# File 'lib/active_record_custom_preloader/preloader.rb', line 5

def args
  @args
end

#klassObject (readonly)

Returns the value of attribute klass.



5
6
7
# File 'lib/active_record_custom_preloader/preloader.rb', line 5

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/active_record_custom_preloader/preloader.rb', line 5

def name
  @name
end

Instance Method Details

#preload(records) ⇒ Object

override this method in a subclass to provide customized preloading behavior



14
15
16
17
18
19
20
# File 'lib/active_record_custom_preloader/preloader.rb', line 14

def preload(records)
  values = args.fetch(:values).call(records)
  records.each do |record|
    value = values[record.id]
    record._set_custom_preloaded_value(name, value)
  end
end