Skip to content
rubyworksPublic

About

Experiment in DCI for Ruby

Resources

Stars

35 stars

Watchers

3 watching

Forks

Repository files navigation

DCI for Ruby

Source Code / Report Issue / API     CI

Description

The DCI library for Ruby is a fairly faithful implementation of the DCI (Data, Context and Interaction) concepts developed by Trygve Reenskaug and James O. Coplien.

It defines two reusable base classes, the Role and the Context. The data is any object of yours.

It is an experiment more than a tool: a small, working statement of the idea, to read and try out.

Synopsis

The data is a plain object, which knows nothing of any use case.

class Account
  attr_reader :balance

  def initialize(balance)
    @balance = balance
  end

  def increase_balance(amount)
    @balance += amount
  end

  def decrease_balance(amount)
    @balance -= amount
  end
end

A role is what an object does in a use case. Anything a role doesn't define is sent on to the object playing it.

class TransferWithdraw < DCI::Role
  def transfer(amount)
    decrease_balance(amount)
  end
end

class TransferDeposit < DCI::Role
  def transfer(amount)
    increase_balance(amount)
  end
end

A context names the roles of a use case, casts objects in them, and carries the use case out.

class Transfer < DCI::Context
  role :source_account      => TransferWithdraw
  role :destination_account => TransferDeposit

  def initialize(source_account, destination_account)
    self.source_account      = source_account
    self.destination_account = destination_account
  end

  def transfer(amount)
    roles.each{ |role| role.transfer(amount) }
  end
end

acct1 = Account.new(100)
acct2 = Account.new(100)

Transfer.new(acct1, acct2).transfer(50)

acct1.balance  #=> 50
acct2.balance  #=> 150

The demonstrations have more, and are run as tests with QED.

Installation

The usual RubyGems install procedure:

$ gem install dci

DCI needs Ruby 3.1 or newer.

Copyrights

Copyright (c) 2012 Rubyworks. All rights reserved.

DCI for Ruby is distributable under the terms of BSD-2-Clause license.

See COPYING.md file for license details.

About

Experiment in DCI for Ruby

Resources

Stars

35 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages