drsi 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 11309067339e57a0aa6717ea6034208661896539
4
- data.tar.gz: 139fd79736dea05182ac7ec019a5d4d7a98648cd
3
+ metadata.gz: 92ea1b7d7874b1e6ee6596e7d1ff74934ff7faaf
4
+ data.tar.gz: e2da03ccffe38c5d4a07b47c4ade629daa5daf37
5
5
  SHA512:
6
- metadata.gz: c4aae9e60790998f6f1e194d3dd1b0aff8d1c85012bad35e4ef118d982d40254983be9868754714b7f465c2292a8e878c46a4243b79977b1245e9a131f9dcee7
7
- data.tar.gz: c21408e5e63dc26e2ea4cb9d687208ae6b33e9506f5c55ef3c02fcb7f47cb26181a6fbb43d2f6ee5b92ae62e9bec0bd67d2d1be9f9aa3af17f73e1caaa9babda
6
+ metadata.gz: bff09ba36627ace4931fde461587d988b6cb46033af254d5445d4494711f8254e90821303a40a4437af06627d5437124f65b9e81cb49fcc62e3189fdaf52d150
7
+ data.tar.gz: 917f13107b5a9ee8abc8459434a91421ff150d4e7c970d064e625151eee619d6f3386b62b98b0a7657fe316d9500dddf5045661536ad61d7496c73049dda7f70
@@ -1,4 +1,5 @@
1
1
  require 'drsi/dci/role'
2
+ require 'drsi/dci/multiplayer'
2
3
 
3
4
  module DCI
4
5
  class Context
@@ -154,14 +155,14 @@ module DCI
154
155
  # - This context instance get access to this new role player through an instance method named after the role key.
155
156
  def assign_role_to_player!(rolekey, player)
156
157
  role_mod = roles[rolekey]
157
- player.__play_role!(role_mod, self)
158
+ ::DCI::Multiplayer(player).each {|roleplayer| roleplayer.__play_role!(role_mod, self)}
158
159
  instance_variable_set(:"@#{rolekey}", player)
159
160
  end
160
161
 
161
162
  # Disassociates every role from the playing object.
162
163
  def players_unplay_role!
163
164
  roles.keys.each do |rolekey|
164
- @_players[rolekey].__unplay_last_role!
165
+ ::DCI::Multiplayer(@_players[rolekey]).each {|roleplayer| roleplayer.__unplay_last_role!}
165
166
  # 'instance_variable_set(:"@#{rolekey}", nil)
166
167
  end
167
168
  end
@@ -0,0 +1,40 @@
1
+ module DCI
2
+
3
+ # The intend of this method is to create something similar to Array():
4
+ # When called with a Multiplayer object, returns the object unmodified.
5
+ # Otherwise, retuns a new Multiplayer object wrapping the given argument.
6
+ def self.Multiplayer(obj)
7
+ obj.is_a?(::DCI::Multiplayer) ? obj : Multiplayer.new(obj)
8
+ end
9
+
10
+
11
+ # This simple class lets collect together a list of any type of objects.
12
+ # If you pass a multiplayer collection instance as the role player when instantiating a context,
13
+ # every object of the collection get the role instead of the multiplayer instance itself.
14
+ # So this is the way for different objects to play the similar roles at the same time in a DCI Context.
15
+ class Multiplayer
16
+
17
+ # Syntax sugar to easy the creation of Multiplayer instances.
18
+ def self.[](*args)
19
+ new(*args)
20
+ end
21
+
22
+ def each(&block)
23
+ @players.each {|player| block.call(player)}
24
+ end
25
+
26
+ # Give access to the players of this instance in a way similar to an array:
27
+ # multiplayer[1], multiplayer[1..5], multiplayer[-2], ...
28
+ def [](index)
29
+ @players[index]
30
+ end
31
+
32
+ def initialize(*args)
33
+ @players = args
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+
40
+
@@ -4,14 +4,14 @@ module DCI
4
4
  private
5
5
 
6
6
  def context
7
- raise 'This method must be redefined in every module including DCI::Role'
7
+ raise 'This method must be redefined in every module extending DCI::Role'
8
8
  end
9
9
 
10
10
  # Defines a new private reader instance method for a context mate role, delegating it to the context object.
11
11
  def add_role_reader_for!(rolekey)
12
12
  return if private_method_defined?(rolekey)
13
- private
14
13
  define_method(rolekey) {context.send(rolekey)}
14
+ private rolekey
15
15
  end
16
16
 
17
17
  end
