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

Defined in:
foobara-0.0.110/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.



7
8
9
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 7

def records
  @records
end

#tracked_recordsObject

Returns the value of attribute tracked_records.



7
8
9
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 7

def tracked_records
  @tracked_records
end

Instance Method Details

#all_hard_deletedObject



59
60
61
62
63
64
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 59

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

#created(record) ⇒ Object



36
37
38
39
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 36

def created(record)
  tracked_records << record
  mark_created(record)
end

#hard_deleted(record) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 41

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

#initializeObject



9
10
11
12
13
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 9

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

#loading(record) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 15

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



95
96
97
98
99
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 95

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

#rolled_backObject



88
89
90
91
92
93
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 88

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

#tracked(record) ⇒ Object



32
33
34
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 32

def tracked(record)
  tracked_records << record
end

#unhard_deleted(record) ⇒ Object



51
52
53
54
55
56
57
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb', line 51

def unhard_deleted(record)
  unmark_hard_deleted(record)

  if record.dirty?
    mark_updated(record)
  end
end

#updated(record) ⇒ Object



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

def updated(record)
  # Hacky way to handle situation where the primary key might have changed.
  # TODO: make a better way of handling this.
  tracked_records.delete(record)
  tracked_records << 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