surrounded 0.7.2 → 0.7.3

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: 349c77c09cbb18c43dff15df5fcb3ce444f6aee1
4
- data.tar.gz: 61b1e9fb3ce7e7865a624425fbb4fc746644d674
3
+ metadata.gz: c237237d5cefc3b8d15c8c93e1b1835f53b16aa3
4
+ data.tar.gz: ffc1902273df1fad262c481b65a804e2eb1b8753
5
5
  SHA512:
6
- metadata.gz: 166ecba24f2037b1a57c80a163f1a2a75506af524d88552af1caa3d6d72ea79cd0693a97df446acbfd7ac903e57b4d8a8c524b21d5f087dbf21fd0ad00c5c4ab
7
- data.tar.gz: 04b781c0db8fc068de9af65f4fda3f58b3b8d1e4f859c6dfa67800e89be032cc696a5690b6c7adf7661663dea61dd2eea6dbabb74ad915517e04be0faa9d86a1
6
+ metadata.gz: 5faa7eae255b50a89451a223d92b0e2988a187c8e5925b6a999d615ac07f1a61ff0b0f6d2ba574e46815e16b291b26121f97adaad01417a018c7f766b0c83d8b
7
+ data.tar.gz: deb7040f3cb066916086750e32dfe62584759110eba328a242011b5155aba0b070ce7c6533b24729caeb89840950b203b27d51a9f5e3e80c4caf569d5b8cd192
data/README.md CHANGED
@@ -538,6 +538,17 @@ class ActiviatingAccount
538
538
  def disallow_some_trigger_method?
539
539
  # whatever conditional code for the instance of the context
540
540
  end
541
+
542
+ # Create shortcuts for triggers as class methods
543
+ # so you can do ActiviatingAccount.some_trigger_method(activator, account)
544
+ # This will make all triggers shortcuts.
545
+ shortcut_triggers
546
+ # Alterantively, you could implement shortcuts individually:
547
+ def self.some_trigger_method(activator, account)
548
+ instance = self.new(activator.account)
549
+ instance.some_trigger_method
550
+ end
551
+
541
552
  end
