surrounded 1.0.0 → 1.1.1

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.
Files changed (50) hide show
  1. checksums.yaml +5 -5
  2. data/Changelog.md +11 -11
  3. data/LICENSE.txt +1 -1
  4. data/README.md +39 -9
  5. data/Rakefile +12 -5
  6. data/lib/surrounded/access_control.rb +19 -26
  7. data/lib/surrounded/context/forwarding.rb +10 -10
  8. data/lib/surrounded/context/initializing.rb +10 -7
  9. data/lib/surrounded/context/name_collision_detector.rb +17 -17
  10. data/lib/surrounded/context/negotiator.rb +13 -13
  11. data/lib/surrounded/context/role_builders.rb +8 -7
  12. data/lib/surrounded/context/role_map.rb +20 -13
  13. data/lib/surrounded/context/seclusion.rb +20 -0
  14. data/lib/surrounded/context/trigger_controls.rb +14 -16
  15. data/lib/surrounded/context.rb +64 -72
  16. data/lib/surrounded/east_oriented.rb +4 -4
  17. data/lib/surrounded/exceptions.rb +1 -1
  18. data/lib/surrounded/shortcuts.rb +15 -5
  19. data/lib/surrounded/version.rb +2 -2
  20. data/lib/surrounded.rb +7 -7
  21. data/surrounded.gemspec +21 -16
  22. data/test/{casting_role_player_test.rb → casting_test_helper.rb} +4 -3
  23. data/test/collection_role_players_test.rb +16 -16
  24. data/test/context_access_test.rb +31 -30
  25. data/test/context_forwarding_test.rb +30 -30
  26. data/test/context_reuse_test.rb +14 -14
  27. data/test/context_shortcuts_test.rb +38 -11
  28. data/test/east_oriented_triggers_test.rb +14 -13
  29. data/test/example_delegate_class_test.rb +8 -8
  30. data/test/example_proxy_test.rb +25 -23
  31. data/test/example_threaded_test.rb +13 -13
  32. data/test/example_wrapper_test.rb +7 -7
  33. data/test/initialization_test.rb +25 -26
  34. data/test/name_collisions_test.rb +48 -42
  35. data/test/non_surrounded_role_player_test.rb +8 -8
  36. data/test/override_methods_test.rb +9 -9
  37. data/test/role_context_method_test.rb +144 -98
  38. data/test/surrounded_context_test.rb +71 -62
  39. data/test/surrounded_test.rb +13 -15
  40. data/test/test_helper.rb +8 -5
  41. data/test/threaded_context_test.rb +70 -0
  42. metadata +9 -52
  43. data/.codeclimate.yml +0 -4
  44. data/.gitignore +0 -19
  45. data/.pullreview.yml +0 -4
  46. data/.simplecov +0 -3
  47. data/.travis.yml +0 -19
  48. data/Gemfile +0 -10
  49. data/examples/bottles.rb +0 -145
  50. data/examples/rails.rb +0 -57
@@ -1,113 +1,113 @@
1
- require 'test_helper'
1
+ require "test_helper"
2
2
 
3
- describe Surrounded::Context, '#triggers' do
4
- let(:user){ User.new("Jim") }
5
- let(:other_user){ User.new("Guille") }
6
- let(:context){ TestContext.new(user: user, other_user: other_user) }
3
+ describe Surrounded::Context, "#triggers" do
4
+ let(:user) { User.new("Jim") }
5
+ let(:other_user) { User.new("Guille") }
6
+ let(:context) { TestContext.new(user: user, other_user: other_user) }
7
7
 
8
- it 'lists the externally accessible trigger methods' do
8
+ it "lists the externally accessible trigger methods" do
9
9
  assert context.triggers.include?(:access_other_object)
10
10
  end
11
11
 
12
- it 'prevents altering the list of triggers externally' do
12
+ it "prevents altering the list of triggers externally" do
13
13
  original_trigger_list = context.triggers
