role_playing 0.0.4 → 0.0.5
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.
- data/README.md +23 -3
- data/lib/role_playing/context.rb +14 -0
- data/lib/role_playing/{dci.rb → core_ext.rb} +0 -20
- data/lib/role_playing/role.rb +19 -0
- data/lib/role_playing/version.rb +1 -1
- data/lib/role_playing.rb +4 -1
- data/role_playing.gemspec +2 -0
- data/spec/role_playing/{dci_spec.rb → role_playing_spec.rb} +28 -10
- metadata +23 -5
data/README.md
CHANGED
@@ -29,7 +29,9 @@ Or install it yourself as:
|
|
29
29
|
|
30
30
|
Using it is as simple as defining (usually) a context like so:
|
31
31
|
|
32
|
-
class
|
32
|
+
class MoneyTransferring
|
33
|
+
include RolePlaying::Context
|
34
|
+
|
33
35
|
def initialize(from_account, to_account)
|
34
36
|
@from_account = from_account
|
35
37
|
@to_account = to_account
|
@@ -39,19 +41,37 @@ Using it is as simple as defining (usually) a context like so:
|
|
39
41
|
@to_account.in_role(DestinationAccount).deposit(withdrawal)
|
40
42
|
end
|
41
43
|
|
42
|
-
|
44
|
+
## inside a context, a role can be defined
|
45
|
+
## using the class method "role"
|
46
|
+
role :source_account do
|
43
47
|
def withdraw(amount)
|
44
48
|
self.amount=self.amount-amount
|
45
49
|
amount
|
46
50
|
end
|
47
51
|
end
|
48
52
|
|
49
|
-
|
53
|
+
## the #role method comes from including
|
54
|
+
## the RolePlaying::Context module
|
55
|
+
role :destination_account do
|
50
56
|
def deposit(amount)
|
51
57
|
self.amount=self.amount+amount
|
52
58
|
end
|
53
59
|
end
|
54
60
|
|
61
|
+
## roles can be defined as classes too of course
|
62
|
+
#class SourceAccount < RolePlaying::Role
|
63
|
+
# def withdraw(amount)
|
64
|
+
# self.amount=self.amount-amount
|
65
|
+
# amount
|
66
|
+
# end
|
67
|
+
#end
|
68
|
+
|
69
|
+
#class DestinationAccount < RolePlaying::Role
|
70
|
+
# def deposit(amount)
|
71
|
+
# self.amount=self.amount+amount
|
72
|
+
# end
|
73
|
+
#end
|
74
|
+
|
55
75
|
end
|
56
76
|
|
57
77
|
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.
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module RolePlaying
|
2
|
+
module Context
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
module ClassMethods
|
7
|
+
def role(name, parent=nil, &block)
|
8
|
+
parent = parent || RolePlaying::Role
|
9
|
+
klass = Class.new(parent, &block)
|
10
|
+
self.const_set name.to_s.camelize, klass
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -1,23 +1,3 @@
|
|
1
|
-
require 'delegate'
|
2
|
-
|
3
|
-
module RolePlaying
|
4
|
-
class Role < SimpleDelegator
|
5
|
-
def class
|
6
|
-
role_player.class ## this makes self.class return the extended objects class instead of DCIRole - should make the extension completely transparent
|
7
|
-
end
|
8
|
-
|
9
|
-
## return the FINAL wrapped object
|
10
|
-
def role_player
|
11
|
-
player = self
|
12
|
-
while player.respond_to?(:__getobj__)
|
13
|
-
player = player.__getobj__
|
14
|
-
end
|
15
|
-
player
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
1
|
## PLEASE NOTE: This is almost completely unnecessary except for some syntax sugar which I like.
|
22
2
|
## It basically does this: TheRole.new(theObject) and returns it and also runs an instance_eval
|
23
3
|
## if a block was given. It can also take many Roles, not just one so you could do theObject.as(SomeRole, AnotherRole).
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
3
|
+
module RolePlaying
|
4
|
+
class Role < SimpleDelegator
|
5
|
+
def class
|
6
|
+
role_player.class ## this makes self.class return the extended objects class instead of DCIRole - should make the extension completely transparent
|
7
|
+
end
|
8
|
+
|
9
|
+
## return the FINAL wrapped object
|
10
|
+
def role_player
|
11
|
+
player = self
|
12
|
+
while player.respond_to?(:__getobj__)
|
13
|
+
player = player.__getobj__
|
14
|
+
end
|
15
|
+
player
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
data/lib/role_playing/version.rb
CHANGED
data/lib/role_playing.rb
CHANGED
data/role_playing.gemspec
CHANGED
@@ -22,8 +22,10 @@ class MyRole < RolePlaying::Role
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
## a context
|
26
|
-
class
|
25
|
+
## a context/use case
|
26
|
+
class MoneyTransferring
|
27
|
+
include RolePlaying::Context
|
28
|
+
|
27
29
|
def initialize(from_account, to_account)
|
28
30
|
@from_account = from_account
|
29
31
|
@to_account = to_account
|
@@ -33,19 +35,35 @@ class MoneyTransfer
|
|
33
35
|
@to_account.in_role(DestinationAccount).deposit(withdrawal)
|
34
36
|
end
|
35
37
|
|
36
|
-
|
38
|
+
## inside a context, a role can be defined
|
39
|
+
## using the class method "role"
|
40
|
+
role :source_account do
|
37
41
|
def withdraw(amount)
|
38
42
|
self.amount=self.amount-amount
|
39
43
|
amount
|
40
44
|
end
|
41
45
|
end
|
42
46
|
|
43
|
-
|
47
|
+
role :destination_account do
|
44
48
|
def deposit(amount)
|
45
49
|
self.amount=self.amount+amount
|
46
50
|
end
|
47
51
|
end
|
48
52
|
|
53
|
+
## roles can be defined as classes too of course
|
54
|
+
#class SourceAccount < RolePlaying::Role
|
55
|
+
# def withdraw(amount)
|
56
|
+
# self.amount=self.amount-amount
|
57
|
+
# amount
|
58
|
+
# end
|
59
|
+
#end
|
60
|
+
|
61
|
+
#class DestinationAccount < RolePlaying::Role
|
62
|
+
# def deposit(amount)
|
63
|
+
# self.amount=self.amount+amount
|
64
|
+
# end
|
65
|
+
#end
|
66
|
+
|
49
67
|
end
|
50
68
|
|
51
69
|
describe RolePlaying do
|
@@ -84,12 +102,12 @@ describe RolePlaying do
|
|
84
102
|
|
85
103
|
end
|
86
104
|
|
87
|
-
context
|
105
|
+
context MoneyTransferring do
|
88
106
|
|
89
|
-
role
|
107
|
+
role :source_account do
|
90
108
|
let(:original_amount) { 50 }
|
91
109
|
let(:bare_account) {Account.new(original_amount)}
|
92
|
-
subject {
|
110
|
+
subject { MoneyTransferring::SourceAccount.new(bare_account) }
|
93
111
|
it "adds a withdraw method to data object" do
|
94
112
|
bare_account.should_not respond_to(:withdraw)
|
95
113
|
subject.should respond_to(:withdraw)
|
@@ -101,10 +119,10 @@ describe RolePlaying do
|
|
101
119
|
end
|
102
120
|
end
|
103
121
|
|
104
|
-
role
|
122
|
+
role :destination_account do
|
105
123
|
let(:original_amount) { 50 }
|
106
124
|
let(:bare_account) {Account.new(original_amount)}
|
107
|
-
subject {
|
125
|
+
subject { MoneyTransferring::DestinationAccount.new(bare_account) }
|
108
126
|
it "adds a deposit method to data object" do
|
109
127
|
bare_account.should_not respond_to(:deposit)
|
110
128
|
subject.should respond_to(:deposit)
|
@@ -121,7 +139,7 @@ describe RolePlaying do
|
|
121
139
|
let(:source_account) { Account.new(original_source_account_amount) }
|
122
140
|
let(:destination_account) { Account.new(original_destination_account_amount) }
|
123
141
|
let(:transfer_amount) { 50 }
|
124
|
-
subject {
|
142
|
+
subject { MoneyTransferring.new(source_account, destination_account) }
|
125
143
|
|
126
144
|
it "transfers a specified amount from a SourceAccount to a DestinationAccount" do
|
127
145
|
source_account.amount.should == original_source_account_amount
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: role_playing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -43,6 +43,22 @@ dependencies:
|
|
43
43
|
- - ~>
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: 1.2.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: activesupport
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 3.0.0
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.0.0
|
46
62
|
description: A ruby DCI implementation
|
47
63
|
email:
|
48
64
|
- john@insane.se
|
@@ -56,11 +72,13 @@ files:
|
|
56
72
|
- README.md
|
57
73
|
- Rakefile
|
58
74
|
- lib/role_playing.rb
|
59
|
-
- lib/role_playing/
|
75
|
+
- lib/role_playing/context.rb
|
76
|
+
- lib/role_playing/core_ext.rb
|
77
|
+
- lib/role_playing/role.rb
|
60
78
|
- lib/role_playing/rspec_role.rb
|
61
79
|
- lib/role_playing/version.rb
|
62
80
|
- role_playing.gemspec
|
63
|
-
- spec/role_playing/
|
81
|
+
- spec/role_playing/role_playing_spec.rb
|
64
82
|
- spec/spec_helper.rb
|
65
83
|
homepage: ''
|
66
84
|
licenses: []
|
@@ -87,6 +105,6 @@ signing_key:
|
|
87
105
|
specification_version: 3
|
88
106
|
summary: A ruby DCI implementation
|
89
107
|
test_files:
|
90
|
-
- spec/role_playing/
|
108
|
+
- spec/role_playing/role_playing_spec.rb
|
91
109
|
- spec/spec_helper.rb
|
92
110
|
has_rdoc:
|