dci 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/COPYING.md +3 -3
- data/HISTORY.md +24 -0
- data/README.md +78 -9
- data/demo/account_example.md +5 -6
- data/demo/applique/dci.rb +2 -0
- data/demo/context.md +43 -0
- data/demo/role.md +45 -0
- data/lib/dci/context.rb +92 -76
- data/lib/dci/role.rb +58 -47
- data/lib/dci/version.rb +4 -0
- data/lib/dci.rb +2 -1
- metadata +53 -36
- data/.ruby +0 -43
- data/.yardopts +0 -7
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 5b7ac684e438b2b414e153b5b4b93e1ec88949272df8c7127639797244ec2978
|
|
4
|
+
data.tar.gz: f60bf40bf38d3fe9799c62d70fe4593cd5902066930144cf0163dfff27994121
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: ef6ba8d455d742eeadbd37c46be2d2c5f3957d7ed8874a163b3ccd0498d36a5d2d041919d4169fe59146379c6f9e8c2b704ef0f0a54795a09b9b9de6bdbfb490
|
|
7
|
+
data.tar.gz: fb5c1a8b929c0a14dee632c59a35fda83ba405ba687dfce3776c8911262917b68e119627284b246ec3184fc3068649db1fd4a505d7476efab17608aa62351c17
|
data/COPYING.md
CHANGED
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
## NOTICES
|
|
4
4
|
|
|
5
|
-
###
|
|
5
|
+
### DCI
|
|
6
6
|
|
|
7
|
-
| Project |
|
|
7
|
+
| Project | DCI |
|
|
8
8
|
|-----------|-----------------------------------|
|
|
9
9
|
| Copyright | (c) 2012 Rubyworks |
|
|
10
10
|
| License | (r) BSD-2-Clause |
|
|
11
|
-
| Website |
|
|
11
|
+
| Website | https://github.com/rubyworks/dci |
|
|
12
12
|
|
|
13
13
|
## LICENSES
|
|
14
14
|
|
data/HISTORY.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# HISTORY
|
|
2
2
|
|
|
3
|
+
## 0.3.0 | 2026-11-30
|
|
4
|
+
|
|
5
|
+
DCI is brought up to date: it needs Ruby 3.1 or newer, and its
|
|
6
|
+
demonstrations run in CI.
|
|
7
|
+
|
|
8
|
+
Changes:
|
|
9
|
+
|
|
10
|
+
* A role passes keyword arguments on to its player.
|
|
11
|
+
* A role answers `respond_to?` for its player's methods.
|
|
12
|
+
* `DCI::VERSION` is a plain constant.
|
|
13
|
+
* Plain gemspec; the demonstrations are included in the gem.
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
## 0.2.0 | 2012-02-08
|
|
17
|
+
|
|
18
|
+
This release wraps the code in the DCI namepace and improved role
|
|
19
|
+
lookup in Context class.
|
|
20
|
+
|
|
21
|
+
Changes:
|
|
22
|
+
|
|
23
|
+
* Use DCI namespace.
|
|
24
|
+
* Improve context role lookup.
|
|
25
|
+
|
|
26
|
+
|
|
3
27
|
## 0.1.0 | 2012-02-07
|
|
4
28
|
|
|
5
29
|
This is the initial working version of DCI.
|
data/README.md
CHANGED
|
@@ -1,27 +1,96 @@
|
|
|
1
1
|
# DCI for Ruby
|
|
2
2
|
|
|
3
|
-
[
|
|
4
|
-
[
|
|
5
|
-
[
|
|
6
|
-
[
|
|
3
|
+
[Source Code](https://github.com/rubyworks/dci) /
|
|
4
|
+
[Report Issue](https://github.com/rubyworks/dci/issues) /
|
|
5
|
+
[API](https://rubydoc.info/gems/dci)
|
|
6
|
+
[](https://github.com/rubyworks/dci/actions/workflows/ci.yml)
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
## Description
|
|
10
10
|
|
|
11
11
|
The DCI library for Ruby is a fairly faithful implementation of the DCI
|
|
12
|
-
|
|
12
|
+
(Data, Context and Interaction) concepts developed by Trygve Reenskaug
|
|
13
|
+
and James O. Coplien.
|
|
13
14
|
|
|
14
|
-
It
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
It defines two reusable base classes, the Role and the Context. The data
|
|
16
|
+
is any object of yours.
|
|
17
|
+
|
|
18
|
+
It is an experiment more than a tool: a small, working statement of the
|
|
19
|
+
idea, to read and try out.
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## Synopsis
|
|
23
|
+
|
|
24
|
+
The data is a plain object, which knows nothing of any use case.
|
|
25
|
+
|
|
26
|
+
class Account
|
|
27
|
+
attr_reader :balance
|
|
28
|
+
|
|
29
|
+
def initialize(balance)
|
|
30
|
+
@balance = balance
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def increase_balance(amount)
|
|
34
|
+
@balance += amount
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def decrease_balance(amount)
|
|
38
|
+
@balance -= amount
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
A role is what an object does in a use case. Anything a role doesn't
|
|
43
|
+
define is sent on to the object playing it.
|
|
44
|
+
|
|
45
|
+
class TransferWithdraw < DCI::Role
|
|
46
|
+
def transfer(amount)
|
|
47
|
+
decrease_balance(amount)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
class TransferDeposit < DCI::Role
|
|
52
|
+
def transfer(amount)
|
|
53
|
+
increase_balance(amount)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
A context names the roles of a use case, casts objects in them, and
|
|
58
|
+
carries the use case out.
|
|
59
|
+
|
|
60
|
+
class Transfer < DCI::Context
|
|
61
|
+
role :source_account => TransferWithdraw
|
|
62
|
+
role :destination_account => TransferDeposit
|
|
63
|
+
|
|
64
|
+
def initialize(source_account, destination_account)
|
|
65
|
+
self.source_account = source_account
|
|
66
|
+
self.destination_account = destination_account
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def transfer(amount)
|
|
70
|
+
roles.each{ |role| role.transfer(amount) }
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
acct1 = Account.new(100)
|
|
75
|
+
acct2 = Account.new(100)
|
|
76
|
+
|
|
77
|
+
Transfer.new(acct1, acct2).transfer(50)
|
|
78
|
+
|
|
79
|
+
acct1.balance #=> 50
|
|
80
|
+
acct2.balance #=> 150
|
|
81
|
+
|
|
82
|
+
The [demonstrations](https://github.com/rubyworks/dci/tree/master/demo)
|
|
83
|
+
have more, and are run as tests with [QED](https://github.com/rubyworks/qed).
|
|
17
84
|
|
|
18
85
|
|
|
19
86
|
## Installation
|
|
20
87
|
|
|
21
|
-
The
|
|
88
|
+
The usual RubyGems install procedure:
|
|
22
89
|
|
|
23
90
|
$ gem install dci
|
|
24
91
|
|
|
92
|
+
DCI needs Ruby 3.1 or newer.
|
|
93
|
+
|
|
25
94
|
|
|
26
95
|
## Copyrights
|
|
27
96
|
|
data/demo/account_example.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Account Balance Transfer
|
|
2
2
|
|
|
3
|
-
The Account Balance
|
|
3
|
+
The Account Balance Transfer is the classic example of using DCI.
|
|
4
4
|
|
|
5
5
|
First we need our Data model. In the example that is the Account class.
|
|
6
6
|
To keep our example simple we will initialize new accounts with
|
|
@@ -28,14 +28,14 @@ a balance of $100. (We're generous like that.)
|
|
|
28
28
|
We set up two Roles, one role for withdrawing money from an account,
|
|
29
29
|
and one for depositing money into an account.
|
|
30
30
|
|
|
31
|
-
class Account::TransferWithdraw < Role
|
|
31
|
+
class Account::TransferWithdraw < DCI::Role
|
|
32
32
|
def transfer(amount)
|
|
33
33
|
decrease_balance(amount)
|
|
34
34
|
#log "Tranfered $#{amount} from account ##{account_id}."
|
|
35
35
|
end
|
|
36
36
|
end
|
|
37
37
|
|
|
38
|
-
class Account::TransferDeposit < Role
|
|
38
|
+
class Account::TransferDeposit < DCI::Role
|
|
39
39
|
def transfer(amount)
|
|
40
40
|
increase_balance(amount)
|
|
41
41
|
#log "Tranfered $#{amount} into account ##{account_id}."
|
|
@@ -43,10 +43,9 @@ and one for depositing money into an account.
|
|
|
43
43
|
end
|
|
44
44
|
|
|
45
45
|
Now we create a Context which will assign accounts to the roles
|
|
46
|
-
and used to
|
|
46
|
+
and used to perform the transfer.
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
class Account::Transfer < Context
|
|
48
|
+
class Account::Transfer < DCI::Context
|
|
50
49
|
role :source_account => Account::TransferWithdraw
|
|
51
50
|
role :destination_account => Account::TransferDeposit
|
|
52
51
|
|
data/demo/applique/dci.rb
CHANGED
data/demo/context.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Context
|
|
2
|
+
|
|
3
|
+
Let's setup a very simple example to run some tests.
|
|
4
|
+
|
|
5
|
+
class ExampleObject
|
|
6
|
+
def say_anything
|
|
7
|
+
"anything"
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class ExampleRole < DCI::Role
|
|
12
|
+
def say_anything
|
|
13
|
+
@self.say_anything
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class ExampleContext < DCI::Context
|
|
18
|
+
role :player => ExampleRole
|
|
19
|
+
|
|
20
|
+
def initialize(player)
|
|
21
|
+
self.player = player
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def say_anything
|
|
25
|
+
roles.each{ |role| role.say_anything }
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
So lets make sure the roles are listed.
|
|
30
|
+
|
|
31
|
+
ExampleContext.roles #=> [:player]
|
|
32
|
+
|
|
33
|
+
Now if subclass the ExampleContext and added a new roll,
|
|
34
|
+
|
|
35
|
+
class AlternateContext < ExampleContext
|
|
36
|
+
role :fanboy => ExampleRole
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
Then both roles should be in the the list.
|
|
40
|
+
|
|
41
|
+
AlternateContext.roles #=> [:fanboy, :player]
|
|
42
|
+
|
|
43
|
+
|
data/demo/role.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Role
|
|
2
|
+
|
|
3
|
+
A role is played by an object. Whatever the role doesn't define itself
|
|
4
|
+
is sent on to that object, the player.
|
|
5
|
+
|
|
6
|
+
class Greeter
|
|
7
|
+
def name
|
|
8
|
+
"World"
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def greet(greeting: "Hello")
|
|
12
|
+
"#{greeting}, #{name}!"
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class LoudRole < DCI::Role
|
|
17
|
+
def shout
|
|
18
|
+
greet(greeting: "HEY").upcase
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
role = LoudRole.new(Greeter.new)
|
|
23
|
+
|
|
24
|
+
The role has its own methods, which call the player's as if they were
|
|
25
|
+
its own.
|
|
26
|
+
|
|
27
|
+
role.shout #=> "HEY, WORLD!"
|
|
28
|
+
|
|
29
|
+
The player's methods can be called on the role too, keyword arguments
|
|
30
|
+
and all.
|
|
31
|
+
|
|
32
|
+
role.name #=> "World"
|
|
33
|
+
role.greet(greeting: "Howdy") #=> "Howdy, World!"
|
|
34
|
+
|
|
35
|
+
So a role responds to whatever its player responds to.
|
|
36
|
+
|
|
37
|
+
role.respond_to?(:shout) #=> true
|
|
38
|
+
role.respond_to?(:name) #=> true
|
|
39
|
+
role.respond_to?(:nope) #=> false
|
|
40
|
+
|
|
41
|
+
And a method neither of them has is a NoMethodError, as usual.
|
|
42
|
+
|
|
43
|
+
expect NoMethodError do
|
|
44
|
+
role.nope
|
|
45
|
+
end
|
data/lib/dci/context.rb
CHANGED
|
@@ -1,89 +1,105 @@
|
|
|
1
|
-
|
|
2
|
-
# for a given algorithm, scenario, or use case, as well as the code to map these
|
|
3
|
-
# roles into objects at run time and to enact the use case. Each role is bound
|
|
4
|
-
# to exactly one object during any given use case enactment; however, a single
|
|
5
|
-
# object may simultaneously play several roles. A context is instantiated at the
|
|
6
|
-
# beginning of the enactment of an algorithm, scenario, or use case. In summary,
|
|
7
|
-
# a Context comprises use cases and algorithms in which data objects are used
|
|
8
|
-
# through specific Roles.
|
|
9
|
-
#
|
|
10
|
-
# Each context represents one or more use cases. A context object is instantiated
|
|
11
|
-
# for each enactment of a use case for which it is responsible. Its main job
|
|
12
|
-
# is to identify the objects that will participate in the use case and to
|
|
13
|
-
# assign them to play the Roles which carry out the use case through their
|
|
14
|
-
# responsibilities. A role may comprise methods, and each method is some small
|
|
15
|
-
# part of the logic of an algorithm implementing a use case. Role methods run
|
|
16
|
-
# in the context of an object that is selected by the context to play that role
|
|
17
|
-
# for the current use case enactment. The role-to-object bindings that take place
|
|
18
|
-
# in a context can be contrasted with the polymorphism of vernacular object-oriented
|
|
19
|
-
# programming. The overall business functionality is the sum of complex, dynamic
|
|
20
|
-
# networks of methods decentralized in multiple contexts and their roles.
|
|
21
|
-
#
|
|
22
|
-
# Each context is a scope that includes identifiers that correspond to its roles.
|
|
23
|
-
# Any role executing within that context can refer to the other roles in that
|
|
24
|
-
# context through these identifiers. These identifiers have come to be called
|
|
25
|
-
# methodless roles. At use case enactment time, each and every one of these
|
|
26
|
-
# identifiers becomes bound to an object playing the corresponding Role for
|
|
27
|
-
# this Context.
|
|
28
|
-
#
|
|
29
|
-
# An example of a context could be a wire transfer between two accounts,
|
|
30
|
-
# where data models (the banking accounts) are used through roles named
|
|
31
|
-
# SourceAccount and # DestinationAccount.
|
|
32
|
-
#
|
|
33
|
-
# class Account::Transfer < Context
|
|
34
|
-
# role :source_account => Account::TransferWithdraw
|
|
35
|
-
# role :destination_account => Account::TransferDeposit
|
|
36
|
-
#
|
|
37
|
-
# def initialize(source_account, destination_account)
|
|
38
|
-
# self.source_account = source_account
|
|
39
|
-
# self.destination_account = destination_account
|
|
40
|
-
# end
|
|
41
|
-
#
|
|
42
|
-
# def transfer(amount)
|
|
43
|
-
# roles.each{ |role| role.transfer(amount) }
|
|
44
|
-
# end
|
|
45
|
-
# end
|
|
46
|
-
#
|
|
47
|
-
class Context
|
|
1
|
+
module DCI
|
|
48
2
|
|
|
49
|
-
#
|
|
50
|
-
#
|
|
3
|
+
# Context - The Context is the class (or its instance) whose code includes the roles
|
|
4
|
+
# for a given algorithm, scenario, or use case, as well as the code to map these
|
|
5
|
+
# roles into objects at run time and to enact the use case. Each role is bound
|
|
6
|
+
# to exactly one object during any given use case enactment; however, a single
|
|
7
|
+
# object may simultaneously play several roles. A context is instantiated at the
|
|
8
|
+
# beginning of the enactment of an algorithm, scenario, or use case. In summary,
|
|
9
|
+
# a Context comprises use cases and algorithms in which data objects are used
|
|
10
|
+
# through specific Roles.
|
|
51
11
|
#
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
12
|
+
# Each context represents one or more use cases. A context object is instantiated
|
|
13
|
+
# for each enactment of a use case for which it is responsible. Its main job
|
|
14
|
+
# is to identify the objects that will participate in the use case and to
|
|
15
|
+
# assign them to play the Roles which carry out the use case through their
|
|
16
|
+
# responsibilities. A role may comprise methods, and each method is some small
|
|
17
|
+
# part of the logic of an algorithm implementing a use case. Role methods run
|
|
18
|
+
# in the context of an object that is selected by the context to play that role
|
|
19
|
+
# for the current use case enactment. The role-to-object bindings that take place
|
|
20
|
+
# in a context can be contrasted with the polymorphism of vernacular object-oriented
|
|
21
|
+
# programming. The overall business functionality is the sum of complex, dynamic
|
|
22
|
+
# networks of methods decentralized in multiple contexts and their roles.
|
|
23
|
+
#
|
|
24
|
+
# Each context is a scope that includes identifiers that correspond to its roles.
|
|
25
|
+
# Any role executing within that context can refer to the other roles in that
|
|
26
|
+
# context through these identifiers. These identifiers have come to be called
|
|
27
|
+
# methodless roles. At use case enactment time, each and every one of these
|
|
28
|
+
# identifiers becomes bound to an object playing the corresponding Role for
|
|
29
|
+
# this Context.
|
|
30
|
+
#
|
|
31
|
+
# An example of a context could be a wire transfer between two accounts,
|
|
32
|
+
# where data models (the banking accounts) are used through roles named
|
|
33
|
+
# SourceAccount and # DestinationAccount.
|
|
34
|
+
#
|
|
35
|
+
# class Account::Transfer < Context
|
|
36
|
+
# role :source_account => Account::TransferWithdraw
|
|
37
|
+
# role :destination_account => Account::TransferDeposit
|
|
38
|
+
#
|
|
39
|
+
# def initialize(source_account, destination_account)
|
|
40
|
+
# self.source_account = source_account
|
|
41
|
+
# self.destination_account = destination_account
|
|
42
|
+
# end
|
|
43
|
+
#
|
|
44
|
+
# def transfer(amount)
|
|
45
|
+
# roles.each{ |role| role.transfer(amount) }
|
|
46
|
+
# end
|
|
47
|
+
# end
|
|
48
|
+
#
|
|
49
|
+
class Context
|
|
55
50
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
51
|
+
# Define a role given the name the role will use in this context,
|
|
52
|
+
# and the role class that is to be played.
|
|
53
|
+
#
|
|
54
|
+
def self.role(name_to_role)
|
|
55
|
+
@roles = nil # reset
|
|
56
|
+
|
|
57
|
+
name_to_role.each do |name, role|
|
|
58
|
+
define_method("role_#{name}"){ role }
|
|
60
59
|
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
module_eval %{
|
|
61
|
+
def #{name}=(data)
|
|
62
|
+
@#{name} = role_#{name}.new(data)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def #{name}
|
|
66
|
+
@#{name}
|
|
67
|
+
end
|
|
68
|
+
}
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Return a list of the names of defined roles.
|
|
73
|
+
#
|
|
74
|
+
# TODO: Consider private vs public roles.
|
|
75
|
+
def self.roles
|
|
76
|
+
@roles ||= (
|
|
77
|
+
list = []
|
|
78
|
+
instance_methods.each do |name|
|
|
79
|
+
next unless name.to_s.start_with?('role_')
|
|
80
|
+
list << name.to_s[5..-1].to_sym
|
|
63
81
|
end
|
|
64
|
-
|
|
82
|
+
list
|
|
83
|
+
)
|
|
65
84
|
end
|
|
66
|
-
end
|
|
67
85
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
86
|
+
# The default contructor can be used to assign roles via
|
|
87
|
+
# a settings hash.
|
|
88
|
+
#
|
|
89
|
+
def initialize(settings={})
|
|
90
|
+
settings.each do |k,v|
|
|
91
|
+
__send__("#{k}=", v)
|
|
92
|
+
end
|
|
74
93
|
end
|
|
75
|
-
end
|
|
76
94
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
next unless name.to_s.start_with?('role_')
|
|
84
|
-
list << __send__(name.to_s.sub('role_',''))
|
|
95
|
+
# Return Array of all role instances in the context.
|
|
96
|
+
#
|
|
97
|
+
def roles
|
|
98
|
+
self.class.roles.map do |name|
|
|
99
|
+
__send__(name)
|
|
100
|
+
end
|
|
85
101
|
end
|
|
86
|
-
|
|
102
|
+
|
|
87
103
|
end
|
|
88
104
|
|
|
89
105
|
end
|
data/lib/dci/role.rb
CHANGED
|
@@ -1,53 +1,64 @@
|
|
|
1
|
-
|
|
2
|
-
# is implemented as Roles which are played by objects at run time. These objects
|
|
3
|
-
# combine the state and methods of a Data (domain) object with methods (but no
|
|
4
|
-
# state, as Roles are stateless) from one or more Roles. In good DCI style,
|
|
5
|
-
# a Role addresses another object only in terms of its (methodless) Role. There
|
|
6
|
-
# is a special Role called `@self` which binds to the object playing the current
|
|
7
|
-
# Role. Code within a Role method may invoke a method on `@self` and thereby
|
|
8
|
-
# invoke a method of the Data part of the current object. One curious aspect
|
|
9
|
-
# of DCI is that these bindings are guaranteed to be in place only at run time
|
|
10
|
-
# (using a variety of approaches and conventions; C++ templates can be used to
|
|
11
|
-
# guarantee that the bindings will succeed). This means that Interactions—the
|
|
12
|
-
# Role methods—are generic. In fact, some DCI implementations use generics or
|
|
13
|
-
# templates for Roles.
|
|
14
|
-
#
|
|
15
|
-
# A Role is a stateless programming construct that corresponds to the end user's
|
|
16
|
-
# mental model of some entity in the system. A Role represents a collection of
|
|
17
|
-
# responsibilities. Whereas vernacular object-oriented programming speaks of
|
|
18
|
-
# objects or classes as the loci of responsibilities, DCI ascribes them to Roles.
|
|
19
|
-
# An object participating in a use case has responsibilities: those that it takes
|
|
20
|
-
# on as a result of playing a particular Role.
|
|
21
|
-
#
|
|
22
|
-
# In the money transfer use case, for example, the role methods in the
|
|
23
|
-
# SourceAccount and DestinationAccount enact the actual transfer.
|
|
24
|
-
#
|
|
25
|
-
# class Account::TransferWithdraw < Role
|
|
26
|
-
# def transfer(amount)
|
|
27
|
-
# decrease_balance(amount)
|
|
28
|
-
# log "Tranfered from account #{account_id} $#{amount}"
|
|
29
|
-
# end
|
|
30
|
-
# end
|
|
31
|
-
#
|
|
32
|
-
# class Account::TransferDepoit < Role
|
|
33
|
-
# def transfer(amount)
|
|
34
|
-
# increase_balance(amount)
|
|
35
|
-
# log "Tranfered into account #{account_id} $#{amount}"
|
|
36
|
-
# end
|
|
37
|
-
# end
|
|
38
|
-
#
|
|
39
|
-
class Role
|
|
1
|
+
module DCI
|
|
40
2
|
|
|
3
|
+
# Interaction - The Interaction is "what the system does." The Interaction
|
|
4
|
+
# is implemented as Roles which are played by objects at run time. These objects
|
|
5
|
+
# combine the state and methods of a Data (domain) object with methods (but no
|
|
6
|
+
# state, as Roles are stateless) from one or more Roles. In good DCI style,
|
|
7
|
+
# a Role addresses another object only in terms of its (methodless) Role. There
|
|
8
|
+
# is a special Role called `@self` which binds to the object playing the current
|
|
9
|
+
# Role. Code within a Role method may invoke a method on `@self` and thereby
|
|
10
|
+
# invoke a method of the Data part of the current object. One curious aspect
|
|
11
|
+
# of DCI is that these bindings are guaranteed to be in place only at run time
|
|
12
|
+
# (using a variety of approaches and conventions; C++ templates can be used to
|
|
13
|
+
# guarantee that the bindings will succeed). This means that Interactions—the
|
|
14
|
+
# Role methods—are generic. In fact, some DCI implementations use generics or
|
|
15
|
+
# templates for Roles.
|
|
41
16
|
#
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
17
|
+
# A Role is a stateless programming construct that corresponds to the end user's
|
|
18
|
+
# mental model of some entity in the system. A Role represents a collection of
|
|
19
|
+
# responsibilities. Whereas vernacular object-oriented programming speaks of
|
|
20
|
+
# objects or classes as the loci of responsibilities, DCI ascribes them to Roles.
|
|
21
|
+
# An object participating in a use case has responsibilities: those that it takes
|
|
22
|
+
# on as a result of playing a particular Role.
|
|
23
|
+
#
|
|
24
|
+
# In the money transfer use case, for example, the role methods in the
|
|
25
|
+
# SourceAccount and DestinationAccount enact the actual transfer.
|
|
26
|
+
#
|
|
27
|
+
# class Account::TransferWithdraw < Role
|
|
28
|
+
# def transfer(amount)
|
|
29
|
+
# decrease_balance(amount)
|
|
30
|
+
# log "Tranfered from account #{account_id} $#{amount}"
|
|
31
|
+
# end
|
|
32
|
+
# end
|
|
33
|
+
#
|
|
34
|
+
# class Account::TransferDeposit < Role
|
|
35
|
+
# def transfer(amount)
|
|
36
|
+
# increase_balance(amount)
|
|
37
|
+
# log "Tranfered into account #{account_id} $#{amount}"
|
|
38
|
+
# end
|
|
39
|
+
# end
|
|
46
40
|
#
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
41
|
+
class Role
|
|
42
|
+
|
|
43
|
+
# A role is played by an object, the player, which the role's methods
|
|
44
|
+
# know as `@self`.
|
|
45
|
+
def initialize(player)
|
|
46
|
+
@self = player
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Anything the role doesn't define is sent on to the player, so a
|
|
50
|
+
# role method can call the player's methods as if they were its own.
|
|
51
|
+
#
|
|
52
|
+
# @todo Should use #public_send?
|
|
53
|
+
def method_missing(s, *a, **k, &b)
|
|
54
|
+
@self.__send__(s, *a, **k, &b)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# A role responds to whatever its player responds to.
|
|
58
|
+
def respond_to_missing?(s, include_all=false)
|
|
59
|
+
@self.respond_to?(s, include_all) || super
|
|
60
|
+
end
|
|
61
|
+
|
|
50
62
|
end
|
|
51
63
|
|
|
52
64
|
end
|
|
53
|
-
|
data/lib/dci/version.rb
ADDED
data/lib/dci.rb
CHANGED
metadata
CHANGED
|
@@ -1,82 +1,99 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: dci
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
5
|
-
prerelease:
|
|
4
|
+
version: 0.3.0
|
|
6
5
|
platform: ruby
|
|
7
6
|
authors:
|
|
8
|
-
-
|
|
9
|
-
autorequire:
|
|
7
|
+
- Trans
|
|
10
8
|
bindir: bin
|
|
11
9
|
cert_chain: []
|
|
12
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
13
11
|
dependencies:
|
|
14
12
|
- !ruby/object:Gem::Dependency
|
|
15
|
-
name:
|
|
16
|
-
requirement:
|
|
17
|
-
none: false
|
|
13
|
+
name: rake
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
18
15
|
requirements:
|
|
19
|
-
- -
|
|
16
|
+
- - ">="
|
|
20
17
|
- !ruby/object:Gem::Version
|
|
21
|
-
version: '
|
|
18
|
+
version: '13'
|
|
22
19
|
type: :development
|
|
23
20
|
prerelease: false
|
|
24
|
-
version_requirements:
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '13'
|
|
25
26
|
- !ruby/object:Gem::Dependency
|
|
26
27
|
name: qed
|
|
27
|
-
requirement:
|
|
28
|
-
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '3.0'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
29
36
|
requirements:
|
|
30
|
-
- -
|
|
37
|
+
- - ">="
|
|
31
38
|
- !ruby/object:Gem::Version
|
|
32
|
-
version: '0'
|
|
39
|
+
version: '3.0'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: ae
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '1.9'
|
|
33
47
|
type: :development
|
|
34
48
|
prerelease: false
|
|
35
|
-
version_requirements:
|
|
36
|
-
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '1.9'
|
|
54
|
+
description: 'A faithful, if small, implementation of Data, Context and Interaction
|
|
55
|
+
(DCI) for Ruby: a Role class and a Context class.'
|
|
37
56
|
email:
|
|
38
57
|
- transfire@gmail.com
|
|
39
58
|
executables: []
|
|
40
59
|
extensions: []
|
|
41
|
-
extra_rdoc_files:
|
|
60
|
+
extra_rdoc_files: []
|
|
61
|
+
files:
|
|
42
62
|
- COPYING.md
|
|
43
63
|
- HISTORY.md
|
|
44
64
|
- README.md
|
|
45
|
-
files:
|
|
46
|
-
- .ruby
|
|
47
|
-
- .yardopts
|
|
48
65
|
- demo/account_example.md
|
|
49
66
|
- demo/applique/dci.rb
|
|
67
|
+
- demo/context.md
|
|
68
|
+
- demo/role.md
|
|
69
|
+
- lib/dci.rb
|
|
50
70
|
- lib/dci/context.rb
|
|
51
71
|
- lib/dci/object.rb
|
|
52
72
|
- lib/dci/role.rb
|
|
53
|
-
- lib/dci.rb
|
|
54
|
-
|
|
55
|
-
- HISTORY.md
|
|
56
|
-
- README.md
|
|
57
|
-
homepage: https://rubyworks.github.com/dci
|
|
73
|
+
- lib/dci/version.rb
|
|
74
|
+
homepage: https://github.com/rubyworks/dci
|
|
58
75
|
licenses:
|
|
59
76
|
- BSD-2-Clause
|
|
60
|
-
|
|
77
|
+
metadata:
|
|
78
|
+
source_code_uri: https://github.com/rubyworks/dci
|
|
79
|
+
bug_tracker_uri: https://github.com/rubyworks/dci/issues
|
|
80
|
+
changelog_uri: https://github.com/rubyworks/dci/blob/master/HISTORY.md
|
|
81
|
+
documentation_uri: https://rubydoc.info/gems/dci
|
|
61
82
|
rdoc_options: []
|
|
62
83
|
require_paths:
|
|
63
84
|
- lib
|
|
64
85
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
65
|
-
none: false
|
|
66
86
|
requirements:
|
|
67
|
-
- -
|
|
87
|
+
- - ">="
|
|
68
88
|
- !ruby/object:Gem::Version
|
|
69
|
-
version: '
|
|
89
|
+
version: '3.1'
|
|
70
90
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
|
-
none: false
|
|
72
91
|
requirements:
|
|
73
|
-
- -
|
|
92
|
+
- - ">="
|
|
74
93
|
- !ruby/object:Gem::Version
|
|
75
94
|
version: '0'
|
|
76
95
|
requirements: []
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
signing_key:
|
|
80
|
-
specification_version: 3
|
|
96
|
+
rubygems_version: 3.6.9
|
|
97
|
+
specification_version: 4
|
|
81
98
|
summary: DCI for Ruby
|
|
82
99
|
test_files: []
|
data/.ruby
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
source:
|
|
3
|
-
- Profile
|
|
4
|
-
authors:
|
|
5
|
-
- name: Thomas Sawyer
|
|
6
|
-
email: transfire@gmail.com
|
|
7
|
-
copyrights:
|
|
8
|
-
- holder: Thomas Sawyer
|
|
9
|
-
year: '2012'
|
|
10
|
-
license: BSD-2-Clause
|
|
11
|
-
replacements: []
|
|
12
|
-
alternatives: []
|
|
13
|
-
requirements:
|
|
14
|
-
- name: detroit
|
|
15
|
-
groups:
|
|
16
|
-
- build
|
|
17
|
-
development: true
|
|
18
|
-
- name: qed
|
|
19
|
-
groups:
|
|
20
|
-
- test
|
|
21
|
-
development: true
|
|
22
|
-
dependencies: []
|
|
23
|
-
conflicts: []
|
|
24
|
-
repositories:
|
|
25
|
-
- uri: git@github.com:rubyworks/dci.git
|
|
26
|
-
scm: git
|
|
27
|
-
name: upstream
|
|
28
|
-
resources:
|
|
29
|
-
home: https://rubyworks.github.com/dci
|
|
30
|
-
code: https://github.com/rubyworks/dci
|
|
31
|
-
mail: http://groups.google.com/groups/rubyworks-mailinglist
|
|
32
|
-
extra: {}
|
|
33
|
-
load_path:
|
|
34
|
-
- lib
|
|
35
|
-
revision: 0
|
|
36
|
-
version: 0.1.0
|
|
37
|
-
name: dci
|
|
38
|
-
title: DCI
|
|
39
|
-
summary: DCI for Ruby
|
|
40
|
-
created: '2011-03-04'
|
|
41
|
-
description: Faithful DCI framework for Ruby application development.
|
|
42
|
-
organization: Rubyworks
|
|
43
|
-
date: '2012-02-07'
|