14
- context.triggers << 'another_trigger'
14
+ context.triggers << "another_trigger"
15
15
  assert_equal original_trigger_list, context.triggers
16
16
  end
17
17
  end
18
18
 
19
- describe Surrounded::Context, '.triggers' do
20
- it 'lists the externally accessible trigger methods' do
19
+ describe Surrounded::Context, ".triggers" do
20
+ it "lists the externally accessible trigger methods" do
21
21
  assert TestContext.triggers.include?(:access_other_object)
22
22
  end
23
23
 
24
- it 'prevents altering the list of triggers externally' do
24
+ it "prevents altering the list of triggers externally" do
25
25
  original_trigger_list = TestContext.triggers
26
- TestContext.triggers << 'another_trigger'
26
+ TestContext.triggers << "another_trigger"
27
27
  assert_equal original_trigger_list, TestContext.triggers
28
28
  end
29
29
  end
30
30
 
31
- describe Surrounded::Context, '.trigger' do
32
- let(:user){ User.new("Jim") }
33
- let(:other_user){ User.new("Guille") }
34
- let(:context){ TestContext.new(user: user, other_user: other_user) }
31
+ describe Surrounded::Context, ".trigger" do
32
+ let(:user) { User.new("Jim") }
33
+ let(:other_user) { User.new("Guille") }
34
+ let(:context) { TestContext.new(user: user, other_user: other_user) }
35
35
 
36
- it 'defines a public method on the context' do
36
+ it "defines a public method on the context" do
37
37
  assert context.respond_to?(:access_other_object)
38
38
  end
39
39
 
