Sha256: ede90bbe3a979888bf72f5c7559bc605308f25dd7a4ded74de3fff58e3ae80c4

Contents?: true

Size: 882 Bytes

Versions: 3

Compression:

Stored size: 882 Bytes

Contents

require 'rack'
module DataMapper
  class RestfulTransactions

    class Rollback < StandardError; end

    def initialize(app, name = :default)
      @app = app
      @name = name.to_sym
    end
  
    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 [301, 302, 303, 307].include?(status)
          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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rack-datamapper-0.2.4 lib/rack_datamapper/restful_transactions.rb
rack-datamapper-0.2.3 lib/rack_datamapper/restful_transactions.rb
rack-datamapper-0.2.2 lib/rack_datamapper/restful_transactions.rb