surrounded 0.9.11 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,10 +17,35 @@ class ShortcutContext
17
17
  end
18
18
  end
19
19
 
20
+ class ShortcutContextNoKeywords
21
+ extend Surrounded::Context
22
+ shortcut_triggers
23
+
24
+ initialize_without_keywords :user, :other
25
+
26
+ trigger :shorty do
27
+ user.speak
28
+ end
29
+
30
+ role :user do
31
+ def speak
32
+ 'it works, shorty!'
33
+ end
34
+ end
35
+ end
36
+
20
37
  describe Surrounded::Context, 'shortcuts' do
21
38
  let(:user){ User.new("Jim") }
22
39
  let(:other){ User.new("Guille") }
23
40
  it 'creates shortcut class methods for triggers' do
24
- assert_equal 'it works, shorty!', ShortcutContext.shorty(user, other)
41
+ assert_equal 'it works, shorty!', ShortcutContext.shorty(user: user, other: other)
25
42
  end
26
- end
43
+ end
44
+
45
+ describe Surrounded::Context, 'shortcuts with initialize_without_keywords' do
46
+ let(:user){ User.new("Jim") }
47
+ let(:other){ User.new("Guille") }
48
+ it 'creates shortcut class methods for triggers' do
49
+ assert_equal 'it works, shorty!', ShortcutContextNoKeywords.shorty(user, other)
50
+ end
51
+ end
@@ -14,7 +14,7 @@ end
14
14
  describe Surrounded::Context, '.east_oriented_triggers' do
15
15
  let(:user){ User.new("Jim") }
16
16
  let(:other_user){ User.new("Guille") }
17
- let(:context){ EastTestContext.new(user, other_user) }
17
+ let(:context){ EastTestContext.new(user: user, other_user: other_user) }
18
18
 
19
19
  it 'returns the context object from trigger methods' do
20
20
  assert_equal context, context.ask?
@@ -24,8 +24,8 @@ end
24
24
  describe Surrounded::Context, '.east_oriented_triggers with protect_triggers' do
25
25
  let(:user){ User.new("Jim") }
26
26
  let(:other_user){ User.new("Guille") }
27
- let(:context){
28
- ctxt = EastTestContext.new(user, other_user)
27
+ let(:context){
28
+ ctxt = EastTestContext.new(user: user, other_user: other_user)
29
29
  ctxt.singleton_class.send(:protect_triggers)
30
30
  ctxt
31
31
  }
@@ -33,4 +33,4 @@ describe Surrounded::Context, '.east_oriented_triggers with protect_triggers' do
33
33
  it 'returns the context object from trigger methods' do
34
34
  assert_equal context, context.ask?
35
35
  end
36
- end
36
+ end
@@ -21,7 +21,7 @@ end
21
21
 
22
22
  describe DelegateClassContext do
23
23
  let(:context){
24
- DelegateClassContext.new(User.new('jim'), Object.new)
24
+ DelegateClassContext.new(user: User.new('jim'), task: Object.new)
25
25
  }
26
26
  it 'wraps objects using DelegateClass' do
27
27
  assert_equal 'hello from the admin DelegateClass wrapper!', context.do_something
@@ -66,7 +66,7 @@ describe ProxyContext do
66
66
  OpenStruct.new(name: 'GTD')
67
67
  }
68
68
  let(:context){
69
- ProxyContext.new(user, task)
69
+ ProxyContext.new(admin: user, task: task)
70
70
  }
71
71
  it 'proxys methods between objects and its interface' do
72
72
  assert_equal 'hello from Jim, the admin interface!', context.do_something
@@ -77,7 +77,7 @@ describe ProxyContext do
77
77
  end
78
78
 
79
79
  it 'passes missing methods up the ancestry of the object' do
80
- err = ->{ context.admin_missing_method }.must_raise(NoMethodError)
80
+ err = _{ context.admin_missing_method }.must_raise(NoMethodError)
81
81
 
82
82
  assert_match(/ProxyUser.*name="Jim"/, err.message)
83
83
  end
@@ -105,7 +105,7 @@ describe ProxyContext do
105
105
  end
106
106
 
