Class: Synapse::UnitOfWork::UnitOfWorkProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/synapse/uow/provider.rb

Overview

Entry point for components to access units of work. Components managing transactional boundaries can register and clear unit of work instances.

Instance Method Summary collapse

Constructor Details

#initializeUnitOfWorkProvider

Returns a new instance of UnitOfWorkProvider.



6
7
8
# File 'lib/synapse/uow/provider.rb', line 6

def initialize
  @threads = Hash.new
end

Instance Method Details

#clear(unit) ⇒ undefined

Clears the given unit of work from this provider

If the given unit of work is not known to the provider, or it is not the active unit of work, then this method will raise an exception.

Parameters:

Returns:

  • (undefined)


17
18
19
20
21
22
23
# File 'lib/synapse/uow/provider.rb', line 17

def clear(unit)
  unless stack.last == unit
    raise ArgumentError, 'The given unit of work is not the active unit of work'
  end

  stack.pop
end

#commitundefined

Commits the current unit of work

Returns:

  • (undefined)


27
28
29
# File 'lib/synapse/uow/provider.rb', line 27

def commit
  current.commit
end

#currentUnitOfWork

Returns the current unit of work if one is set

Returns:

Raises:

  • (RuntimeError)

    If no unit of work is active



35
36
37
38
39
40
41
# File 'lib/synapse/uow/provider.rb', line 35

def current
  if stack.empty?
    raise 'No unit of work is active'
  end

  stack.last
end

#push(unit) ⇒ undefined

Pushes the given unit of work onto the top of the stack, making it the active unit of work

If there are other units of work bound to this provider, they will be held until the given unit of work is cleared.

Parameters:

Returns:

  • (undefined)


51
52
53
# File 'lib/synapse/uow/provider.rb', line 51

def push(unit)
  stack.push unit
end

#started?Boolean

Returns true if there is an active unit of work

Returns:

  • (Boolean)


57
58
59
# File 'lib/synapse/uow/provider.rb', line 57

def started?
  !stack.empty?
end