@@ -1,3 +1,3 @@
1
1
  module Drsi
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,134 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'ostruct'
3
+
4
+ describe 'Role MultiPlayers' do
5
+
6
+ context "A role can be played by many objects in the same context at the same time" do
7
+ before(:all) do
8
+ class TestingRoleplayersContext < DCI::Context
9
+ role :role1 do
10
+ def role1method1; :role1method1_executed end
11
+ def role1self; self end
12
+ end
13
+
14
+ role :role2 do
15
+ def role2method1; role1 end
16
+ def role2self; self end
17
+
18
+ private
19
+ def private_role2method2; :private_rolemethod_return_value end
20
+ end
21
+
22
+ def check_role1_identity(obj)
23
+ [role1 == obj, role1.role1self == obj, role1.respond_to?(:role1method1), obj.respond_to?(:role1method1),
24
+ role1.role1method1 == :role1method1_executed, obj.role1method1 == :role1method1_executed].uniq == [true]
25
+ end
26
+
27
+ def get_role2_from_role1
28
+ role1.send(:role2)
29
+ end
30
+
31
+ def multiplayer_dont_get_role
32
+ [!role2.respond_to?(:role2method1), !role2.private_methods.map(&:to_s).include?('private_role2method2')].uniq == [true]
33
+ end
34
+
35
+ def multiplayer_players_get_their_role?
36
+ role2players = role2[0..-1]
37
+ [!role2players.empty?,
38
+ (role2players.all? {|roleplayer| roleplayer.respond_to?(:role2method1)}),
39
+ (role2players.all? {|roleplayer| roleplayer.role2method1 == role1}),
40
+ (role2players.all? {|roleplayer| roleplayer.private_methods.map(&:to_s).include?('private_role2method2')}),
41
+
42
+ ].uniq == [true]
43
+ end
44
+
45
+ def check_role2players_identity(*objs)
46
+ objs.all? do |obj|
47
+ index = objs.index(obj)
48
+ [role2 != obj, role2[index] == obj, role2[index].role2self == obj,
49
+ role2[index].send(:private_role2method2) == :private_rolemethod_return_value,
50
+ obj.send(:private_role2method2) == :private_rolemethod_return_value].uniq == [true]
51
+ end
52
+ end
53
+
54
+ def roles2_external_interfaces_accessible?(*names)
55
+ names.all? do |name|
56
+ index = names.index(name)
57
+ role2[index].name == name
58
+ end
59
+ end
60
+
61
+ def roles2_have_context_private_access?
62
+ role2[0..-1].all? do |roleplayer|
63
+ [!roleplayer.respond_to?(:context), roleplayer.private_methods.map(&:to_s).include?('context'), roleplayer.send(:context) == self].uniq == [true]
64
+ end
65
+ end
66
+
67
+ def roles2_have_settings_private_access?(settings_copy)
68
+ role2[0..-1].all? do |roleplayer|
69
+ [!roleplayer.respond_to?(:settings), roleplayer.private_methods.map(&:to_s).include?('settings'),
70
+ roleplayer.send(:settings) == settings, roleplayer.send(:settings) == settings_copy,
71
+ roleplayer.send(:settings, :setting2) == settings_copy[:setting2],
72
+ roleplayer.send(:settings, :setting1, :setting3) == {:setting1 => settings_copy[:setting1], :setting3 => settings_copy[:setting3]}
73
+ ].uniq == [true]
74
+ end
75
+ end
76
+
77
+ end
78
+
79
+ @player1, @player2, @player22 = OpenStruct.new(:name => 'player1'), OpenStruct.new(:name => 'player2'), OpenStruct.new(:name => 'player22')
80
+ @multiplayer = DCI::Multiplayer[@player2, @player22]
81
+ #puts @multiplayer.instance_variable_get(:@players)
82
+ @testing_roleplayers_context = TestingRoleplayersContext.new(:role1 => @player1,
83
+ :role2 => @multiplayer,
84
+ :setting1 => :one,
85
+ :setting2 => :two,
86
+ :setting3 => :three)
87
+ end
88
+
89
+ it("For it to work, the developer has to give a rolekey the value of a DCI::Multiplayer instance when instantiating the context...") do
90
+ @testing_roleplayers_context.get_role2_from_role1.should be(@multiplayer)
91
+ end
92
+
93
+ it("...so now, all the objects wrapped by the multiplayer instance play the role intended") do
94
+ @testing_roleplayers_context.multiplayer_dont_get_role.should be_true
95
+ @testing_roleplayers_context.multiplayer_players_get_their_role?.should be_true
96
+ end
97
+
98
+ it("...preserving their identity") do
99
+ @testing_roleplayers_context.check_role2players_identity(@player2, @player22).should be_true
100
+ end
101
+ it("...and therefore, their state and behaviour are accessible inside the context.") do
102
+ @testing_roleplayers_context.roles2_external_interfaces_accessible?(@player2.name, @player22.name).should be_true
103
+ end
104
+
105
+ it("They have private access to the context.") do
106
+ @testing_roleplayers_context.roles2_have_context_private_access?.should be_true
107
+ end
108
+
109
+ it("And also private access to extra args received in the instantiation of its context.") do
110
+ @testing_roleplayers_context.roles2_have_settings_private_access?({:setting1 => :one, :setting2 => :two, :setting3 => :three}).should be_true
111
+ end
112
+
113
+ it("But all these features, are only inside a context. Never out of it!") do
114
+ @player2.should_not respond_to(:role1method1)
115
+ @player22.should_not respond_to(:role1method1)
116
+ @player2.private_methods.map(&:to_s).should_not include(:private_role2method2)
117
+ @player22.private_methods.map(&:to_s).should_not include(:private_role2method2)
118
+ @player2.name.should eq('player2')
119
+ @player22.name.should eq('player22')
120
+ @player2.should_not respond_to(:role1)
121
+ @player22.should_not respond_to(:role1)
122
+ @player2.should_not respond_to(:context)
123
+ @player22.should_not respond_to(:context)
124
+ @player2.should_not respond_to(:settings)
125
+ @player22.should_not respond_to(:settings)
126
+ (@player2.private_methods.map(&:to_s) & ['role1', 'context', 'settings']).should be_empty
127
+ (@player22.private_methods.map(&:to_s) & ['role1', 'context', 'settings']).should be_empty
128
+ end
129
+
130
+
131
+
132
+ end
133
+
134
+ end
@@ -5,10 +5,8 @@ describe 'Role' do
5
5
  context "When defining roles inside a DCI::Context subclass..." do