107
107
  it 'allows Surrounded objects to interact with others' do
108
- assert context.rebind(user: User.new('Surrounded'), task: task).talking
108
+ assert context.rebind(admin: User.new('Surrounded'), task: task).talking
109
109
  end
110
110
 
111
111
  it 'works with frozen and primitive objects' do
@@ -54,7 +54,7 @@ describe ThreadedContext do
54
54
  let(:members){ [amy, guille, jason, dave] }
55
55
 
56
56
  it 'works in multi-threaded environments' do
57
- meeting = ThreadedContext.new(jim, members)
57
+ meeting = ThreadedContext.new(leader: jim, members: members)
58
58
 
59
59
  result = meeting.meet
60
60
 
@@ -20,7 +20,7 @@ end
20
20
 
21
21
  describe WrapperContext do
22
22
  let(:context){
23
- WrapperContext.new(Object.new, Object.new)
23
+ WrapperContext.new(admin: Object.new, task: Object.new)
24
24
  }
25
25
  it 'wraps objects and allows them to respond to new methods' do
26
26
  assert_equal 'hello from the admin wrapper!', context.do_something
@@ -1,58 +1,66 @@
1
1
  require 'test_helper'
2
2
 
3
- class InitContext
3
+ class KeywordContext
4
4
  extend Surrounded::Context
5
5
 
6
- initialize(:user, :other_user) do
6
+ keyword_initialize(:user, :other_user) do
7
7
  @defined_by_initializer_block = 'yup'
8
8
  end
9
9
  end
10
10
 
11
11
  describe Surrounded::Context, '.initialize' do
12
- it 'defines an initialize method accepting the same arguments' do
13
- assert_equal 2, InitContext.instance_method(:initialize).arity
14
- end
15
12
 
16
13
  it 'applies a provided block to the instance' do
17
- context = InitContext.new(User.new('Jim'), User.new('Amy'))
14
+ context = KeywordContext.new(user: User.new('Jim'), other_user: User.new('Amy'))
18
15
  assert_equal 'yup', context.instance_variable_get(:@defined_by_initializer_block)
19
16
  end
20
17
 
21
18
  it 'keeps track of the original initialize arguments' do
22
19
  jim = User.new('Jim')
23
20
  amy = User.new('Amy')
24
- context = InitContext.new(jim, amy)
21
+ context = KeywordContext.new(user: jim, other_user: amy)
25
22
  tracked = context.send(:initializer_arguments)
26
23
  assert_equal jim, tracked[:user]
27
24
  assert_equal amy, tracked[:other_user]
28
25
  end
26
+
27
+ it 'raises errors with missing keywords' do
28
+ err = assert_raises(ArgumentError){
29
+ KeywordContext.new(other_user: User.new('Amy'))
30
+ }
31
+ assert_match(/missing keyword: :?user/, err.message)
32
+ end
29
33
  end
30
34
 
31
- begin
32
- class Keyworder
33
- extend Surrounded::Context
35
+ class NonKeyworder
36
+ extend Surrounded::Context
37
+
38
+ initialize_without_keywords :this, :that do
39
+ self.instance_variable_set(:@defined_by_initializer_block, 'yes')
40
+ end
41
+
42
+ trigger :access_other_object do
43
+ that.name
44
+ end
45
+ end
34
46
 
35
- keyword_initialize :this, :that do
36
- self.instance_variable_set(:@defined_by_initializer_block, 'yes')
37
- end
47
+ describe Surrounded::Context, 'non-keyword initializers' do
48
+ it 'defines an initialize method accepting the same arguments' do
49
+ assert_equal 2, NonKeyworder.instance_method(:initialize).arity
38
50
  end
39
51
 
40
- describe Surrounded::Context, 'keyword initializers' do
41
- it 'works with keyword arguments' do
42
- assert Keyworder.new(this: User.new('Jim'), that: User.new('Guille'))
43
- end
52
+ it 'works without keyword arguments' do
53
+ assert NonKeyworder.new(User.new('Jim'), User.new('Guille'))
54
+ end
44
55
 
