Project

tphases

0.0
No commit activity in last 3 years
No release in over 3 years
TPhases (Transactional Phases) is a support framework that helps you build your Rails request life cycles into read-only and write-only phases.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

TPhases

Build Status Code Climate

TPhases (Transactional Phases) is a support framework that helps you isolate database transactional code in your Ruby app by providing read-only, write-only, and no-transaction-allowed phases.

The way it accomplishes this is with the methods TPhases.read_phase, TPhases.write_phase and TPhases.no_transactions_phase which take blocks. Here is a simple example inside of a controller action:

class BarsController < ApplicationController

  def update
    TPhases.read_phase do
      @bar = Bar.find(params[:id])
    end
    
    TPhases.write_phase do
      @bar.update_attributes(params[:bar])
    end

    TPhases.no_transactions_phase do
      process(@bar)
      redirect_to @bar
    end
  end
end

** Currently supports ActiveRecord only. Planned support includes Memcached, Redis, MongoDB, and more.

Installation

Add this line to your application's Gemfile:

gem 'tphases'

And then execute:

$ bundle

Or install it yourself as:

$ gem install tphases

Somewhere in the initialization process of your app, call

TPhases.initiate!

Usage

Environments

In production:

The read_phase, write_phase, and no_transaction_phase methods simply yield to the block given.

In development:

read_phase

throws an exception if a database transaction is attempted within its block which is a write. This is known as a "read transactional violation".

write_phase

throws an exception if a database transaction is attempted within its block which is a read. This is a write transactional violation.

no_transactions_phase

throws an exception if any database transaction is attempted within its block.

In test:

If a transactional violation occurs in a TPhase, the code will continue to run, but the test will fail at the end citing the list of transactional violations that occurred.

Methods

ensure_no_transactions_on

Call ensure_no_transactions_on on controllers to prevent DB transactions within the view and action of a controller action. e.g.:

class BarsController < ApplicationController

  ensure_no_transactions_on [:show,:update]
end

ignore_phases

Call ignore_phases to disable TPhases within the block passed

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request