role_playing 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -21
- data/lib/role_playing/context.rb +5 -1
- data/lib/role_playing/core_ext.rb +7 -0
- data/lib/role_playing/role.rb +8 -0
- data/lib/role_playing/version.rb +1 -1
- data/spec/role_playing/role_playing_spec.rb +5 -23
- metadata +1 -1
data/README.md
CHANGED
@@ -37,15 +37,11 @@ Using it is as simple as defining (usually) a context like so:
|
|
37
37
|
@to_account = to_account
|
38
38
|
end
|
39
39
|
def call(amount)
|
40
|
-
withdrawal = @from_account
|
41
|
-
|
40
|
+
withdrawal = SourceAccount(@from_account) do |source_account|
|
41
|
+
DestinationAccount(@to_account).deposit(source_account.withdraw(amount))
|
42
|
+
end
|
42
43
|
end
|
43
44
|
|
44
|
-
## inside a context, a role can be defined
|
45
|
-
## using the class method "role", it's basically
|
46
|
-
## the same as using class SourceAccount < RolePlaying::Role
|
47
|
-
## but a nicer language and does the inheritance for us and -
|
48
|
-
## YES these should be constants, not strings or symbols
|
49
45
|
role SourceAccount do
|
50
46
|
def withdraw(amount)
|
51
47
|
self.amount=self.amount-amount
|
@@ -59,20 +55,6 @@ Using it is as simple as defining (usually) a context like so:
|
|
59
55
|
end
|
60
56
|
end
|
61
57
|
|
62
|
-
## roles can be defined as classes too of course
|
63
|
-
#class SourceAccount < RolePlaying::Role
|
64
|
-
# def withdraw(amount)
|
65
|
-
# self.amount=self.amount-amount
|
66
|
-
# amount
|
67
|
-
# end
|
68
|
-
#end
|
69
|
-
|
70
|
-
#class DestinationAccount < RolePlaying::Role
|
71
|
-
# def deposit(amount)
|
72
|
-
# self.amount=self.amount+amount
|
73
|
-
# end
|
74
|
-
#end
|
75
|
-
|
76
58
|
end
|
77
59
|
|
78
60
|
Please read the specs for a better understanding. Also please look up DCI (data, context, interaction) for a better understanding of what this is trying to accomplish.
|
data/lib/role_playing/context.rb
CHANGED
@@ -5,11 +5,15 @@ module RolePlaying
|
|
5
5
|
end
|
6
6
|
module ClassMethods
|
7
7
|
def const_missing(sym)
|
8
|
-
|
8
|
+
sym
|
9
9
|
end
|
10
10
|
def role(name, parent=nil, &block)
|
11
11
|
parent = parent || RolePlaying::Role
|
12
12
|
klass = Class.new(parent, &block)
|
13
|
+
define_method(name) do |object, &role_block|
|
14
|
+
instance = klass.new(object)
|
15
|
+
role_block.nil? ? instance : role_block.call(instance)
|
16
|
+
end
|
13
17
|
const_set name, klass
|
14
18
|
end
|
15
19
|
end
|
data/lib/role_playing/role.rb
CHANGED
@@ -2,6 +2,14 @@ require 'delegate'
|
|
2
2
|
|
3
3
|
module RolePlaying
|
4
4
|
class Role < SimpleDelegator
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def played_by(object, &block)
|
8
|
+
extended = new(object)
|
9
|
+
block_given? ? yield(extended) : extended
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
5
13
|
def class
|
6
14
|
role_player.class ## this makes self.class return the extended objects class instead of DCIRole - should make the extension completely transparent
|
7
15
|
end
|
data/lib/role_playing/version.rb
CHANGED
@@ -31,15 +31,11 @@ class MoneyTransferring
|
|
31
31
|
@to_account = to_account
|
32
32
|
end
|
33
33
|
def call(amount)
|
34
|
-
withdrawal = @from_account
|
35
|
-
|
34
|
+
withdrawal = SourceAccount(@from_account) do |source_account|
|
35
|
+
DestinationAccount(@to_account).deposit(source_account.withdraw(amount))
|
36
|
+
end
|
36
37
|
end
|
37
38
|
|
38
|
-
## inside a context, a role can be defined
|
39
|
-
## using the class method "role", it's basically
|
40
|
-
## the same as using class SourceAccount < RolePlaying::Role
|
41
|
-
## but a nicer language and does the inheritance for us and -
|
42
|
-
## YES these should be constants, not strings or symbols
|
43
39
|
role SourceAccount do
|
44
40
|
def withdraw(amount)
|
45
41
|
self.amount=self.amount-amount
|
@@ -53,20 +49,6 @@ class MoneyTransferring
|
|
53
49
|
end
|
54
50
|
end
|
55
51
|
|
56
|
-
## roles can be defined as classes too of course
|
57
|
-
#class SourceAccount < RolePlaying::Role
|
58
|
-
# def withdraw(amount)
|
59
|
-
# self.amount=self.amount-amount
|
60
|
-
# amount
|
61
|
-
# end
|
62
|
-
#end
|
63
|
-
|
64
|
-
#class DestinationAccount < RolePlaying::Role
|
65
|
-
# def deposit(amount)
|
66
|
-
# self.amount=self.amount+amount
|
67
|
-
# end
|
68
|
-
#end
|
69
|
-
|
70
52
|
end
|
71
53
|
|
72
54
|
describe RolePlaying do
|
@@ -107,7 +89,7 @@ describe RolePlaying do
|
|
107
89
|
|
108
90
|
context MoneyTransferring do
|
109
91
|
|
110
|
-
role
|
92
|
+
role MoneyTransferring::SourceAccount do
|
111
93
|
let(:original_amount) { 50 }
|
112
94
|
let(:bare_account) {Account.new(original_amount)}
|
113
95
|
subject { MoneyTransferring::SourceAccount.new(bare_account) }
|
@@ -122,7 +104,7 @@ describe RolePlaying do
|
|
122
104
|
end
|
123
105
|
end
|
124
106
|
|
125
|
-
role
|
107
|
+
role MoneyTransferring::DestinationAccount do
|
126
108
|
let(:original_amount) { 50 }
|
127
109
|
let(:bare_account) {Account.new(original_amount)}
|
128
110
|
subject { MoneyTransferring::DestinationAccount.new(bare_account) }
|