Class: Rack::PactBroker::DatabaseTransaction

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/pact_broker/database_transaction.rb

Constant Summary collapse

REQUEST_METHOD =
"REQUEST_METHOD".freeze
TRANS_METHODS =
%{POST PUT PATCH DELETE}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, database_connection) ⇒ DatabaseTransaction

Returns a new instance of DatabaseTransaction.



11
12
13
14
# File 'lib/rack/pact_broker/database_transaction.rb', line 11

def initialize app, database_connection
  @app = app
  @database_connection = database_connection
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/rack/pact_broker/database_transaction.rb', line 16

def call env
  if use_transaction? env
    call_with_transaction env
  else
    call_without_transaction env
  end
end

#call_with_transaction(env) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/rack/pact_broker/database_transaction.rb', line 32

def call_with_transaction env
  response = nil
  @database_connection.transaction do
    response = @app.call(env)
    raise Sequel::Rollback if response.first == 500
  end
  response
end

#call_without_transaction(env) ⇒ Object



28
29
30
# File 'lib/rack/pact_broker/database_transaction.rb', line 28

def call_without_transaction env
  @app.call(env)
end

#use_transaction?(env) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/rack/pact_broker/database_transaction.rb', line 24

def use_transaction? env
  TRANS_METHODS.include? env[REQUEST_METHOD]
end