Class: OccamsRecord::EagerLoaders::AdHocBase
- Inherits:
-
Object
- Object
- OccamsRecord::EagerLoaders::AdHocBase
- 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.
Constructor Details
#initialize(name, mapping, sql, binds: {}, model: nil, use: nil) { ... } ⇒ AdHocBase
Initialize a new add hoc association.
21 22 23 24 25 26 27 |
# File 'lib/occams-record/eager_loaders/ad_hoc_base.rb', line 21 def initialize(name, mapping, sql, binds: {}, model: nil, use: nil, &eval_block) @name = name.to_s @sql, @binds, @use, @model, @eval_block = sql, binds, use, model, eval_block 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) end |
Instance Attribute Details
#name ⇒ String (readonly)
Returns association name.
8 9 10 |
# File 'lib/occams-record/eager_loaders/ad_hoc_base.rb', line 8 def name @name end |
Instance Method Details
#run(rows, query_logger: nil) ⇒ Object
Run the query and merge the results into the given rows.
35 36 37 38 39 40 41 |
# File 'lib/occams-record/eager_loaders/ad_hoc_base.rb', line 35 def run(rows, query_logger: nil) calc_ids(rows) { |ids| binds = @binds.merge({:ids => ids}) assoc_rows = RawQuery.new(@sql, binds, use: @use, query_logger: query_logger, &@eval_block).model(@model).run merge! assoc_rows, rows } end |