Browse the Ruby on Rails Community.

You are here: Browse Railsplugins Simple Services

Simple Services

Creates a simple services layer with automatic transactional demarcation. To use, simply create a folder at app/services, and add classes that derive from Service::Base. That’s it. All the methods in the service will be wrapped in a transaction by default. Any exception within that method will rollback the transaction.

Services are singletons to reduce memory overhead. And since they’re singletons, they MUST remain stateless.

Dependencies

SimpleServices relies on Aquarium for method aspecting.

sudo gem install aquarium

Example

  1. in RAILS_ROOT/app/services class AccountService < Service::Base

    def transfer(base_acct, target_acct, amt)

    base_acct.balance -= amt
    base_acct.save!
    target_acct.balance += amt
    target_acct.save!

    end

end
class AccountController &lt; ApplicationController
  1. adds two methods to instances of this controller:
  2. account_service
  3. user_service services :account, :user
def update
  base = Account.find(params[:base_id])
  target = Account.find(params[:target_id])
  # use service.  enjoy transactions.  lather. rinse. repeat.
  account_service.transfer(base, target, params[:amount])
end
end

Copyright© 2008 Relevance, Inc., released under the MIT license

NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly

Users


See all details


Membership

+ Join this railsplugin

Record Maintainer

'None'