45
- it 'raises errors with missing keywords' do
46
- err = assert_raises(ArgumentError){
47
- Keyworder.new(this: User.new('Amy'))
48
- }
49
- assert_match(/missing keyword: that/, err.message)
50
- end
56
+ it 'evaluates a given block' do
57
+ assert_equal 'yes', NonKeyworder.new(User.new('Jim'), User.new('Guille')).instance_variable_get(:@defined_by_initializer_block)
58
+ end
51
59
 
52
- it 'evaluates a given block' do
53
- assert_equal 'yes', Keyworder.new(this: User.new('Jim'), that: User.new('Guille')).instance_variable_get(:@defined_by_initializer_block)
54
- end
60
+ it 'allows rebinding with a hash' do
61
+ context = NonKeyworder.new(User.new('Jim'), User.new('Guille'))
62
+ expect(context.access_other_object).must_equal 'Guille'
63
+ context.rebind(this: User.new('Amy'), that: User.new('Elizabeth'))
64
+ expect(context.access_other_object).must_equal 'Elizabeth'
55
65
  end
56
- rescue SyntaxError
57
- STDOUT.puts "No support for keywords"
58
- end
66
+ end
@@ -49,7 +49,7 @@ describe Surrounded::Context, 'custom role application' do
49
49
  let(:user){ User.new('Jim') }
50
50
  let(:other){ User.new('Amy') }
51
51
 
52
- let(:context){ PrependedRoles.new(user, other) }
52
+ let(:context){ PrependedRoles.new(user: user, other: other) }
53
53
 
54
54
  it 'allows you to override existing methods on a role player' do
55
55
  assert_equal "Not what you thought, Jim", context.get_name
@@ -53,11 +53,35 @@ describe Surrounded::Context, '.role' do
53
53
  def hello
54
54
  'hello from admin'
55
55
  end
56
+
57
+ def splat_args(*args)
58
+ args
59
+ end
60
+
61
+ def keyword_args(**kwargs)
62
+ kwargs
63
+ end
64
+
65
+ def mixed_args(*args, **kwargs)
66
+ [args, kwargs]
67
+ end
56
68
  end
57
69
 
58
70
  trigger :admin_hello do
59
71
  admin.hello
60
72
  end
73
+
74
+ trigger :splat_args do |*args|
75
+ admin.splat_args(*args)
76
+ end
77
+
78
+ trigger :keyword_args do |**kwargs|
79
+ admin.keyword_args(**kwargs)
80
+ end
81
+
82
+ trigger :mixed_args do |*args, **kwargs|
83
+ admin.mixed_args(*args, **kwargs)
84
+ end
61
85
  end
62
86
 
63
87
  class Hello
@@ -69,7 +93,7 @@ describe Surrounded::Context, '.role' do
69
93
 
70
94
  describe 'interfaces' do
71
95
  let(:context){
72
- InterfaceContext.new(Hello.new, Hello.new)
96
+ InterfaceContext.new(admin: Hello.new, other: Hello.new)
73
97
  }
74
98
  it 'sets interface objects to use interface methods before singleton methods' do
75
99
  assert_equal 'hello from admin', context.admin_hello
@@ -85,6 +109,18 @@ describe Surrounded::Context, '.role' do
85
109
  it 'creates a private accessor method' do
86
110
  assert context.respond_to?(:admin, true)
87
111
  end
112
+
113
+ it 'works with multiple args' do
114
+ assert_equal context.splat_args("one", "two"), %w[ one two ]
115
+ end
116
+
117
+ it 'works with multiple keyword args' do
118
+ assert_equal context.keyword_args(one: "one", two: "two"), { one: "one", two: "two" }
119
+ end
120
+
121
+ it 'works with multiple mixed args' do
122
+ assert_equal context.mixed_args("one", :two, three: "three", four: "four"), [["one", :two], { three: "three", four: "four" }]
123
+ end
88
124
  end
89
125
 
90
126
  describe 'unknown' do
@@ -121,7 +157,7 @@ describe Surrounded::Context, '.role' do
121
157
  the_test.assert_kind_of SimpleDelegator, admin
122
158
  end
123
159
  end
124
- context = CustomDefaultWrap.new(Object.new, self)
160
+ context = CustomDefaultWrap.new(admin: Object.new, the_test: self)
125
161
  context.check_admin_type
126
162
  end
127
163
 
@@ -142,11 +178,11 @@ describe Surrounded::Context, '.role' do
142
178
  end
143
179
  end
144
180
 
145
- context = CustomGlobalDefault.new(Object.new, self)
181
+ context = CustomGlobalDefault.new(admin: Object.new, the_test: self)
146
182
  context.check_admin_type
147
183
  ensure
148
184
  Surrounded::Context.default_role_type = old_default
149
185
  end
150
186
  end
151
187
  end
152
- end
188
+ end
@@ -3,7 +3,7 @@ require 'test_helper'
3
3
  describe Surrounded::Context, '#triggers' do
4
4
  let(:user){ User.new("Jim") }
5
5
  let(:other_user){ User.new("Guille") }
6
- let(:context){ TestContext.new(user, other_user) }
6
+ let(:context){ TestContext.new(user: user, other_user: other_user) }
7
7
 
8
8
  it 'lists the externally accessible trigger methods' do
9
9
  assert context.triggers.include?(:access_other_object)
@@ -31,7 +31,7 @@ end
31
31
  describe Surrounded::Context, '.trigger' do
32
32
  let(:user){ User.new("Jim") }
33
33
  let(:other_user){ User.new("Guille") }
34
- let(:context){ TestContext.new(user, other_user) }
34
+ let(:context){ TestContext.new(user: user, other_user: other_user) }
35
35
 
36
36
  it 'defines a public method on the context' do
37
37
  assert context.respond_to?(:access_other_object)
@@ -78,7 +78,7 @@ describe Surrounded::Context, '#role?' do
78
78
  end
79
79
  external_object
80
80
  }
81
- let(:context){ TestContext.new(user, other_user) }
81
+ let(:context){ TestContext.new(user: user, other_user: other_user) }
82
82
 
83
83
  it 'returns the object assigned to the named role' do
84
84
  assert_equal user, user.get_role(:user, context)
@@ -101,7 +101,7 @@ describe Surrounded::Context, '#role_player?' do
101
101
  let(:player){ User.new("Jim") }
102
102
  let(:other_player){ User.new("Amy") }
103
103
  let(:non_player){ User.new("Guille") }
104
- let(:context){ TestContext.new(player, other_player) }
104
+ let(:context){ TestContext.new(user: player, other_user: other_player) }
105
105
 
106
106
  it 'is true if the given object is a role player' do
107
107
  expect(context.role_player?(player)).must_equal true
@@ -181,7 +181,7 @@ describe Surrounded::Context, 'assigning roles' do
181
181
 
182
182
  let(:user){ User.new("Jim") }
183
183
  let(:other_user){ CastingUser.new("Guille") }
184
- let(:context){ RoleAssignmentContext.new(user, other_user) }
184
+ let(:context){ RoleAssignmentContext.new(user: user, other_user: other_user) }
185
185
 
186
186
  it 'tries to use casting to add roles' do
187
187
  refute_includes(context.other_user_ancestors, RoleAssignmentContext::OtherUser)
@@ -199,7 +199,7 @@ describe Surrounded::Context, 'assigning roles' do
199
199
  it 'will use classes as roles' do
200
200
  user = User.new('Jim')
201
201
 
202
- context = ClassRoleAssignmentContext.new(user, self)
202
+ context = ClassRoleAssignmentContext.new(thing: user, the_test: self)
203
203
 
204
204
  assert context.check_user_response
205
205
  end
@@ -207,7 +207,7 @@ describe Surrounded::Context, 'assigning roles' do
207
207
  it 'does not use constants defined outside the context class' do
208
208
  special = User.new('Special')
209
209
  other = User.new('Other')
210
- context = IgnoreExternalConstantsContext.new(user, special, other)
210
+ context = IgnoreExternalConstantsContext.new(user: user, special: special, other: other)
211
211
  assert_equal User, context.check_special
212
212
  end
213
213
  end
@@ -16,7 +16,7 @@ describe "Surrounded" do
16
16
  let(:external_user){ User.new("External User") }