542
553
  ```
543
554
 
@@ -30,12 +30,11 @@ module Surrounded
30
30
  remove_roles if __apply_role_policy == :trigger
31
31
  end
32
32
  end
33
- }
33
+ }, __FILE__, __LINE__
34
34
  end
35
35
 
36
36
  def define_access_method(name, &block)
37
37
  class_eval {
38
- meth = AccessMethods.instance_method(:with_roles)
39
38
  define_method "disallow_#{name}?" do
40
39
  begin
41
40
  apply_roles if __apply_role_policy == :trigger
@@ -58,15 +57,6 @@ module Surrounded
58
57
  !self.respond_to?(method_restrictor, true) || !self.send(method_restrictor)
59
58
  }.to_set
60
59
  end
61
-
62
- def with_roles(policy = :trigger)
63
- begin
64
- apply_roles if __apply_role_policy == policy
65
- yield
66
- ensure
67
- remove_roles if __apply_role_policy == policy
68
- end
69
- end
70
60
  end
71
61
  end
72
62
  end
@@ -1,6 +1,7 @@
1
1
  require 'set'
2
2
  require 'surrounded/context/role_map'
3
3
  require 'surrounded/access_control'
4
+ require 'surrounded/shortcuts'
4
5
 
5
6
  # Some features are only available in versions of Ruby
6
7
  # where this method is true
@@ -21,19 +22,6 @@ module Surrounded
21
22
  @triggers = Set.new
22
23
  include InstanceMethods
23
24
  }
24
- base.singleton_class.send(:alias_method, :setup, :initialize)
25
- end
26
-
27
- def self.default_role_type
28
- @default_role_type ||= :module
29
- end
30
-
31
- class << self
32
- attr_writer :default_role_type
33
- end
34
-
35
- def protect_triggers
36
- self.extend(::Surrounded::AccessControl)
37
25
  end
38
26
 
39
27
  def new(*args, &block)
@@ -50,6 +38,18 @@ module Surrounded
50
38
 
51
39
  private
52
40
 
41
+ def self.default_role_type
42
+ @default_role_type ||= :module
43
+ end
44
+
45
+ class << self
46
+ attr_writer :default_role_type
47
+ end
48
+
49
+ # Additional Features
50
+ def protect_triggers; self.extend(::Surrounded::AccessControl); end
51
+ def shortcut_triggers; self.extend(::Surrounded::Shortcuts); end
52
+
53
53
  def private_const_set(name, const)
54
54
  const = const_set(name, const)
55
55
  private_constant name.to_sym
@@ -115,10 +115,11 @@ module Surrounded
115
115
  def initialize(#{setup_args.join(',')})
116
116
  preinitialize
117
117
  arguments = method(__method__).parameters.map{|arg| eval(arg[1].to_s) }
118
+ @role_map = RoleMap.new
118
119
  map_roles(#{setup_args}.zip(arguments))
119
120
  postinitialize
120
121
  end
121
- "
122
+ ", __FILE__, __LINE__
122
123
  end
123
124
 
124
125
  def private_attr_reader(*method_names)
@@ -129,13 +130,7 @@ module Surrounded
129
130
  def trigger(*names, &block)
130
131
  if block.nil?
131
132
  names.each do |name|
132
- unless triggers.include?(name) || name.nil?
133
- alias_method :"__trigger_#{name}", :"#{name}"
134
- private :"__trigger_#{name}"
135
- remove_method :"#{name}"
136
- redo_method(name)
137
- store_trigger(name)
138
- end
133
+ define_trigger_method(name, &block)
139
134
  end
140
135
  else
141
136
  name = names.first
@@ -158,6 +153,16 @@ module Surrounded
158
153
  const_get(name)
159
154
  end
160
155
  end
156
+
157
+ def define_trigger_method(name, &block)
158
+ unless triggers.include?(name) || name.nil?
159
+ alias_method :"__trigger_#{name}", :"#{name}"
160
+ private :"__trigger_#{name}"
161
+ remove_method :"#{name}"
162
+ redo_method(name)
163
+ store_trigger(name)
164
+ end
165
+ end
161
166
 
162
167
  def redo_method(name)
163
168
  class_eval %{
@@ -171,7 +176,7 @@ module Surrounded
171
176
  remove_roles if __apply_role_policy == :trigger
172
177
  end
173
178
  end
174
- }
179
+ }, __FILE__, __LINE__
175
180
  end
176
181
 
177
182
  module InstanceMethods
@@ -0,0 +1,19 @@
1
+ module Surrounded
2
+ module Shortcuts
3
+ private
4
+
5
+ def define_shortcut(name)
6
+ singleton_class.send(:define_method, name) do |*args|
7
+ instance = self.new(*args)
8
+ instance.public_send(name)
9
+ end
10
+ end
11
+
12
+ def store_trigger(*names)
13
+ names.each do |name|
14
+ define_shortcut(name)
15
+ end
16
+ @triggers.merge(names)
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module Surrounded
2
- VERSION = "0.7.2"
2
+ VERSION = "0.7.3"
3
3
  end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ class ShortcutContext
4
+ extend Surrounded::Context
5
+ shortcut_triggers
6
+
7
+ initialize :user, :other
8
+
9
+ trigger :shorty do
10
+ user.speak
11
+ end
12
+
13
+ role :user do
14
+ def speak
15
+ 'it works, shorty!'
16
+ end
17
+ end
18
+ end
19
+
20
+ describe Surrounded::Context, 'shortcuts' do
21
+ let(:user){ User.new("Jim") }
22
+ let(:other){ User.new("Guille") }
23
+ it 'creates shortcut class methods for triggers' do
24
+ assert_equal 'it works, shorty!', ShortcutContext.shorty(user, other)
25
+ end
26
+ end
@@ -124,7 +124,7 @@ class RoleAssignmentContext
124
124
  end
125
125
  end
126
126
 
127
- describe Surrounded::Context, '.setup' do
127
+ describe Surrounded::Context, '.initialize' do
128
128
  it 'defines an initialize method accepting the same arguments' do
129
129
  assert_equal 2, RoleAssignmentContext.instance_method(:initialize).arity
130
130
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: surrounded
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - '''Jim Gay'''
@@ -73,9 +73,11 @@ files:
73
73
  - lib/surrounded/context/negotiator.rb
74
74
  - lib/surrounded/context/role_map.rb
75
75
  - lib/surrounded/context_errors.rb
76
+ - lib/surrounded/shortcuts.rb
76
77
  - lib/surrounded/version.rb
77
78
  - surrounded.gemspec
78
79
  - test/context_access_test.rb
80
+ - test/context_shortcuts_test.rb
79
81
  - test/example_proxy_test.rb
80
82
  - test/example_threaded_test.rb
81
83
  - test/example_wrapper_test.rb
@@ -109,6 +111,7 @@ specification_version: 4
109
111
  summary: Create encapsulated environments for your objects.
110
112
  test_files:
111
113
  - test/context_access_test.rb
114
+ - test/context_shortcuts_test.rb
112
115
  - test/example_proxy_test.rb
113
116
  - test/example_threaded_test.rb
114
117
  - test/example_wrapper_test.rb