Module: Foobara::Persistence::EntityBase::TransactionTable::Concerns::RecordTracking

Defined in:
foobara-0.5.8/projects/entities/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#recordsObject

Returns the value of attribute records.



9
10
11
# File 'projects/entities/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 9

def records
  @records
end

#tracked_recordsObject

Returns the value of attribute tracked_records.



9
10
11
# File 'projects/entities/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 9

def tracked_records
  @tracked_records
end

Instance Method Details

#all_hard_deletedObject



60
61
62
63
64
65
# File 'projects/entities/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 60

def all_hard_deleted
  tracked_records.clear
  marked_hard_deleted.clear
  marked_updated.clear
  marked_created.clear
end

#closedObject



98
99
100
101
102
103
# File 'projects/entities/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 98

def closed
  marked_hard_deleted.clear
  marked_updated.clear
  marked_created.clear
  marked_loading.clear
end

#committedObject



90
91
92
93
94
95
96
# File 'projects/entities/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 90

def committed
  marked_persisted.each do |record|
    record.fire(:persisted)
  end

  closed
end

#created(record) ⇒ Object



38
39
40
41
# File 'projects/entities/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 38

def created(record)
  tracked(record)
  mark_created(record)
end

#hard_deleted(record) ⇒ Object



43
44
45
46
47
48
49
50
# File 'projects/entities/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 43

def hard_deleted(record)
  if record.persisted?
    mark_hard_deleted(record)
    unmark_updated(record)
  else
    unmark_created(record)
  end
end

#initializeObject



11
12
13
14
15
# File 'projects/entities/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 11

def initialize
  @records = {}
  self.tracked_records = WeakObjectSet.new(entity_class.primary_key_attribute)
  super
end

#loading(record) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'projects/entities/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 17

def loading(record)
  if loading?(record)
    # :nocov:
    raise "Already loading #{record}"
    # :nocov:
  end

  begin
    mark_loading(record)
    yield
  ensure
    unmark_loading(record)
  end

  record
end

#revertedObject



112
113
114
115
116
# File 'projects/entities/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 112

def reverted
  marked_hard_deleted.clear
  marked_updated.clear
  marked_created.clear
end

#rolled_backObject



86
87
88
# File 'projects/entities/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 86

def rolled_back
  closed
end

#tracked(record) ⇒ Object



34
35
36
# File 'projects/entities/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 34

def tracked(record)
  tracked_records << record
end

#transaction_closedObject

We need to clear this one separately. That’s because otherwise a different table might flush and create a thunk if it has an association to this table but we’ve stopped tracking the record.



108
109
110
# File 'projects/entities/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 108

def transaction_closed
  tracked_records.close
end

#unhard_deleted(record) ⇒ Object



52
53
54
55
56
57
58
# File 'projects/entities/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 52

def unhard_deleted(record)
  unmark_hard_deleted(record)

  if record.dirty?
    mark_updated(record)
  end
end

#updated(record) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'projects/entities/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 67

def updated(record)
  tracked(record)

  # TODO: is this check redundant? Maybe have the entity explode directly instead?
  if hard_deleted?(record)
    # :nocov:
    raise "Cannot update a hard deleted record"
    # :nocov:
  end

  unless created?(record)
    if record.dirty?
      mark_updated(record)
    else
      unmark_updated(record)
    end
  end
end