17
17
 
18
18
  let(:context){
19
- TestContext.new(jim, guille)
19
+ TestContext.new(user: jim, other_user: guille)
20
20
  }
21
21
 
22
22
  it "has access to objects in the context" do
@@ -46,7 +46,7 @@ describe "Surrounded", "added to an existing object" do
46
46
 
47
47
  other = User.new('Guille')
48
48
 
49
- context = TestContext.new(thing, other)
49
+ context = TestContext.new(user: thing, other_user: other)
50
50
  assert context.access_other_object
51
51
  end
52
52
  end
@@ -61,4 +61,4 @@ describe "Surrounded", "added to an object through another module" do
61
61
  object.extend(SpecialSurrounding)
62
62
  assert object.respond_to?(:context, true)
63
63
  end
64
- end
64
+ end
data/test/test_helper.rb CHANGED
@@ -1,10 +1,8 @@
1
1
  require 'simplecov'
2
- require 'minitest/autorun' unless ENV['MUTANT']
3
- require 'coveralls'
4
-
5
- if ENV['COVERALLS']
6
- Coveralls.wear!
7
- end
2
+ require 'minitest/autorun'
3
+ SimpleCov.enable_coverage :branch
4
+ SimpleCov.add_filter %r{version.rb}
5
+ SimpleCov.start unless defined?(Coverage)
8
6
 
9
7
  require 'surrounded'
10
8
  require 'surrounded/context'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: surrounded
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.11
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "'Jim Gay'"
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-09 00:00:00.000000000 Z
11
+ date: 2022-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: triad
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  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
27
  - !ruby/object:Gem::Dependency
42
28
  name: rake
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -60,10 +46,11 @@ extensions: []
60
46
  extra_rdoc_files: []
61
47
  files:
62
48
  - ".codeclimate.yml"
49
+ - ".github/workflows/codeql-analysis.yml"
50
+ - ".github/workflows/test.yml"
63
51
  - ".gitignore"
64
52
  - ".pullreview.yml"
65
53
  - ".simplecov"
66
- - ".travis.yml"
67
54
  - Changelog.md
68
55
  - Gemfile
69
56
  - LICENSE.txt
@@ -80,6 +67,7 @@ files:
80
67
  - lib/surrounded/context/negotiator.rb
81
68
  - lib/surrounded/context/role_builders.rb
82
69
  - lib/surrounded/context/role_map.rb
70
+ - lib/surrounded/context/seclusion.rb
83
71
  - lib/surrounded/context/trigger_controls.rb
84
72
  - lib/surrounded/east_oriented.rb
85
73
  - lib/surrounded/exceptions.rb
@@ -109,7 +97,7 @@ homepage: http://github.com/saturnflyer/surrounded
109
97
  licenses:
110
98
  - MIT
111
99
  metadata: {}
112
- post_install_message:
100
+ post_install_message:
113
101
  rdoc_options: []
114
102
  require_paths:
115
103
  - lib
@@ -124,9 +112,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
112
  - !ruby/object:Gem::Version
125
113
  version: '0'
126
114
  requirements: []
127
- rubyforge_project:
128
- rubygems_version: 2.5.1
129
- signing_key:
115
+ rubygems_version: 3.3.23
116
+ signing_key:
130
117
  specification_version: 4
131
118
  summary: Create encapsulated environments for your objects.
132
119
  test_files:
@@ -149,4 +136,3 @@ test_files:
149
136
  - test/surrounded_context_test.rb
150
137
  - test/surrounded_test.rb
151
138
  - test/test_helper.rb
152
- has_rdoc:
data/.travis.yml DELETED
@@ -1,16 +0,0 @@
1
- before_install:
2
- - gem install bundler
3
- language: ruby
4
- cache: bundler
5
- rvm:
6
- - 2.3.1
7
- - 2.2.5
8
- - 2.1.10
9
- - ruby-head
10
- - jruby-head
11
- env:
12
- - COVERALLS=true
13
- matrix:
14
- allow_failures:
15
- - rvm: ruby-head
16
- - rvm: jruby-head