Sha256: a9dafc10f38147b242067c3babeaad95f74eb40733c89b2daa1bdc2d43d722b7

Contents?: true

Size: 876 Bytes

Versions: 4

Compression:

Stored size: 876 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 (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
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rack-datamapper-0.3.3 lib/rack_datamapper/restful_transactions.rb
rack-datamapper-0.3.2 lib/rack_datamapper/restful_transactions.rb
rack-datamapper-0.2.6 lib/rack_datamapper/restful_transactions.rb
rack-datamapper-0.2.5 lib/rack_datamapper/restful_transactions.rb