Class: OccamsRecord::EagerLoaders::AdHocBase
- Inherits:
-
Object
- Object
- OccamsRecord::EagerLoaders::AdHocBase
- Includes:
- Builder
- Defined in:
- lib/occams-record/eager_loaders/ad_hoc_base.rb
Overview
Base class for eager loading ad hoc associations.
Instance Attribute Summary collapse
- #eager_loaders ⇒ OccamsRecord::EagerLoaders::Context readonly
-
#name ⇒ String
readonly
Association name.
-
#tracer ⇒ OccamsRecord::EagerLoaders::Tracer | nil
readonly
A reference to this eager loader and its parent (if any).
Instance Method Summary collapse
-
#initialize(name, mapping, sql, binds: {}, model: nil, use: nil, parent: nil) { ... } ⇒ AdHocBase
constructor
Initialize a new add hoc association.
-
#run(rows, query_logger: nil, measurements: nil) ⇒ Object
Run the query and merge the results into the given rows.
Methods included from Builder
#eager_load, #eager_load_many, #eager_load_one, #nest
Constructor Details
#initialize(name, mapping, sql, binds: {}, model: nil, use: nil, parent: nil) { ... } ⇒ AdHocBase
Initialize a new add hoc association.
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/occams-record/eager_loaders/ad_hoc_base.rb', line 32 def initialize(name, mapping, sql, binds: {}, model: nil, use: nil, parent: nil, &builder) @name, @mapping = name.to_s, mapping @sql, @binds, @use, @model = sql, binds, use, model @tracer = Tracer.new(name, parent) @eager_loaders = EagerLoaders::Context.new(@model, tracer: @tracer) if builder if builder.arity > 0 builder.call(self) else instance_exec(&builder) end end end |
Instance Attribute Details
#eager_loaders ⇒ OccamsRecord::EagerLoaders::Context (readonly)
18 19 20 |
# File 'lib/occams-record/eager_loaders/ad_hoc_base.rb', line 18 def eager_loaders @eager_loaders end |
#name ⇒ String (readonly)
Returns association name.
12 13 14 |
# File 'lib/occams-record/eager_loaders/ad_hoc_base.rb', line 12 def name @name end |
#tracer ⇒ OccamsRecord::EagerLoaders::Tracer | nil (readonly)
Returns a reference to this eager loader and its parent (if any).
15 16 17 |
# File 'lib/occams-record/eager_loaders/ad_hoc_base.rb', line 15 def tracer @tracer end |
Instance Method Details
#run(rows, query_logger: nil, measurements: nil) ⇒ Object
Run the query and merge the results into the given rows.
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/occams-record/eager_loaders/ad_hoc_base.rb', line 52 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 |