class DataMapper::RestfulTransactions

Public Class Methods

new(app, name = :default) click to toggle source
# File lib/rack_datamapper/restful_transactions.rb, line 7
def initialize(app, name = :default)
  @app = app
  @name = name.to_sym
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack_datamapper/restful_transactions.rb, line 12
def call(env)
  request = ::Rack::Request.new(env)
  if ["POST", "PUT", "DELETE"].include? request.request_method
    status, headers, response = nil, nil, nil
    begin
      transaction = DataMapper::Transaction.new(DataMapper.repository(@name))
      transaction.commit do
        status, headers, response = @app.call(env)
        raise Rollback unless (200 <= status && status < 400)
      end
    rescue Rollback 
      # ignore,
      # this is just needed to trigger the rollback on the transaction
    end
    [status, headers, response]
  else
    @app.call(env)        
  end
end