surrounded 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5b50df20142b16850a46c0824115d7286d5c5db0
4
+ data.tar.gz: bbf2f02bc6d2bf089f8a1a6c33627cac0efb8e43
5
+ SHA512:
6
+ metadata.gz: 4d1badd06972d938d14bc50ea50d8d074c882a4ebf2728724de48131e875f55f75dcdd019fbcdd64322eb6591d414ec9fccbe12cc106518fa3d309a93ab31294
7
+ data.tar.gz: b3aa0cfaaec0178fbe799fff2b64d16f4565be58eb6d95e29eb46542a7ef35a7bc50ddc80bae8b51ff5b614277ff3079846d4a872697e8d2c11364f3984781ea
data/README.md CHANGED
@@ -244,17 +244,21 @@ You might find that useful for dynamically defining user interfaces.
244
244
 
245
245
  Sometimes I'd rather not use this DSL, however. I want to just write regular methods.
246
246
 
247
- We can do that too. You'll need to opt in to this by specifying `set_methods_as_triggers` for the context class.
247
+ We can do that too. You'll need to opt in to this by specifying `trigger :your_method_name` for the methods you want to use.
248
248
 
249
249
  ```ruby
250
250
  class MyEnvironment
251
251
  # other stuff from above is still here...
252
-
253
- set_methods_as_triggers
254
252
 
255
253
  def shove_it
256
254
  employee.quit
257
255
  end
256
+ trigger :shove_it
257
+
258
+ # or in Ruby 2.x
259
+ trigger def shove_it
260
+ employee.quit
261
+ end
258
262
 
259
263
  role :employee do
260
264
  def quit
@@ -269,7 +273,7 @@ end
269
273
 
270
274
  This will allow you to write methods like you normally would. They are aliased internally with a prefix and the method name that you use is rewritten to add and remove the context for the objects in this context. The public API of your class remains the same, but the extra feature of wrapping your method is handled for you.
271
275
 
272
- This will treat all instance methods defined on your context the same way, so be aware of that.
276
+ This works like Ruby's `public`,`protected`, and `private` keywords in that you can send symbols of method names to it. But `trigger` does not alter the parsing of the document like those core keywords do. In other words, you can't merely type `trigger` on one line, and have methods added afterward be treated as trigger methods.
273
277
 
274
278
  ## Where roles exist
275
279
 
@@ -367,7 +371,7 @@ of your context.
367
371
  Here's how it works:
368
372
 
369
373
  ```ruby
370
- class ActiviatingAccount
374
+ class ActivatingAccount
371
375
  extend Surrounded::Context
372
376
 
373
377
  apply_roles_on(:trigger) # this is the default
@@ -423,12 +427,17 @@ class ActiviatingAccount
423
427
  apply_roles_on(:trigger) # this is the default
424
428
  # apply_roles_on(:initialize) # set this to apply behavior from the start
425
429
 
426
- set_methods_as_triggers # allows you to skip the 'trigger' dsl
427
-
428
430
  # set the default role type only for this class
429
431
  self.default_role_type = :module # also :wrap, :wrapper, or :interface
430
432
 
433
+ # shortcut initialization code
431
434
  initialize(:activator, :account)
435
+ # or handle it yourself
436
+ def initialize(activator, account)
437
+ # this must be done to handle the mapping of roles to objects
438
+ # pass an array of arrays with role name symbol and the object for that role
439
+ map_roles([[:activator, activator],[:account, account]])
440
+ end
432
441
 
433
442
  role :activator do # module by default
434
443
  def some_behavior; end
@@ -463,6 +472,15 @@ class ActiviatingAccount
463
472
  trigger :some_trigger_method do
464
473
  activator.some_behavior # behavior always available
465
474
  end
475
+
476
+ trigger def some_other_trigger
477
+ activator.some_behavior # behavior always available
478
+ end
479
+
480
+ def regular_non_trigger
481
+ activator.some_behavior # behavior always available with the following line
482
+ end
483
+ trigger :regular_non_trigger # turns the method into a trigger
466
484
  end