6
6
  before(:all) do
7
7
  class TestingRoleContext < DCI::Context
8
- role :rolename do
9
- end
10
- role :anotherrolename do
11
- end
8
+ role :rolename
9
+ role :anotherrolename do end
12
10
  end
13
11
  end
14
12
  it("...you can define as many as you want.") do
@@ -25,8 +23,9 @@ describe 'Role' do
25
23
  end
26
24
  end.to raise_error
27
25
  end
28
- it("A block defining rolemethods must be provided as well.") do
26
+ it("A block defining rolemethods can be provided as well.") do
29
27
  TestingRoleContext.roles[:rolename].should be_a(Module)
28
+ TestingRoleContext.roles[:anotherrolename].should be_a(Module)
30
29
  end
31
30
  end
32
31
 
@@ -19,7 +19,9 @@ describe 'RolePlayers' do
19
19
  def private_role2method2; :private_rolemethod_return_value end
20
20
  end
21
21
 
22
- def check_role_interaccess; role2.role2method1 == role1 end
22
+ def check_role_interaccess
23
+ [role2.role2method1 == role1, !role2.respond_to?(:role1)].uniq == [true]
24
+ end
23
25
 
24
26
  def check_role1_identity(obj)
25
27
  [role1 == obj, role1.role1self == obj, role1.respond_to?(:role1method1), obj.respond_to?(:role1method1),
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: drsi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lorenzo Tello
@@ -43,6 +43,7 @@ files:
43
43
  - examples/money_transfer.rb
44
44
  - lib/drsi.rb
45
45
  - lib/drsi/dci/context.rb
46
+ - lib/drsi/dci/multiplayer.rb
46
47
  - lib/drsi/dci/role.rb
47
48
  - lib/drsi/module.rb
48
49
  - lib/drsi/object.rb
@@ -50,6 +51,7 @@ files:
50
51
  - lib/drsi/version.rb
51
52
  - spec/context_spec.rb
52
53
  - spec/interaction_spec.rb
54
+ - spec/multiplayers_spec.rb
53
55
  - spec/players_spec.rb
54
56
  - spec/role_spec.rb
55
57
  - spec/roleplayers_spec.rb
@@ -83,6 +85,7 @@ summary: Make DCI paradigm available to Ruby applications by enabling developers
83
85
  test_files:
84
86
  - spec/context_spec.rb
85
87
  - spec/interaction_spec.rb
88
+ - spec/multiplayers_spec.rb
86
89
  - spec/players_spec.rb
87
90
  - spec/role_spec.rb
88
91
  - spec/roleplayers_spec.rb