Class: DataMapper::RestfulTransactions

Inherits:
Object
  • Object
show all
Defined in:
lib/rack_datamapper/restful_transactions.rb

Defined Under Namespace

Classes: Rollback

Instance Method Summary collapse

Constructor Details

#initialize(app, name = :default) ⇒ RestfulTransactions

Returns a new instance of RestfulTransactions.



7
8
9
10
# File 'lib/rack_datamapper/restful_transactions.rb', line 7

def initialize(app, name = :default)
  @app = app
  @name = name.to_sym
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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