40
- it 'gives objects access to each other inside the method' do
41
- assert_raises(NoMethodError){
40
+ it "gives objects access to each other inside the method" do
41
+ assert_raises(NoMethodError) {
42
42
  user.other_user
43
43
  }
44
44
  assert_equal "Guille", context.access_other_object
45
45
  end
46
46
 
47
- it 'preserves arguments and blocks' do
48
- result = context.block_method('argument') do |*args, obj|
47
+ it "preserves arguments and blocks" do
48
+ result = context.block_method("argument") do |*args, obj|
49
49
  "Having an #{args.first} with #{obj.class}"
50
50
  end
51
51
  assert_equal "Having an argument with TestContext", result
52
52
  end
53
53
 
54
- it 'allows usage of regular methods for triggers' do
54
+ it "allows usage of regular methods for triggers" do
55
55
  assert context.regular_method_trigger
56
56
  end
57
57
 
58
- it 'ignores nil trigger names' do
58
+ it "ignores nil trigger names" do
59
59
  assert context.class.send(:trigger)
60
60
  end
61
61
  end
62
62
 
63
- describe Surrounded::Context, '#role?' do
64
- let(:user){
63
+ describe Surrounded::Context, "#role?" do
64
+ let(:user) {
65
65
  test_user = User.new("Jim")
66
66
 
67
67
  def test_user.get_role(name, context)
68
- context.role?(name){}
68
+ context.role?(name) {}
69
69
  end
70
70
 
71
71
  test_user
72
72
  }
73
- let(:other_user){ User.new("Guille") }
74
- let(:external){
73
+ let(:other_user) { User.new("Guille") }
74
+ let(:external) {
75
75
  external_object = Object.new
76
76
  def external_object.get_role_from_context(name, context)
77
- context.role?(name){}
77
+ context.role?(name) {}
78
78
  end
79
79
  external_object
80
80
  }
81
- let(:context){ TestContext.new(user: user, other_user: other_user) }
81
+ let(:context) { TestContext.new(user: user, other_user: other_user) }
82
82
 
83
- it 'returns the object assigned to the named role' do
83
+ it "returns the object assigned to the named role" do
84
84
  assert_equal user, user.get_role(:user, context)
85
85
  end
86
86
 
87
- it 'returns false if the role does not exist' do
87
+ it "returns false if the role does not exist" do
88
88
  refute user.get_role(:non_existant_role, context)
89
89
  end
90
90
 
91
- it 'returns false if the accessing object is not a role player in the context' do
91
+ it "returns false if the accessing object is not a role player in the context" do
92
92
  refute external.get_role_from_context(:user, context)
93
93
  end
94
94
 
95
- it 'checks for the role based upon the calling object' do
96
- refute context.role?(:user){} # this test is the caller
95
+ it "checks for the role based upon the calling object" do
96
+ refute context.role?(:user) {} # this test is the caller
97
97
  end
98
98
  end
99
99
 
100
- describe Surrounded::Context, '#role_player?' do
101
- let(:player){ User.new("Jim") }
102
- let(:other_player){ User.new("Amy") }
103
- let(:non_player){ User.new("Guille") }
104
- let(:context){ TestContext.new(user: player, other_user: other_player) }
100
+ describe Surrounded::Context, "#role_player?" do
101
+ let(:player) { User.new("Jim") }
102
+ let(:other_player) { User.new("Amy") }
103
+ let(:non_player) { User.new("Guille") }
104
+ let(:context) { TestContext.new(user: player, other_user: other_player) }
105
105
 
106
- it 'is true if the given object is a role player' do
106
+ it "is true if the given object is a role player" do
107
107
  expect(context.role_player?(player)).must_equal true
108
108
  end
109
109
 
110
- it 'is false if the given oject is not a role player' do
110
+ it "is false if the given oject is not a role player" do
111
111
  expect(context.role_player?(non_player)).must_equal false
112
112
  end
113
113
  end
@@ -133,25 +133,30 @@ class RoleAssignmentContext
133
133
  trigger :check_other_user_response do
134
134
  user.respond_to?(:a_method!)
135
135
  end
136
-
136
+
137
137
  trigger :user_ancestors, :other_user_ancestors
138
138
 
139
139
  module User
140
- def a_method!; end
140
+ def a_method!
141
+ end
141
142
  end
143
+
142
144
  module OtherUser
143
- def a_method!; end
145
+ def a_method!
146
+ end
144
147
  end
145
148
  end
146
149
 
147
150
  class Special; end
151
+
148
152
  class IgnoreExternalConstantsContext
149
153
  extend Surrounded::Context
150
154
 
151
155
  initialize :user, :special, :other
152
156
 
153
157
  role :other do
154
- def something_or_other; end
158
+ def something_or_other
159
+ end
155
160
  end
156
161
 
157
162
  trigger :check_special do
@@ -170,44 +175,48 @@ class ClassRoleAssignmentContext
170
175
 
171
176
  class Thing
172
177
  include Surrounded
173
- def initialize(obj); end
174
- def method_from_class; end
175
- end
176
178
 
179
+ def initialize(obj)
180
+ @obj = obj
181
+ end
182
+
183
+ def method_from_class
184
+ end
185
+ end
177
186
  end
178
187
 
179
- describe Surrounded::Context, 'assigning roles' do
188
+ describe Surrounded::Context, "assigning roles" do
180
189
  include Surrounded # the test must be context-aware
181
190
 
182
- let(:user){ User.new("Jim") }
183
- let(:other_user){ CastingUser.new("Guille") }
184
- let(:context){ RoleAssignmentContext.new(user: user, other_user: other_user) }
191
+ let(:user) { User.new("Jim") }
192
+ let(:other_user) { CastingUser.new("Guille") }
193
+ let(:context) { RoleAssignmentContext.new(user: user, other_user: other_user) }
185
194
 
186
- it 'tries to use casting to add roles' do
195
+ it "tries to use casting to add roles" do
187
196
  refute_includes(context.other_user_ancestors, RoleAssignmentContext::OtherUser)
188
197
  end
189
198
 
190
- it 'extends objects with role modules failing casting' do
199
+ it "extends objects with role modules failing casting" do
191
200
  assert_includes(context.user_ancestors, RoleAssignmentContext::User)
192
201
  end
193
202
 
194
- it 'sets role players to respond to role methods' do
203
+ it "sets role players to respond to role methods" do
195
204
  assert context.check_user_response
196
205
  assert context.check_other_user_response
197
206
  end
198
207
 
199
- it 'will use classes as roles' do
200
- user = User.new('Jim')
208
+ it "will use classes as roles" do
209
+ user = User.new("Jim")
201
210
 
202
211
  context = ClassRoleAssignmentContext.new(thing: user, the_test: self)
203
212
 
204
213
  assert context.check_user_response
205
214
  end
206
215
 
207
- it 'does not use constants defined outside the context class' do
208
- special = User.new('Special')
209
- other = User.new('Other')
216
+ it "does not use constants defined outside the context class" do
217
+ special = User.new("Special")
218
+ other = User.new("Other")
210
219
  context = IgnoreExternalConstantsContext.new(user: user, special: special, other: other)
211
220
  assert_equal User, context.check_special
212
221
  end
213
- end
222
+ end
@@ -1,21 +1,19 @@
1
- require 'test_helper'
1
+ require "test_helper"
2
2
 
3
- describe "Surrounded", 'without context' do
4
-
5
- let(:jim){ User.new("Jim") }
3
+ describe "Surrounded", "without context" do
4
+ let(:jim) { User.new("Jim") }
6
5
 
7
6
  it "never has context roles" do
8
- assert_nil jim.send(:context).role?('anything')
7
+ assert_nil jim.send(:context).role?("anything")
9
8
  end
10
-
11
9
  end
12
10
 
13
11
  describe "Surrounded" do
14
- let(:jim){ User.new("Jim") }
15
- let(:guille){ User.new("Guille") }
16
- let(:external_user){ User.new("External User") }
12
+ let(:jim) { User.new("Jim") }
13
+ let(:guille) { User.new("Guille") }
14
+ let(:external_user) { User.new("External User") }
17
15
 
18
- let(:context){
16
+ let(:context) {
19
17
  TestContext.new(user: jim, other_user: guille)
20
18
  }
21
19
 
@@ -24,7 +22,7 @@ describe "Surrounded" do
24
22
  end
25
23
 
26
24
  it "prevents access to context objects for external objects" do
27
- assert_raises(NoMethodError){
25
+ assert_raises(NoMethodError) {
28
26
  external_user.user
29
27
  }
30
28
  end
@@ -37,14 +35,14 @@ end
37
35
  describe "Surrounded", "added to an existing object" do
38
36
  it "allows the object to store its context" do
39
37
  thing = UnsurroundedObject.new
40
- thing.name = 'Jim'
38
+ thing.name = "Jim"
41
39
 
42
- assert_raises(NoMethodError){
40
+ assert_raises(NoMethodError) {
43
41
  thing.__send__(:store_context)
44
42
  }
45
43
  thing.extend(Surrounded)
46
44
 
47
- other = User.new('Guille')
45
+ other = User.new("Guille")
48
46
 
49
47
  context = TestContext.new(user: thing, other_user: other)
50
48
  assert context.access_other_object
@@ -57,7 +55,7 @@ end
57
55
 
58
56
  describe "Surrounded", "added to an object through another module" do
59
57
  it "allows the object to store its context" do
60
- object = Array.new
58
+ object = []
61
59
  object.extend(SpecialSurrounding)
62
60
  assert object.respond_to?(:context, true)
63
61
  end
data/test/test_helper.rb CHANGED
@@ -1,12 +1,15 @@
1
- require 'simplecov'
2
- require 'minitest/autorun'
3
- SimpleCov.start
1
+ require "simplecov"
2
+ require "minitest/autorun"
3
+ SimpleCov.enable_coverage :branch
4
+ SimpleCov.add_filter %r{version.rb}
5
+ SimpleCov.start unless defined?(Coverage)
4
6
 
5
- require 'surrounded'
6
- require 'surrounded/context'
7
+ require "surrounded"
8
+ require "surrounded/context"
7
9
 
8
10
  class User
9
11
  include Surrounded
12
+
10
13
  def initialize(name)
11
14
  @name = name
12
15
  end
@@ -0,0 +1,70 @@
1
+ require "test_helper"
2
+ require "async"
3
+
4
+ class AsyncThreadedContext
5
+ extend Surrounded::Context
6
+
7
+ def initialize(leader:, members:)
8
+ role_names = [:leader, :members]
9
+ role_players = [leader, members]
10
+
11
+ role_names.concat(members.map { |member| :"member_#{member.object_id}" })
12
+ role_players.concat(members)
13
+
14
+ map_roles(role_names.zip(role_players))
15
+ end
16
+ private_attr_reader :leader, :members
17
+
18
+ trigger :meet do
19
+ result = []
20
+ result << leader.greet
21
+ result << members.concurrent_map do |member|
22
+ result << member.greet
23
+ end
24
+ result.flatten.join(" ")
25
+ end
26
+
27
+ module Leader
28
+ def greet
29
+ "Hello everyone. I am #{name}"
30
+ end
31
+ end
32
+
33
+ module Member
34
+ def greet
35
+ "Hello #{leader.name}, I am #{name}"
36
+ end
37
+ end
38
+
39
+ module Members
40
+ include Surrounded
41
+
42
+ def concurrent_map
43
+ Async do
44
+ map do |member|
45
+ yield member
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ describe AsyncThreadedContext do
53
+ let(:jim) { User.new("Jim") }
54
+ let(:amy) { User.new("Amy") }
55
+ let(:guille) { User.new("Guille") }
56
+ let(:jason) { User.new("Jason") }
57
+ let(:dave) { User.new("Dave") }
58
+
59
+ let(:greeter) { jim }
60
+ let(:members) { [amy, guille, jason, dave] }
61
+
62
+ it "works in multi-threaded environments with async" do
63
+ meeting = AsyncThreadedContext.new(leader: jim, members: members)
64
+
65
+ result = meeting.meet
66
+
67
+ assert_includes result, "Hello everyone. I am Jim"
68
+ assert_includes result, "Hello Jim, I am Amy"
69
+ end
70
+ end
metadata CHANGED
@@ -1,43 +1,28 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: surrounded
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - "'Jim Gay'"
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2017-04-30 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: triad
15
14
  requirement: !ruby/object:Gem::Requirement
16
15
  requirements:
17
- - - "~>"
16
+ - - ">="
18
17
  - !ruby/object:Gem::Version
19
18
  version: 0.3.0
20
19
  type: :runtime
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
- - - "~>"
23
+ - - ">="
25
24
  - !ruby/object:Gem::Version
26
25
  version: 0.3.0
27
- - !ruby/object:Gem::Dependency
28
- name: bundler
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '1.12'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '1.12'
41
26
  - !ruby/object:Gem::Dependency
42
27
  name: rake
43
28
  requirement: !ruby/object:Gem::Requirement
@@ -59,18 +44,10 @@ executables: []
59
44
  extensions: []
60
45
  extra_rdoc_files: []
61
46
  files:
62
- - ".codeclimate.yml"
63
- - ".gitignore"
64
- - ".pullreview.yml"
65
- - ".simplecov"
66
- - ".travis.yml"
67
47
  - Changelog.md
68
- - Gemfile
69
48
  - LICENSE.txt
70
49
  - README.md
71
50
  - Rakefile
72
- - examples/bottles.rb
73
- - examples/rails.rb
74
51
  - lib/surrounded.rb
75
52
  - lib/surrounded/access_control.rb
76
53
  - lib/surrounded/context.rb
@@ -80,13 +57,14 @@ files:
80
57
  - lib/surrounded/context/negotiator.rb
81
58
  - lib/surrounded/context/role_builders.rb
82
59
  - lib/surrounded/context/role_map.rb
60
+ - lib/surrounded/context/seclusion.rb
83
61
  - lib/surrounded/context/trigger_controls.rb
84
62
  - lib/surrounded/east_oriented.rb
85
63
  - lib/surrounded/exceptions.rb
86
64
  - lib/surrounded/shortcuts.rb
87
65
  - lib/surrounded/version.rb
88
66
  - surrounded.gemspec
89
- - test/casting_role_player_test.rb
67
+ - test/casting_test_helper.rb
90
68
  - test/collection_role_players_test.rb
91
69
  - test/context_access_test.rb
92
70
  - test/context_forwarding_test.rb
@@ -105,11 +83,11 @@ files:
105
83
  - test/surrounded_context_test.rb
106
84
  - test/surrounded_test.rb
107
85
  - test/test_helper.rb
86
+ - test/threaded_context_test.rb
108
87
  homepage: http://github.com/saturnflyer/surrounded
109
88
  licenses:
110
89
  - MIT
111
90
  metadata: {}
112
- post_install_message:
113
91
  rdoc_options: []
114
92
  require_paths:
115
93
  - lib
@@ -124,28 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
102
  - !ruby/object:Gem::Version
125
103
  version: '0'
126
104
  requirements: []
127
- rubyforge_project:
128
- rubygems_version: 2.6.8
129
- signing_key:
105
+ rubygems_version: 3.7.2
130
106
  specification_version: 4
131
107
  summary: Create encapsulated environments for your objects.
132
- test_files:
133
- - test/casting_role_player_test.rb
134
- - test/collection_role_players_test.rb
135
- - test/context_access_test.rb
136
- - test/context_forwarding_test.rb
137
- - test/context_reuse_test.rb
138
- - test/context_shortcuts_test.rb
139
- - test/east_oriented_triggers_test.rb
140
- - test/example_delegate_class_test.rb
141
- - test/example_proxy_test.rb
142
- - test/example_threaded_test.rb
143
- - test/example_wrapper_test.rb
144
- - test/initialization_test.rb
145
- - test/name_collisions_test.rb
146
- - test/non_surrounded_role_player_test.rb
147
- - test/override_methods_test.rb
148
- - test/role_context_method_test.rb
149
- - test/surrounded_context_test.rb
150
- - test/surrounded_test.rb
151
- - test/test_helper.rb
108
+ test_files: []
data/.codeclimate.yml DELETED
@@ -1,4 +0,0 @@
1
- languages:
2
- Ruby: true
3
- exclude_paths:
4
- - "examples"
data/.gitignore DELETED
@@ -1,19 +0,0 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- .rvmrc
7
- .DS_Store
8
- Gemfile.lock
9
- InstalledFiles
10
- _yardoc
11
- coverage
12
- doc/
13
- lib/bundler/man
14
- pkg
15
- rdoc
16
- spec/reports
17
- test/tmp
18
- test/version_tmp
19
- tmp
data/.pullreview.yml DELETED
@@ -1,4 +0,0 @@
1
- ---
2
- notifications:
3
- pullrequest:
4
- comment: verbose
data/.simplecov DELETED
@@ -1,3 +0,0 @@
1
- SimpleCov.start do
2
- add_filter 'test'
3
- end
data/.travis.yml DELETED
@@ -1,19 +0,0 @@
1
- before_install:
2
- - gem install bundler
3
- language: ruby
4
- cache: bundler
5
- rvm:
6
- - 2.4.1
7
- - 2.3.4
8
- - 2.2.7
9
- - ruby-head
10
- - jruby-head
11
- matrix:
12
- allow_failures:
13
- - rvm: ruby-head
14
- - rvm: jruby-head
15
- addons:
16
- code_climate:
17
- repo_token: 7488b157e7b7f48eac865a9f830fe90a39e6ac10b17f854e17b9529e1854762c
18
- after_success:
19
- - bundle exec codeclimate-test-reporter
data/Gemfile DELETED
@@ -1,10 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- group :test do
4
- gem 'minitest'
5
- gem "simplecov"
6
- gem 'codeclimate-test-reporter', '~> 1.0.8'
7
- gem 'casting'
8
- end
9
-
10
- gemspec