Module: TypedEAV::BulkWrite

Defined in:
lib/typed_eav/bulk_write.rb

Overview

Internal executor for host-class bulk typed-value writes.

Host models keep the public ‘bulk_set_typed_eav_values` API; this module owns the transaction shape, savepoint isolation, error aggregation, field name resolution delegation, and version-group stamping.

Class Method Summary collapse

Class Method Details

.apply_record_save(record:, vbn:, effective_grouping:, uuids:, accumulator:) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/typed_eav/bulk_write.rb', line 26

def apply_record_save(record:, vbn:, effective_grouping:, uuids:, accumulator:)
  push_uuid = case effective_grouping
              when :per_record then uuids[:record]
              when :per_field  then uuids[:field].values.first
              end

  do_save = lambda do
    record.typed_eav_attributes = vbn.map { |name, value| { name: name, value: value } }
    stamp_pending_version_group_ids(record, effective_grouping, uuids)

    if record.save
      accumulator[:successes] << record
    else
      accumulator[:errors_by_record][record] = record.errors.messages.transform_keys(&:to_s)
      raise ActiveRecord::Rollback
    end
  end

  if push_uuid
    TypedEAV.with_context(version_group_id: push_uuid, &do_save)
  else
    do_save.call
  end
end

.execute(host_class:, records:, values_by_field_name:, version_grouping: :default) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/typed_eav/bulk_write.rb', line 11

def execute(host_class:, records:, values_by_field_name:, version_grouping: :default)
  validate_inputs!(records, values_by_field_name, version_grouping)

  records = records.to_a
  return { successes: [], errors_by_record: {} } if records.empty?

  validate_record_classes!(host_class, records)

  effective_grouping = resolve_grouping(version_grouping)
  vbn = values_by_field_name.transform_keys(&:to_s)
  field_uuids = effective_grouping == :per_field ? vbn.keys.index_with { SecureRandom.uuid } : nil

  execute_records(records, vbn, effective_grouping, field_uuids)
end