467
485
  ```
468
486
 
data/examples/rails.rb CHANGED
@@ -2,9 +2,13 @@
2
2
 
3
3
  # First, be guarded against changes in third-party libraries
4
4
  module Awareness
5
- include Surrounded
6
- include Casting::Client
7
- delegate_missing_methods
5
+ def self.included(base)
6
+ base.class_eval {
7
+ include Surrounded
8
+ include Casting::Client
9
+ delegate_missing_methods
10
+ }
11
+ end
8
12
  end
9
13
 
10
14
  class User
@@ -18,7 +22,7 @@ end
18
22
  class SomeUseCase
19
23
  extend Surrounded::Context
20
24
 
21
- setup(:admin, :other_user, :listener)
25
+ initialize(:admin, :other_user, :listener)
22
26
 
23
27
  trigger :do_something do
24
28
  admin.something
@@ -18,7 +18,6 @@ module Surrounded
18
18
  def self.extended(base)
19
19
  base.class_eval {
20
20
  @triggers = Set.new
21
- @methods_as_triggers = Surrounded::Context.methods_as_triggers
22
21
  include InstanceMethods
23
22
  }
24
23
  base.singleton_class.send(:alias_method, :setup, :initialize)
@@ -29,18 +28,7 @@ module Surrounded
29
28
  end
30
29
 
31
30
  class << self
32
- attr_writer :default_role_type, :methods_as_triggers
33
- end
34
-
35
- attr_reader :methods_as_triggers
36
-
37
- def self.methods_as_triggers
38
- return @methods_as_triggers if defined?(@methods_as_triggers)
39
- @methods_as_triggers = false
40
- end
41
-
42
- def set_methods_as_triggers
43
- @methods_as_triggers = true
31
+ attr_writer :default_role_type
44
32
  end
45
33
 
46
34
  def new(*args, &block)
@@ -105,6 +93,7 @@ module Surrounded
105
93
  rescue NameError => e
106
94
  raise e.extend(InvalidRoleType)
107
95
  end
96
+ alias_method :role_methods, :role
108
97
 
109
98
  def apply_roles_on(which)
110
99
  @__apply_role_policy = which
@@ -132,18 +121,31 @@ module Surrounded
132
121
  private(*method_names)
133
122
  end
134
123
 
135
- def trigger(name, *args, &block)
136
- store_trigger(name)
124
+ def trigger(*names, &block)
125
+ if block.nil?
126
+ names.each do |name|
127
+ unless triggers.include?(name) || name.nil?
128
+ alias_method :"__trigger_#{name}", :"#{name}"
129
+ private :"__trigger_#{name}"
130
+ remove_method :"#{name}"
131
+ redo_method(name)
132
+ store_trigger(name)
133
+ end
134
+ end
135
+ else
136
+ name = names.first
137
137
 
138
- define_method(:"__trigger_#{name}", *args, &block)
138
+ define_method(:"__trigger_#{name}", &block)
139
139
 
140
- private :"__trigger_#{name}"
140
+ private :"__trigger_#{name}"
141
+ store_trigger(name)
141
142
 
142
- redo_method(name, args)
143
+ redo_method(name)
144
+ end
143
145
  end
144
146
 
145
- def store_trigger(name)
146
- @triggers << name
147
+ def store_trigger(*names)
148
+ @triggers.merge(names)
147
149
  end
148
150
 
149
151
  def role_const(name)
@@ -152,13 +154,13 @@ module Surrounded
152
154
  end
153
155
  end
154
156
 
155
- def redo_method(name, args)
157
+ def redo_method(name)
156
158
  class_eval %{
157
- def #{name}(#{args.join(', ')})
159
+ def #{name}
158
160
  begin
159
161
  apply_roles if __apply_role_policy == :trigger
160
162
 
161
- self.send("__trigger_#{name}", #{args.join(', ')})
163
+ self.send("__trigger_#{name}")
162
164
 
163
165
  ensure
164
166
  remove_roles if __apply_role_policy == :trigger
@@ -167,20 +169,6 @@ module Surrounded
167
169
  }
168
170
  end
169
171
 
170
- def method_added(name)
171
- if methods_as_triggers
172
- unless name.to_s.match(/^__trigger|initialize/) || (@triggers && triggers.include?(name))
173
- store_trigger(name)
174
- args = self.instance_method(name).parameters.map{|p| p.last }
175
- alias_method :"__trigger_#{name}", :"#{name}"
176
- private :"__trigger_#{name}"
177
- remove_method :"#{name}"
178
- redo_method(name, args)
179
- end
180
- end
181
- super
182
- end
183
-
184
172
  module InstanceMethods
185
173
  def role?(name, &block)
186
174
  return false unless role_map.role?(name)
@@ -1,3 +1,3 @@
1
1
  module Surrounded
2
- VERSION = "0.5.1"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -4,7 +4,7 @@ describe Surrounded::Context, '.role' do
4
4
  class RoleContextTester
5
5
  extend Surrounded::Context
6
6
 
7
- role :admin do
7
+ role_methods :admin do
8
8
  end
9
9
  end
10
10
 
@@ -90,21 +90,21 @@ end
90
90
 
91
91
  class RoleAssignmentContext
92
92
  extend Surrounded::Context
93
- set_methods_as_triggers
94
93
 
95
94
  initialize(:user, :other_user)
96
95
 
97
- trigger :user_ancestors do
96
+ def user_ancestors
98
97
  user.singleton_class.ancestors
99
98
  end
100
99
 
101
- trigger :other_user_ancestors do
100
+ def other_user_ancestors
102
101
  other_user.singleton_class.ancestors
103
102
  end
104
103
 
105
- trigger :check_user_response do
104
+ trigger def check_user_response
106
105
  user.respond_to?(:a_method!)
107
106
  end
107
+ trigger :check_user_response # should not raise error
108
108
 
109
109
  trigger :check_other_user_response do
110
110
  user.respond_to?(:a_method!)
@@ -113,6 +113,8 @@ class RoleAssignmentContext
113
113
  def regular_method_trigger
114
114
  user.respond_to?(:a_method!)
115
115
  end
116
+
117
+ trigger :user_ancestors, :other_user_ancestors, :regular_method_trigger
116
118
 
117
119
  module User
118
120
  def a_method!; end
@@ -175,4 +177,8 @@ describe Surrounded::Context, 'assigning roles' do
175
177
  it 'allows usage of regular methods for triggers' do
176
178
  assert context.regular_method_trigger
177
179
  end
180
+
181
+ it 'ignores nil trigger names' do
182
+ assert context.class.send(:trigger)
183
+ end
178
184
  end
metadata CHANGED
@@ -1,62 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: surrounded
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
5
- prerelease:
4
+ version: 0.6.0
6
5
  platform: ruby
7
6
  authors:
8
- - '''Jim Gay'''
7
+ - "'Jim Gay'"
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-11-06 00:00:00.000000000 Z
11
+ date: 2013-12-16 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: triad
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
19
  version: 0.1.2
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: 0.1.2
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: bundler
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ~>
31
+ - - "~>"
36
32
  - !ruby/object:Gem::Version
37
33
  version: '1.3'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ~>
38
+ - - "~>"
44
39
  - !ruby/object:Gem::Version
45
40
  version: '1.3'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rake
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - '>='
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - '>='
52
+ - - ">="
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  description: Gives an object implicit access to other objects in it's environment.
@@ -66,9 +59,9 @@ executables: []
66
59
  extensions: []
67
60
  extra_rdoc_files: []
68
61
  files:
69
- - .gitignore
70
- - .simplecov
71
- - .travis.yml
62
+ - ".gitignore"
63
+ - ".simplecov"
64
+ - ".travis.yml"
72
65
  - Gemfile
73
66
  - LICENSE.txt
74
67
  - README.md
@@ -91,33 +84,26 @@ files:
91
84
  homepage: http://github.com/saturnflyer/surrounded
92
85
  licenses:
93
86
  - MIT
87
+ metadata: {}
94
88
  post_install_message:
95
89
  rdoc_options: []
96
90
  require_paths:
97
91
  - lib
98
92
  required_ruby_version: !ruby/object:Gem::Requirement
99
- none: false
100
93
  requirements:
101
- - - '>='
94
+ - - ">="
102
95
  - !ruby/object:Gem::Version
103
96
  version: '0'
104
- segments:
105
- - 0
106
- hash: 2352251997784321563
107
97
  required_rubygems_version: !ruby/object:Gem::Requirement
108
- none: false
109
98
  requirements:
110
- - - '>='
99
+ - - ">="
111
100
  - !ruby/object:Gem::Version
112
101
  version: '0'
113
- segments:
114
- - 0
115
- hash: 2352251997784321563
116
102
  requirements: []
117
103
  rubyforge_project:
118
- rubygems_version: 1.8.26
104
+ rubygems_version: 2.2.0
119
105
  signing_key:
120
- specification_version: 3
106
+ specification_version: 4
121
107
  summary: Create encapsulated environments for your objects.
122
108
  test_files:
123
109
  - test/example_proxy_test.rb