GithubHelp home page GithubHelp logo

dci-ruby's Introduction

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 = settings(: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 use case wrapped in just one entity instead of spread out 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)

or the short preferred way:

MoneyTransfer[:source_account => Account.new(1),
              :target_account => Account.new(2),
              :amount         => 100]

In a context instance, every role instantiates an object (roleplayer) that gathers the behaviour defined inside its role, and has private access to the original object adopting the role (player): the Account instances above are players.

The instance of MoneyTransfer::SourceAccount and the one of MoneyTransfer::TargetAccount are roleplayers. They are accesible inside #run through #source_account and #target_account private methods. Also, every roleplayer has private access to the rest of roleplayers in the context.

Unlike extending players with role modules this Presenter approach gets on well with ruby method call caching mechanism. (see Tony Arcieri’s article DCI in Ruby is completely broken)

When instanciating a Context, the extra no-role pairs given as arguments are read-only attributes accessible via #settings:

MoneyTransfer[:source_account => Account.new(1),
              :target_account => Account.new(2),
              :amount => 500]

here, :amount is not a player (has no associated role) but is still privately accessible both in the interactions and the roles via #settings(:amount).

Inside a role definition, use the macro

delegate_to_player :player_methodname, :role_method_name

to create a method delegating its behaviour to the object playing the role (player) so a roleplayer can also respond to a player’s method.

delegates_to_player :methodname1, :methodname2... (for multiple methods delegation in just one sentence)

There are also private counterpart macros (private_delegate_to_player and private_delegates_to_player) to create private delegations not accessible outside a roleplayer.

See the examples folder for examples of use and the DCI-Sample repository for a sample Rails application using DCI through this gem.

Notice how your models and controllers are not overloaded anymore. They are thinner and simpler. Also note how now most of the functionality of the system is isolated, totally dry-ied and easily maintainable in the different context classes.

Copyright © 2012, 2013 Lorenzo Tello. See LICENSE.txt for further details.

dci-ruby's People

Contributors

ltello avatar

Stargazers

Billy Ma avatar Ariel Santos avatar  avatar Thomas Klemm avatar Angus H. avatar Rodrigo Álvarez avatar Trevor Bramble avatar Christopher Galtenberg avatar Alex avatar Sebastian Wagner avatar Eugene avatar Vladislav Bogomolov avatar Georg Stefan Schmid avatar

Watchers

 avatar Riccardo Lucatuorto avatar Eugene avatar James Cloos avatar

dci-ruby's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.