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
-
#name ⇒ String
readonly
Association name.
Instance Method Summary collapse
-
#initialize(name, mapping, sql, binds: {}, model: nil, use: nil) { ... } ⇒ AdHocBase
constructor
Initialize a new add hoc association.
-
#run(rows, query_logger: 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) { ... } ⇒ AdHocBase
Initialize a new add hoc association.
23 24 25 26 27 28 29 30 31 |
# File 'lib/occams-record/eager_loaders/ad_hoc_base.rb', line 23 def initialize(name, mapping, sql, binds: {}, model: nil, use: nil, &builder) @name = name.to_s @sql, @binds, @use, @model = sql, binds, use, model raise ArgumentError, "Add-hoc eager loading mapping must contain exactly one key-value pair" unless mapping.size == 1 @local_key = mapping.keys.first @foreign_key = mapping.fetch(@local_key) @eager_loaders = EagerLoaders::Context.new(@model) instance_eval(&builder) if builder end |
Instance Attribute Details
#name ⇒ String (readonly)
Returns association name.
10 11 12 |
# File 'lib/occams-record/eager_loaders/ad_hoc_base.rb', line 10 def name @name end |
Instance Method Details
#run(rows, query_logger: nil) ⇒ Object
Run the query and merge the results into the given rows.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/occams-record/eager_loaders/ad_hoc_base.rb', line 39 def run(rows, query_logger: nil) calc_ids(rows) { |ids| assoc = if ids.any? binds = @binds.merge({:ids => ids}) RawQuery.new(@sql, binds, use: @use, eager_loaders: @eager_loaders, query_logger: query_logger).run else [] end merge! assoc, rows } end |