dci-ruby 2.1.2 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dci-ruby (2.1.1)
4
+ dci-ruby (2.1.2)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -15,6 +15,11 @@ module DCI
15
15
  end
16
16
  end
17
17
 
18
+ # A short way for ContextSubclass.new(players_and_extra_args).run(extra_args)
19
+ def [](*args)
20
+ new(*args).run
21
+ end
22
+
18
23
 
19
24
  private
20
25
 
@@ -83,8 +88,10 @@ module DCI
83
88
  private
84
89
 
85
90
  # Private access to the extra args received in the instantiation.
86
- def settings(key)
87
- @settings[key]
91
+ def settings(*keys)
92
+ return @settings.dup if keys.empty?
93
+ entries = @settings.reject {|k, v| !keys.include?(k)}
94
+ keys.size == 1 ? entries.values.first : entries
88
95
  end
89
96
 
90
97
  # Checks there is a player for each role.
@@ -84,5 +84,10 @@ module DCI
84
84
  @player
85
85
  end
86
86
 
87
+ # The role definition code also have private access to the extra args given in the context instantiation.
88
+ def settings(*keys)
89
+ @context.send(:settings, *keys)
90
+ end
91
+
87
92
  end
88
93
  end
@@ -1,3 +1,3 @@
1
1
  module DCI
2
- VERSION = "2.1.2"
2
+ VERSION = "2.2.0"
3
3
  end
@@ -53,15 +53,22 @@ describe DCI::Context do
53
53
  before(:all) do
54
54
  class TestingUseContext < DCI::Context
55
55
  role :role1 do
56
+ def role1_method
57
+ :role1_method
58
+ end
56
59
  end
57
60
  role :role2 do
58
61
  end
59
62
 
60
- def interaction1
61
- role1
63
+ def run
64
+ role1.role1_method
62
65
  end
63
66
 
64
67
  def interaction2
68
+ role1
69
+ end
70
+
71
+ def interaction3
65
72
  role1.object_id - role2.object_id
66
73
  end
67
74
  end
@@ -70,7 +77,9 @@ describe DCI::Context do
70
77
  @context_instance_2 = TestingUseContext.new(:role1 => @player1, :role2 => @player2, :extra_arg => :extra)
71
78
  end
72
79
 
73
- it("...instanciate it from its correspondig DCI::Context subclass...") {@context_instance_1.should be_a(TestingUseContext)}
80
+ it("...instanciate it from its correspondig DCI::Context subclass as usual...") do
81
+ @context_instance_1.should be_a(TestingUseContext)
82
+ end
74
83
  it("...providing pairs of type :rolekey1 => player1 as arguments...") do
75
84
  expect {TestingUseContext.new(@player1, @player2)}.to raise_error
76
85
  end
@@ -85,13 +94,20 @@ describe DCI::Context do
85
94
  expect {TestingUseContext.new(:role1 => @player1, :role2 => @player2, :extra_arg => :extra)}.not_to raise_error
86
95
  end
87
96
 
97
+ it("An shorter way to instantiate a context is through the class method []...") do
98
+ expect {TestingUseContext[:role1 => @player1, :role2 => @player2, :extra_arg => :extra]}.not_to raise_error
99
+ end
100
+ it("...which is equivalent to create a new instance and call #run on it.") do
101
+ TestingUseContext[:role1 => @player1, :role2 => @player2].should be(:role1_method)
102
+ end
103
+
88
104
 
89
105
  it("Once instanciated...") {@context_instance_1.should be_a(TestingUseContext)}
90
106
  it("...you call an interaction (instance method) on it") do
91
- @context_instance_1.should respond_to(:interaction1)
107
+ @context_instance_1.should respond_to(:interaction2)
92
108
  end
93
109
  it("...to start interaction among roleplayers inside the context.") do
94
- @context_instance_1.interaction2.should be_instance_of(Fixnum)
110
+ @context_instance_1.interaction3.should be_instance_of(Fixnum)
95
111
  end
