Class: TypedEAV::BulkRead
- Inherits:
-
Object
- Object
- TypedEAV::BulkRead
- Defined in:
- lib/typed_eav/bulk_read.rb
Overview
Bulk-read query object. Returns { record_id => { field_name => value } }
for an Enumerable of host records — the class-method bulk variant of
HasTypedEAV::InstanceMethods#typed_eav_hash. N+1-free regardless of
record count or field count.
Pipeline (one batched definition query + one bulk value preload)
1. validate_records! — nil -> ArgumentError; single-class invariant
2. group_by_tuple — `[typed_eav_scope, typed_eav_parent_scope]`
3. winning_ids_by_tuple — one exact-partition definition query, then
`Partition.definitions_by_name` per tuple
4. preload_values — single SELECT across ALL records
5. build_result_hash — per-record inner hash; orphan-skip + winning-id
precedence mirrored from the instance path.
Query bound
- 1 SELECT typed_eav_values WHERE entity_type=? AND entity_id IN (?)
- 1 SELECT typed_eav_fields WHERE id IN (?) (via includes)
- 1 SELECT typed_eav_fields for the union of requested partitions
Total: 3 queries — independent of record count and partition cardinality.
Single-class invariant
The polymorphic value query (entity_type: host_class.name) targets ONE
class; mixed-class input would silently miss rows of the other class. STI
subclasses pass via records.all?(host_class).
Instance Method Summary collapse
-
#initialize(host_class:, records:) ⇒ BulkRead
constructor
A new instance of BulkRead.
- #to_hash ⇒ Object
Constructor Details
#initialize(host_class:, records:) ⇒ BulkRead
Returns a new instance of BulkRead.
33 34 35 36 |
# File 'lib/typed_eav/bulk_read.rb', line 33 def initialize(host_class:, records:) @host_class = host_class @records = records end |
Instance Method Details
#to_hash ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/typed_eav/bulk_read.rb', line 38 def to_hash records = coerce_records return {} if records.empty? validate_record_classes!(records) tuples_by_record = group_by_tuple(records) winning_ids_by_tuple = winning_ids_by_tuple(tuples_by_record.values.uniq) values_by_record_id = preload_values(records) build_result(records, tuples_by_record, winning_ids_by_tuple, values_by_record_id) end |