Module: Foobara::Persistence::EntityBase::TransactionTable::Concerns::Queries

Defined in:
foobara-0.0.130/projects/persistence/src/entity_base/transaction_table/concerns/queries.rb

Overview

If something accesses the crud driver and manipulates records then it belongs in this concern.

Instance Method Summary collapse

Instance Method Details

#allObject

TODO: why is this query method here but the rest are not?



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'foobara-0.0.130/projects/persistence/src/entity_base/transaction_table/concerns/queries.rb', line 9

def all(&)
  enumerator = Enumerator.new do |yielder|
    tracked_records.each do |record|
      # if the record is not loaded, it could be an unloaded thunk with a primary key to a row that has
      # been deleted or maybe never even existed. So just exclude those and let them come from the
      # database in the next loop
      if !record.hard_deleted? && (created?(record) || record.loaded?)
        yielder << record
      end
    end

    entity_attributes_crud_driver_table.all.each do |attributes|
      attributes = normalize_attributes(attributes)
      primary_key = primary_key_for_attributes(attributes)

      record = tracked_records.find_by_key(primary_key)
      if record
        next if record.hard_deleted?
        next if created?(record)

        unless record.loaded?
          load(record)
          yielder << record
          next
        end

        next
      end

      yielder << entity_class.loaded(attributes)
    end
  end

  if block_given?
    enumerator.each(&)
  else
    enumerator.to_a
  end
end