class Synapse::UnitOfWork::UnitOfWorkProvider

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

Public Class Methods

new() click to toggle source

@return [undefined]

# File lib/synapse/uow/provider.rb, line 7
def initialize
  @threads = Hash.new
end

Public Instance Methods

clear(unit) click to toggle source

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.

@param [UnitOfWork] unit @return [undefined]

# File lib/synapse/uow/provider.rb, line 18
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
commit() click to toggle source

Commits the current unit of work @return [undefined]

# File lib/synapse/uow/provider.rb, line 28
def commit
  current.commit
end
current() click to toggle source

Returns the current unit of work if one is set

@raise [RuntimeError] If no unit of work is active @return [UnitOfWork]

# File lib/synapse/uow/provider.rb, line 36
def current
  if stack.empty?
    raise 'No unit of work is active'
  end

  stack.last
end
push(unit) click to toggle source

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.

@param [UnitOfWork] unit @return [undefined]

# File lib/synapse/uow/provider.rb, line 51
def push(unit)
  stack.push unit
end
started?() click to toggle source

Returns true if there is an active unit of work @return [Boolean]

# File lib/synapse/uow/provider.rb, line 57
def started?
  !stack.empty?
end

Private Instance Methods

stack() click to toggle source

@return [Array<UnitOfWork>]

# File lib/synapse/uow/provider.rb, line 64
def stack
  @threads.fetch Thread.current
rescue KeyError
  @threads.store Thread.current, Array.new
end