Roleplay

Add support for 'roles' to Ruby classes

Installation

Add this line to your application's Gemfile:

gem 'roleplay'

And then execute:

$ bundle

Or install it yourself as:

$ gem install roleplay

Usage

Create a folder anywhere in the app directory (ex. app/roles/) Create modules of similar methods that define roles (ex. Buyer, Seller might be roles) Set up the model:

class User < ActiveRecord::Base
  has_role :buyer, using: Buyer
  has_role :seller, using: [Seller, Payable]
end

Now, instead of User.new, you can use User.new_with_role(:buyer) to create an instance that includes only the modules specified in the role definition. If you already have an instance, user.playing(:buyer) or user.as_a(:buyer) will both return duped instances with the requested modules included. Adding a bang will modify the original instance.

Contributing

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