96
112
 
97
113
  end
@@ -15,7 +15,11 @@ describe 'Interaction:' do
15
15
  end
16
16
  end
17
17
  @player1, @player2 = Object.new, Object.new
18
- @test_interactions_context = TestingInteractionsContext.new(:role1 => @player1, :role2 => @player2, :setting1 => :one)
18
+ @test_interactions_context = TestingInteractionsContext.new(:role1 => @player1,
19
+ :role2 => @player2,
20
+ :setting1 => :one,
21
+ :setting2 => :two,
22
+ :setting3 => :three)
19
23
  end
20
24
 
21
25
  it("...the developer has access to all the roleplayers...") do
@@ -29,8 +33,14 @@ describe 'Interaction:' do
29
33
  it("He also have private access to extra args received in the instantiation of its context...") do
30
34
  @test_interactions_context.private_methods.should include('settings')
31
35
  end
32
- it("...calling #settings(key)") do
33
- @test_interactions_context.send(:settings, :setting1).should be(:one)
36
+ it("...calling #settings that returns a hash with all the extra args...") do
37
+ @test_interactions_context.send(:settings).should eq({:setting1 => :one, :setting2 => :two, :setting3 => :three})
38
+ end
39
+ it("...or #settings(key) that returns the value of the given extra arg...") do
40
+ @test_interactions_context.send(:settings, :setting2).should be(:two)
41
+ end
42
+ it("...or #settings(key1, key2, ...) that returns a hash with the given extra args.") do
43
+ @test_interactions_context.send(:settings, :setting1, :setting3).should eq({:setting1 => :one, :setting3 => :three})
34
44
  end
35
45
  end
36
46
 
@@ -33,7 +33,11 @@ describe 'RolePlayers' do
33
33
  end
34
34
  end
35
35
  @player1, @player2 = OpenStruct.new(:name => 'player1'), OpenStruct.new(:name => 'player2')
36
- @testing_roleplayers_context = TestingRoleplayersContext.new(:role1 => @player1, :role2 => @player2)
36
+ @testing_roleplayers_context = TestingRoleplayersContext.new(:role1 => @player1,
37
+ :role2 => @player2,
38
+ :setting1 => :one,
39
+ :setting2 => :two,
40
+ :setting3 => :three)
37
41
  end
38
42
 
39
43
  it("...each one instance of the class defined after the role he plays...") do
@@ -72,6 +76,20 @@ describe 'RolePlayers' do
72
76
  @testing_roleplayers_context.send(:role2).instance_variables.should include('@context')
73
77
  @testing_roleplayers_context.send(:role2).instance_variable_get(:@context).should be(@testing_roleplayers_context)
74
78
  end
79
+
80
+ it("They also have private access to extra args received in the instantiation of its context...") do
81
+ @testing_roleplayers_context.private_methods.should include('settings')
82
+ end
83
+ it("...calling #settings that returns a hash with all the extra args...") do
84
+ @testing_roleplayers_context.send(:settings).should eq({:setting1 => :one, :setting2 => :two, :setting3 => :three})
85
+ end
86
+ it("...or #settings(key) that returns the value of the given extra arg...") do
87
+ @testing_roleplayers_context.send(:settings, :setting2).should be(:two)
88
+ end
89
+ it("...or #settings(key1, key2, ...) that returns a hash with the given extra args.") do
90
+ @testing_roleplayers_context.send(:settings, :setting1, :setting3).should eq({:setting1 => :one, :setting3 => :three})
91
+ end
92
+
75
93
  end
76
94
 
77
95
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dci-ruby
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 2
8
- - 1
9
8
  - 2
10
- version: 2.1.2
9
+ - 0
10
+ version: 2.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Lorenzo Tello
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-11-12 00:00:00 Z
18
+ date: 2013-11-13 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rspec