dci-ruby 2.1.0 → 2.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/examples/money_transfer.rb +4 -8
- data/lib/dci-ruby/dci/role.rb +18 -0
- data/lib/dci-ruby/version.rb +1 -1
- data/spec/players_spec.rb +91 -0
- metadata +6 -4
data/Gemfile.lock
CHANGED
data/examples/money_transfer.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
require 'forwardable'
|
2
1
|
require 'dci-ruby'
|
3
2
|
|
4
3
|
|
@@ -17,8 +16,8 @@ class MoneyTransferContext < DCI::Context
|
|
17
16
|
# Roles Definitions
|
18
17
|
|
19
18
|
role :source_account do
|
20
|
-
|
21
|
-
|
19
|
+
delegates_to_player :balance, :account_id
|
20
|
+
private_delegate_to_player :balance=
|
22
21
|
|
23
22
|
def run_transfer_of(amount)
|
24
23
|
self.balance -= amount
|
@@ -27,11 +26,8 @@ class MoneyTransferContext < DCI::Context
|
|
27
26
|
end
|
28
27
|
|
29
28
|
role :target_account do
|
30
|
-
|
31
|
-
|
32
|
-
def balance; player.balance end
|
33
|
-
def balance=(amount) player.balance=(amount) end
|
34
|
-
private :balance=
|
29
|
+
delegates_to_player :balance, :account_id
|
30
|
+
private_delegate_to_player :balance=
|
35
31
|
|
36
32
|
def run_transfer_of(amount)
|
37
33
|
self.balance += amount
|
data/lib/dci-ruby/dci/role.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module DCI
|
2
2
|
|
3
3
|
class Role
|
4
|
+
extend ::Forwardable
|
4
5
|
|
5
6
|
class << self
|
6
7
|
|
@@ -10,6 +11,23 @@ module DCI
|
|
10
11
|
super
|
11
12
|
end
|
12
13
|
|
14
|
+
def delegate_to_player(player_methodname, role_method_name = player_methodname)
|
15
|
+
class_eval("def #{role_method_name}(*args, &block); player.send(:#{player_methodname}, *args, &block) end")
|
16
|
+
end
|
17
|
+
|
18
|
+
def private_delegate_to_player(player_methodname, role_method_name = player_methodname)
|
19
|
+
delegate_to_player(player_methodname, role_method_name)
|
20
|
+
private role_method_name
|
21
|
+
end
|
22
|
+
|
23
|
+
def delegates_to_player(*methodnames)
|
24
|
+
methodnames.each {|methodname| delegate_to_player(methodname)}
|
25
|
+
end
|
26
|
+
|
27
|
+
def private_delegates_to_player(*methodnames)
|
28
|
+
methodnames.each {|methodname| private_delegate_to_player(methodname)}
|
29
|
+
end
|
30
|
+
|
13
31
|
# Defines a new reader instance method for a context mate role, delegating it to the context object.
|
14
32
|
def add_role_reader_for!(rolekey)
|
15
33
|
return if private_method_defined?(rolekey)
|
data/lib/dci-ruby/version.rb
CHANGED
@@ -0,0 +1,91 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
# require 'ostruct'
|
3
|
+
|
4
|
+
|
5
|
+
describe 'Players:' do
|
6
|
+
|
7
|
+
class CheckingAccount
|
8
|
+
attr_reader :account_id
|
9
|
+
attr_accessor :balance
|
10
|
+
|
11
|
+
def initialize(account_id, initial_balance=0)
|
12
|
+
@account_id, @balance = account_id, initial_balance
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
class MoneyTransferContext < DCI::Context
|
18
|
+
|
19
|
+
# Roles Definitions
|
20
|
+
role :source_account do
|
21
|
+
delegate_to_player :balance
|
22
|
+
private_delegate_to_player :balance=
|
23
|
+
|
24
|
+
def run_transfer_of(amount)
|
25
|
+
self.balance -= amount
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
role :target_account do
|
30
|
+
delegate_to_player :balance
|
31
|
+
private_delegate_to_player :balance=
|
32
|
+
# def_delegators :player, :balance, :balance=
|
33
|
+
# private :balance=
|
34
|
+
|
35
|
+
def run_transfer_of(amount)
|
36
|
+
self.balance += amount
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Interactions
|
41
|
+
def run(amount=settings(:amount))
|
42
|
+
source_account.run_transfer_of(amount)
|
43
|
+
target_account.run_transfer_of(amount)
|
44
|
+
balances
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
def accounts
|
49
|
+
[source_account, target_account]
|
50
|
+
end
|
51
|
+
|
52
|
+
def balances
|
53
|
+
accounts.map(&:balance)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "Are common Ruby objects that play roles inside contexts:" do
|
58
|
+
before(:all) do
|
59
|
+
@account1 = CheckingAccount.new(1, 1000)
|
60
|
+
@account2 = CheckingAccount.new(2)
|
61
|
+
@account1_public_interface = @account1.public_methods
|
62
|
+
end
|
63
|
+
|
64
|
+
context "Before becoming roleplayers inside a context..." do
|
65
|
+
it("...they are in an initial state...") do
|
66
|
+
@account1.balance.should be(1000)
|
67
|
+
@account2.balance.should be(0)
|
68
|
+
end
|
69
|
+
it("...and have got a given public interface.") do
|
70
|
+
@account1_public_interface.should be_true
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "After playing a role inside a context..." do
|
75
|
+
before(:all) do
|
76
|
+
MoneyTransferContext.new(:source_account => @account1,
|
77
|
+
:target_account => @account2).run(200)
|
78
|
+
end
|
79
|
+
it("...they still preserve their public interface...") do
|
80
|
+
@account1.public_methods.should eql(@account1_public_interface)
|
81
|
+
@account1.should_not respond_to(:run_transfer_of)
|
82
|
+
@account1.private_methods.should_not include(:run_transfer_of)
|
83
|
+
end
|
84
|
+
it("...although their state might have been changed!") do
|
85
|
+
@account1.balance.should_not be(1000)
|
86
|
+
@account2.balance.should_not be(0)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dci-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 9
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 2
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 2.1.
|
9
|
+
- 1
|
10
|
+
version: 2.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Lorenzo Tello
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2013-10-
|
18
|
+
date: 2013-10-30 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rspec
|
@@ -59,6 +59,7 @@ files:
|
|
59
59
|
- lib/dci-ruby/version.rb
|
60
60
|
- spec/context_spec.rb
|
61
61
|
- spec/interaction_spec.rb
|
62
|
+
- spec/players_spec.rb
|
62
63
|
- spec/role_spec.rb
|
63
64
|
- spec/roleplayers_spec.rb
|
64
65
|
- spec/spec_helper.rb
|
@@ -98,6 +99,7 @@ summary: Make DCI paradigm available to Ruby applications by enabling developers
|
|
98
99
|
test_files:
|
99
100
|
- spec/context_spec.rb
|
100
101
|
- spec/interaction_spec.rb
|
102
|
+
- spec/players_spec.rb
|
101
103
|
- spec/role_spec.rb
|
102
104
|
- spec/roleplayers_spec.rb
|
103
105
|
- spec/spec_helper.rb
|