Acting lets your objects play a role. But just for a specific duration. One of the main-concepts for a clean DCI implementation.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install acting
It is as simple as this:
class User include :: attr_accessor :name end user1 = User.new user2 = User.new module Positivist def speak; 'YES'; end end module Negativist def speak; 'NO'; end end .(user1 => Positivist, user2 => Negativist). do user1.speak # => 'YES' user2.speak # => 'NO' end user1.speak # => NoMethodError
Different actings can also be nested, the defined role-methods will be safely overridden and restored:
# assume the already defined constants user = User.new .(user => Positivist). do user.speak # => 'YES' .(user => Negativist). 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 = .(user => Positivist) user.speak # => NoMethodError acting.play user.speak # => 'YES' acting.quit user.speak # => NoMethodError
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.
- 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