Class: Foobara::Persistence::EntityBase::Transaction

Inherits:
Object
  • Object
show all
Includes:
Concerns::EntityCallbackHandling, Concerns::StateTransitions, Concerns::TransactionTracking
Defined in:
foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb,
foobara-0.0.110/projects/persistence/src/entity_base/transaction/state_machine.rb,
foobara-0.0.110/projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb,
foobara-0.0.110/projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb,
foobara-0.0.110/projects/persistence/src/entity_base/transaction/concerns/entity_callback_handling.rb

Defined Under Namespace

Modules: Concerns Classes: CurrentTransactionIsClosedError, NoCurrentTransactionError, RolledBack, StateMachine

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity_base) ⇒ Transaction

Returns a new instance of Transaction.



11
12
13
14
15
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 11

def initialize(entity_base)
  self.entity_base = entity_base
  self.state_machine = StateMachine.new(owner: self)
  self.tables = {}
end

Instance Attribute Details

#entity_baseObject

Returns the value of attribute entity_base.



9
10
11
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 9

def entity_base
  @entity_base
end

#raw_txObject

Returns the value of attribute raw_tx.



9
10
11
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 9

def raw_tx
  @raw_tx
end

#state_machineObject

Returns the value of attribute state_machine.



9
10
11
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 9

def state_machine
  @state_machine
end

#tablesObject

Returns the value of attribute tables.



9
10
11
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 9

def tables
  @tables
end

Instance Method Details

#closed?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 41

def closed?
  state_machine.currently_closed?
end

#create(entity_class, attributes = {}) ⇒ Object



19
20
21
22
23
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 19

def create(entity_class, attributes = {})
  Persistence.to_base(entity_class).transaction(existing_transaction: self) do
    entity_class.create(attributes)
  end
end

#created?(record) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 152

def created?(record)
  table_for(record).created?(record)
end

#flush_created_record!(record) ⇒ Object

WARNING! this seems to bypass validations, hmmm.…



157
158
159
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 157

def flush_created_record!(record)
  table_for(record).flush_created_record!(record)
end

#hard_delete_all!(entity_class) ⇒ Object



84
85
86
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 84

def hard_delete_all!(entity_class)
  table_for(entity_class).hard_delete_all!
end

#hard_deleted(record) ⇒ Object



72
73
74
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 72

def hard_deleted(record)
  table_for(record).hard_deleted(record)
end

#load(record_or_entity_class, record_id = nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 88

def load(record_or_entity_class, record_id = nil)
  entity_or_id = if record_or_entity_class.is_a?(Entity)
                   if record_id
                     # :nocov:
                     raise ArgumentError, "Do not give a record_id when also giving a record"
                     # :nocov:
                   end

                   record_or_entity_class
                 else
                   unless record_id
                     # :nocov:
                     raise ArgumentError, "Must give a record_id when passing in an entity class"
                     # :nocov:
                   end

                   record_id
                 end

  table_for(record_or_entity_class).load(entity_or_id)
end

#load_aggregate(record) ⇒ Object



136
137
138
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 136

def load_aggregate(record)
  load_aggregates([record]).first
end

#load_aggregates(to_load_records) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 110

def load_aggregates(to_load_records)
  to_load = Set.new

  to_load_records.group_by(&:class).each_pair do |entity_class, records|
    unloaded = records.select { |record| record.persisted? && !record.loaded? }
    entity_class.current_transaction_table.load_many(unloaded)

    associations = entity_class.associations

    records.each do |record|
      associations.each_key do |data_path|
        # TODO: is there a more performant way to append an array to a set? probably.
        Foobara::DataPath.values_at(data_path, record).each do |value|
          to_load << value
        end
      end
    end
  end

  unless to_load.empty?
    load_aggregates(to_load)
  end

  to_load_records
end

#loaded(entity_class, attributes) ⇒ Object



31
32
33
34
35
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 31

def loaded(entity_class, attributes)
  Persistence.to_base(entity_class).transaction(existing_transaction: self) do
    entity_class.loaded(attributes)
  end
end

#loading?(record) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 45

def loading?(record)
  table_for(record).loading?(record)
end

#open?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 37

def open?
  state_machine.currently_open?
end

#performObject

convenience method…



162
163
164
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 162

def perform(&)
  entity_base.using_transaction(self, &)
end

#table_for(entity_class) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 53

def table_for(entity_class)
  if entity_class.is_a?(Entity)
    entity_class = entity_class.class
  end

  # TODO: so much passing self around...
  unless entity_base == entity_class.entity_base
    # :nocov:
    raise "#{entity_class} is from a different entity base! Cannot proceed."
    # :nocov:
  end

  tables[entity_class] ||= TransactionTable.new(self, entity_class)
end

#thunk(entity_class, record_id) ⇒ Object



25
26
27
28
29
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 25

def thunk(entity_class, record_id)
  Persistence.to_base(entity_class).transaction(existing_transaction: self) do
    entity_class.thunk(record_id)
  end
end

#track_created(entity) ⇒ Object



148
149
150
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 148

def track_created(entity)
  table_for(entity).track_created(entity)
end

#track_loaded(entity) ⇒ Object



144
145
146
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 144

def track_loaded(entity)
  table_for(entity).track_loaded(entity)
end

#track_unloaded_thunk(entity) ⇒ Object



140
141
142
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 140

def track_unloaded_thunk(entity)
  table_for(entity).track_unloaded_thunk(entity)
end

#tracking?(record) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 49

def tracking?(record)
  table_for(record).tracking?(record)
end

#truncate!Object



80
81
82
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 80

def truncate!
  each_table(&:hard_delete_all!)
end

#unhard_deleted(record) ⇒ Object



76
77
78
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 76

def unhard_deleted(record)
  table_for(record).unhard_deleted(record)
end

#updated(record) ⇒ Object



68
69
70
# File 'foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb', line 68

def updated(record)
  table_for(record).updated(record)
end