Class: Imap::Backup::Serializer::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/imap/backup/serializer/transaction.rb

Overview

Stores data during a transaction

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner:) ⇒ Transaction

Returns a new instance of Transaction.

Parameters:

  • owner (any)

    the class using the transaction - this is used when raising errors



13
14
15
16
17
# File 'lib/imap/backup/serializer/transaction.rb', line 13

def initialize(owner:)
  @data = nil
  @owner = owner
  @in_transaction = false
end

Instance Attribute Details

#dataObject (readonly)

Returns the transaction’s stored data.

Returns:

  • the transaction’s stored data



9
10
11
# File 'lib/imap/backup/serializer/transaction.rb', line 9

def data
  @data
end

Instance Method Details

#begin(data, &block) ⇒ void

This method returns an undefined value.

Runs the transaction

Parameters:

  • data (any)

    the data to maintain during the transaction

  • block (block)

    the block to wrap with the transaction



23
24
25
26
27
28
# File 'lib/imap/backup/serializer/transaction.rb', line 23

def begin(data, &block)
  @data = data
  @in_transaction = true
  block.call
  @in_transaction = false
end

#clearvoid

This method returns an undefined value.

Clears rollback data



32
33
34
# File 'lib/imap/backup/serializer/transaction.rb', line 32

def clear
  @data = nil
end

#fail_in_transaction!(method, message: "not supported inside trasactions") ⇒ void

This method returns an undefined value.

Throws an exception if there is a current transaction

Parameters:

  • method (Symbol)

    the method where the check is run

Raises:

  • (RuntimeError)

    if called from inside a transaction



44
45
46
# File 'lib/imap/backup/serializer/transaction.rb', line 44

def fail_in_transaction!(method, message: "not supported inside trasactions")
  raise "#{owner.class}##{method} #{message}" if in_transaction?
end

#fail_outside_transaction!(method) ⇒ void

This method returns an undefined value.

Throws an exception if there is not a current transaction

Parameters:

  • method (Symbol)

    the method where the check is run

Raises:

  • (RuntimeError)

    if called from outside a transaction



52
53
54
# File 'lib/imap/backup/serializer/transaction.rb', line 52

def fail_outside_transaction!(method)
  raise "#{owner.class}##{method} can only be called inside a transaction" if !in_transaction?
end

#in_transaction?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/imap/backup/serializer/transaction.rb', line 36

def in_transaction?
  @in_transaction
end