dci-ruby
Trygve Reenskaug, the parent of MVC, proposes an evolution to traditional OO paradigm. (www.artima.com/articles/dci_vision.html). This gem makes Data-Context-Interaction paradigm ready to be used in your Ruby application. See also (rubysource.com/dci-the-evolution-of-the-object-oriented-paradigm/).
Installation
Install as usual, either with rubygems
gem install dci-ruby
or including it in your Gemfile and running bundle install:
# Gemfile
gem "dci-ruby"
Use
dci-ruby gives you the class DCI::Context to inherit from to create your own contexts:
class MoneyTransfer < DCI::Context
# Roles
role :source_account do
def transfer(amount)
player.balance -= amount
end
end
role :target_account do
def get_transfer(amount)
player.balance += amount
end
end
# Interactions
def run(amount=default_amount)
source_account.transfer(amount)
target_account.get_transfer(amount)
end
end
Every context defines some roles to be played by external objects (players) and their interactions. This way you have all the agents and operations in a user case wrapped in just one entity instead of spreaded throughout the application code.
Use the defined contexts, instantiating them wherever you need in your code:
MoneyTransfer.new(:source_account => Account.new(1),
:target_account => Account.new(2)).run(100)
In a context instance, every role instanciates an object (roleplayer) that gathers the behaviour defined inside its role, and that roleplayer object has private access to the original object adopting the role (player): the Account instances above are players. Instances of MoneyTransfer::SourceAccount and MoneyTransfer::TargetAccount accessible inside MoneyTransfer.new via the private instance_methods #source_account and #target_account are roleplayers. Also, every roleplayer has private access to the rest of roleplayers in its context.
When instanciating a Context, the extra no-role pairs given as arguments are read-only attributes accessible inside the instance:
MoneyTransfer.new(:source_account => Account.new(1),
:target_account => Account.new(2),
:default_amount => 500).run
here, default_amount is not a player (has no associated role) but is still accessible in the interactions.
See the examples folder for examples of use and the DCI-Sample repository for a sample application using DCI through this gem.
Copyright
Copyright © 2012, 2013 Lorenzo Tello. See LICENSE.txt for further details.