Class: OccamsRecord::EagerLoaders::AdHocBase

Inherits:
Object
  • Object
show all
Includes:
Builder
Defined in:
lib/occams-record/eager_loaders/ad_hoc_base.rb

Overview

Base class for eager loading ad hoc associations.

Direct Known Subclasses

AdHocMany, AdHocOne

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Builder

#eager_load, #eager_load_many, #eager_load_one, #nest

Constructor Details

#initialize(name, mapping, sql, binds: {}, model: nil, use: nil) { ... } ⇒ AdHocBase

Initialize a new add hoc association.

Yields:

  • eager load associations nested under this one



25
26
27
28
29
30
# File 'lib/occams-record/eager_loaders/ad_hoc_base.rb', line 25

def initialize(name, mapping, sql, binds: {}, model: nil, use: nil, &builder)
  @name, @mapping = name.to_s, mapping
  @sql, @binds, @use, @model = sql, binds, use, model
  @eager_loaders = EagerLoaders::Context.new(@model)
  instance_exec(&builder) if builder
end

Instance Attribute Details

#nameString (readonly)



12
13
14
# File 'lib/occams-record/eager_loaders/ad_hoc_base.rb', line 12

def name
  @name
end

Instance Method Details

#run(rows, query_logger: nil, measurements: nil) ⇒ Object

Run the query and merge the results into the given rows.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/occams-record/eager_loaders/ad_hoc_base.rb', line 38

def run(rows, query_logger: nil, measurements: nil)
  fkey_binds = calc_fkey_binds rows
  assoc = if fkey_binds.all? { |_, vals| vals.any? }
            binds = @binds.merge(fkey_binds)
            RawQuery.new(@sql, binds, use: @use, eager_loaders: @eager_loaders, query_logger: query_logger, measurements: measurements).run
          else
            []
          end
  merge! assoc, rows
  nil
end