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 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.in_role(SourceAccount).withdraw(amount)
41
- @to_account.in_role(DestinationAccount).deposit(withdrawal)
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.
@@ -5,11 +5,15 @@ module RolePlaying
5
5
  end
6
6
  module ClassMethods
7
7
  def const_missing(sym)
8
- class_name = sym.to_s
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
@@ -9,4 +9,11 @@ class Object
9
9
  def in_role(role, &block)
10
10
  in_roles(*role, &block)
11
11
  end
12
+ end
13
+
14
+ class Array
15
+ def played_by(object)
16
+ extended = self.inject(object) { |extended, role| role.new(extended) }
17
+ block_given? ? yield(extended) : extended
18
+ end
12
19
  end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module RolePlaying
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -31,15 +31,11 @@ class MoneyTransferring
31
31
  @to_account = to_account
32
32
  end
33
33
  def call(amount)
34
- withdrawal = @from_account.in_role(SourceAccount).withdraw(amount)
35
- @to_account.in_role(DestinationAccount).deposit(withdrawal)
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 :source_account do
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 :destination_account do
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) }
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.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: