Class: Banzai::LazyReference

Inherits:
Object
  • Object
show all
Defined in:
lib/banzai/lazy_reference.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, ids) ⇒ LazyReference


16
17
18
19
# File 'lib/banzai/lazy_reference.rb', line 16

def initialize(klass, ids)
  @klass = klass
  @ids = Array.wrap(ids).map(&:to_i)
end

Instance Attribute Details

#idsObject (readonly)

Returns the value of attribute ids


14
15
16
# File 'lib/banzai/lazy_reference.rb', line 14

def ids
  @ids
end

#klassObject (readonly)

Returns the value of attribute klass


14
15
16
# File 'lib/banzai/lazy_reference.rb', line 14

def klass
  @klass
end

Class Method Details

.load(refs) ⇒ Object


3
4
5
6
7
8
9
10
11
12
# File 'lib/banzai/lazy_reference.rb', line 3

def self.load(refs)
  lazy_references, values = refs.partition { |ref| ref.is_a?(self) }

  lazy_values = lazy_references.group_by(&:klass).flat_map do |klass, refs|
    ids = refs.flat_map(&:ids)
    klass.where(id: ids)
  end

  values + lazy_values
end

Instance Method Details

#loadObject


21
22
23
# File 'lib/banzai/lazy_reference.rb', line 21

def load
  self.klass.where(id: self.ids)
end