Acting
Acting lets your objects play a role. But just for a specific duration. One of the main-concepts for a clean DCI implementation.
Installation
Add this line to your application's Gemfile:
gem 'acting'
And then execute:
$ bundle
Or install it yourself as:
$ gem install acting
Usage
It is as simple as this:
class User
include Acting::Cast
attr_accessor :name
end
user1 = User.new
user2 = User.new
module Positivist
def speak; 'YES'; end
end
module Negativist
def speak; 'NO'; end
end
Acting.new(user1 => Positivist, user2 => Negativist).play do
user1.speak # => 'YES'
user2.speak # => 'NO'
end
user1.speak # => NoMethodError
Nested Usage
Different actings can also be nested, the defined role-methods will be safely overridden and restored:
# assume the already defined constants
user = User.new
Acting.new(user => Positivist).play do
user.speak # => 'YES'
Acting.new(user => Negativist).play do
user.speak # => 'NO'
end
user.speak # => 'YES'
end
user.speak # => NoMethodError
Usage without a block
The acting can also be manually started/quit. This can come in handy if you need more control over the program flow or want to use the actual roles later:
# assume the already defined constants
user = User.new
acting = Acting.new(user => Positivist)
user.speak # => NoMethodError
acting.play
user.speak # => 'YES'
acting.quit
user.speak # => NoMethodError
TODO:
For now, we are only running on MRI 1.9.2/1.9.3/2.0.0. jRuby and RBX need to be configured.
Already defined methods won't be overridden due to the implementation with method_missing. It's to discuss whether this is a bug or a feature.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request