role_playing 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -37,19 +37,19 @@ 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 = SourceAccount(@from_account) do |source_account|
40
+ SourceAccount(@from_account) do |source_account|
41
41
  DestinationAccount(@to_account).deposit(source_account.withdraw(amount))
42
42
  end
43
43
  end
44
44
 
45
- role SourceAccount do
45
+ role :SourceAccount do
46
46
  def withdraw(amount)
47
47
  self.amount=self.amount-amount
48
48
  amount
49
49
  end
50
50
  end
51
51
 
52
- role DestinationAccount do
52
+ role :DestinationAccount do
53
53
  def deposit(amount)
54
54
  self.amount=self.amount+amount
55
55
  end
@@ -57,11 +57,53 @@ Using it is as simple as defining (usually) a context like so:
57
57
 
58
58
  end
59
59
 
60
+ Basically a role is a class inheriting from RolePlayer::Role, roles can also be defined by themselves(outside a context) like this:
61
+
62
+ class MyRole < RolePlayer::Role
63
+ def my_additional_method
64
+ end
65
+ end
66
+
67
+ class MyOtherRole < RolePlayer::Role
68
+ def my_other_method
69
+ end
70
+ end
71
+
72
+ And, if defined by themselves, they can be applied in a few ways:
73
+
74
+ ## our data object which will play different roles (eg. get new/different behavior within a context)
75
+ class MyDataObject
76
+ end
77
+
78
+ MyRole.played_by(MyDataObject) do |role|
79
+ role.my_additional_method
80
+ end
81
+
82
+ or
83
+
84
+ role = MyRole.played_by(MyDataObject)
85
+ role.my_additional_method
86
+
87
+ several roles can be applied too like so:
88
+
89
+ [MyRole, MyOtherRole].played_by(MyDataObject) do |role|
90
+ role.my_additional_method
91
+ role.my_other_method
92
+ end
93
+
94
+ or
95
+
96
+ role = [MyRole, MyOtherRole].played_by(MyDataObject)
97
+ role.my_additional_method
98
+ role.my_other_method
99
+
100
+ Within a context a role is defined by the role class method. The syntax sugar of applying a role - eg. MyRole(MyDataObject) do |role| - is only available within classes including the RolePlayer::Context module. This was the way I envisioned it - to basically keep all code concerning a context within the same file (and inside the context class).
101
+
60
102
  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.
61
103
 
62
- ## Rspec
104
+ ## RSpec
63
105
 
64
- Theres an Rspec extension included which basically aliases Rspecs context to role so the language used in Rspec can be closer to DCI when testing these things.
106
+ Theres an RSpec extension included which basically aliases RSpecs context to role so the language used in RSpec can be closer to DCI when testing these things.
65
107
  To use that extension just do require 'role_playing/rspec_role' in your spec_helper. Look at the specs in this gem to see what I mean.
66
108
 
67
109
  ## Rails
@@ -4,9 +4,13 @@ module RolePlaying
4
4
  base.extend(ClassMethods)
5
5
  end
6
6
  module ClassMethods
7
- def const_missing(sym)
8
- sym
9
- end
7
+ ## this seemed to dangerous to use
8
+ ## it enabled us to use Constants when
9
+ ## defining roles but would also not
10
+ ## warn about missing constants - which is pretty bad
11
+ #def const_missing(sym)
12
+ # sym
13
+ #end
10
14
  def role(name, parent=nil, &block)
11
15
  parent = parent || RolePlaying::Role
12
16
  klass = Class.new(parent, &block)
@@ -1,15 +1,6 @@
1
1
  ## PLEASE NOTE: This is almost completely unnecessary except for some syntax sugar which I like.
2
2
  ## It basically does this: TheRole.new(theObject) and returns it and also runs an instance_eval
3
3
  ## if a block was given. It can also take many Roles, not just one so you could do theObject.as(SomeRole, AnotherRole).
4
- class Object
5
- def in_roles(*roles, &block)
6
- extended = roles.inject(self) { |extended, role| role.new(extended) }
7
- block_given? ? yield(extended) : extended
8
- end
9
- def in_role(role, &block)
10
- in_roles(*role, &block)
11
- end
12
- end
13
4
 
14
5
  class Array
15
6
  def played_by(object)
@@ -4,9 +4,9 @@ module RolePlaying
4
4
  class Role < SimpleDelegator
5
5
 
6
6
  class << self
7
- def played_by(object, &block)
8
- extended = new(object)
9
- block_given? ? yield(extended) : extended
7
+ def played_by(object)
8
+ role = new(object)
9
+ block_given? ? yield(role) : role
10
10
  end
11
11
  end
12
12
 
@@ -1,3 +1,3 @@
1
1
  module RolePlaying
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -31,19 +31,19 @@ class MoneyTransferring
31
31
  @to_account = to_account
32
32
  end
33
33
  def call(amount)
34
- withdrawal = SourceAccount(@from_account) do |source_account|
34
+ SourceAccount(@from_account) do |source_account|
35
35
  DestinationAccount(@to_account).deposit(source_account.withdraw(amount))
36
36
  end
37
37
  end
38
38
 
39
- role SourceAccount do
39
+ role :SourceAccount do
40
40
  def withdraw(amount)
41
41
  self.amount=self.amount-amount
42
42
  amount
43
43
  end
44
44
  end
45
45
 
46
- role DestinationAccount do
46
+ role :DestinationAccount do
47
47
  def deposit(amount)
48
48
  self.amount=self.amount+amount
49
49
  end
@@ -79,7 +79,7 @@ describe RolePlaying do
79
79
  end
80
80
 
81
81
  it "#in_role takes a block and returns the result" do
82
- two_fields = bare_object.in_role(MyRole) do |role|
82
+ two_fields = MyRole.played_by(bare_object) do |role|
83
83
  role.two_fields
84
84
  end
85
85
  two_fields.should == "#{bare_object.field_1} #{bare_object.field_2}"
@@ -87,6 +87,34 @@ describe RolePlaying do
87
87
 
88
88
  end
89
89
 
90
+ context "playing roles" do
91
+ let(:role_class_1) do
92
+ Class.new(RolePlaying::Role) do
93
+ def role_1
94
+ end
95
+ end
96
+ end
97
+ let(:role_class_2) do
98
+ Class.new(RolePlaying::Role) do
99
+ def role_2
100
+ end
101
+ end
102
+ end
103
+ let(:player_class) do
104
+ Class.new do
105
+ def base
106
+ end
107
+ end
108
+ end
109
+
110
+ it "an array of roles can be applied using Array#played_by" do
111
+ role = [role_class_1, role_class_2].played_by(player_class.new)
112
+ role.should respond_to(:role_1)
113
+ role.should respond_to(:role_2)
114
+ role.should respond_to(:base)
115
+ end
116
+ end
117
+
90
118
  context MoneyTransferring do
91
119
 
92
120
  role MoneyTransferring::SourceAccount do
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.8
4
+ version: 0.0.9
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-08 00:00:00.000000000 Z
12
+ date: 2013-02-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec