surrounded 0.9.6 → 0.9.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +11 -1
- data/lib/surrounded/context/initializing.rb +24 -20
- data/lib/surrounded/context.rb +1 -1
- data/lib/surrounded/version.rb +1 -1
- data/surrounded.gemspec +1 -1
- data/test/surrounded_context_test.rb +14 -2
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 38ccce9051567f5c868f6bf3886a1ed6a7f88886
|
4
|
+
data.tar.gz: 55256f9f23f0e5369426b6a5b8abc5ab8e08f3ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff8264560362b3b41d266abc4498937ca5521c725533febd0d28916af5349235faeb45cb84df0775ce0ee4ae32dbf83e3d9c4e0b8e0bd2d8021e7690e5747898
|
7
|
+
data.tar.gz: 6a96f9ddf345233bcbac765eb28eceec2da8d60756d7adaece9093b111abca093af8915b90dc9b071a6ff922b4531efc933f4487be379f5fd8613ed9c63a6cc7
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -71,6 +71,16 @@ context = Employment.new(employee: user1, boss: user2)
|
|
71
71
|
|
72
72
|
This will allow you to prepare your accessing code to use keywords.
|
73
73
|
|
74
|
+
If you need to override the initializer with additional work, you have the ability to use a block to be evaluated in the context of the initialized object.
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
initialize :role1, :role3 do
|
78
|
+
map_role(:role3, 'SomeRoleConstantName', initialize_the_object_to_play)
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
This block will be called _after_ the default initialization is done.
|
83
|
+
|
74
84
|
## Defining behaviors for roles
|
75
85
|
|
76
86
|
Behaviors for your roles are easily defined just like you define a method. Provide your role a block and define methods there.
|
@@ -866,7 +876,7 @@ You can remember the method name by the convention that `remove` or `apply` desc
|
|
866
876
|
|
867
877
|
If you use this library, it's important to understand it.
|
868
878
|
|
869
|
-
As much as possible, when you use the Surrounded DSL for creating triggers, roles, initialize methods, and others you'll likely
|
879
|
+
As much as possible, when you use the Surrounded DSL for creating triggers, roles, initialize methods, and others you'll likely find the actual method definitions created in a module and then find that module included in your class.
|
870
880
|
|
871
881
|
This is a design choice which allows you to override any standard behavior more easily.
|
872
882
|
|
@@ -3,42 +3,46 @@ module Surrounded
|
|
3
3
|
module Initializing
|
4
4
|
# Shorthand for creating an instance level initialize method which
|
5
5
|
# handles the mapping of the given arguments to their named role.
|
6
|
-
def initialize_without_keywords(*setup_args)
|
7
|
-
|
8
|
-
|
9
|
-
mod = Module.new
|
10
|
-
line = __LINE__
|
11
|
-
mod.class_eval "
|
12
|
-
def initialize(#{setup_args.join(',')})
|
13
|
-
map_roles(#{setup_args.to_s}.zip([#{setup_args.join(',')}]))
|
14
|
-
end
|
15
|
-
", __FILE__, line
|
16
|
-
const_set("ContextInitializer", mod)
|
17
|
-
include mod
|
6
|
+
def initialize_without_keywords(*setup_args, &block)
|
7
|
+
parameters = setup_args.join(',')
|
8
|
+
default_initializer(parameters, setup_args, &block)
|
18
9
|
end
|
19
|
-
def initialize(*setup_args)
|
10
|
+
def initialize(*setup_args, &block)
|
20
11
|
warn "Deprecated: The behavior of 'initialize' will require keywords in the future
|
21
12
|
Consider using keyword arguments or switching to 'initialize_without_keywords'\n\n"
|
22
|
-
initialize_without_keywords(*setup_args)
|
13
|
+
initialize_without_keywords(*setup_args, &block)
|
14
|
+
end
|
15
|
+
|
16
|
+
def keyword_initialize(*setup_args, &block)
|
17
|
+
parameters = setup_args.map{|a| "#{a}:"}.join(',')
|
18
|
+
default_initializer(parameters, setup_args, &block)
|
19
|
+
end
|
20
|
+
alias initialize_with_keywords keyword_initialize
|
21
|
+
|
22
|
+
def initializer_block
|
23
|
+
@initializer_block
|
23
24
|
end
|
24
25
|
|
25
|
-
def
|
26
|
+
def apply_initializer_block(instance)
|
27
|
+
instance.instance_eval(&initializer_block) if initializer_block
|
28
|
+
end
|
29
|
+
|
30
|
+
def default_initializer(params, setup_args, &block)
|
26
31
|
private_attr_reader(*setup_args)
|
27
32
|
|
28
33
|
parameters = setup_args.map{|a| "#{a}:"}.join(',')
|
29
|
-
|
34
|
+
@initializer_block = block || nil
|
30
35
|
mod = Module.new
|
31
|
-
line = __LINE__
|
32
|
-
|
33
|
-
def initialize(#{parameters})
|
36
|
+
line = __LINE__; mod.class_eval %{
|
37
|
+
def initialize(#{params})
|
34
38
|
@role_map = role_mapper_class.new
|
35
39
|
map_roles(#{setup_args.to_s}.zip([#{setup_args.join(',')}]))
|
40
|
+
self.class.apply_initializer_block(self)
|
36
41
|
end
|
37
42
|
}, __FILE__, line
|
38
43
|
const_set("ContextInitializer", mod)
|
39
44
|
include mod
|
40
45
|
end
|
41
|
-
alias initialize_with_keywords keyword_initialize
|
42
46
|
end
|
43
47
|
end
|
44
48
|
end
|
data/lib/surrounded/context.rb
CHANGED
data/lib/surrounded/version.rb
CHANGED
data/surrounded.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency "triad", "~> 0.2.
|
21
|
+
spec.add_dependency "triad", "~> 0.2.2"
|
22
22
|
|
23
23
|
spec.add_development_dependency "bundler", "~> 1.3"
|
24
24
|
spec.add_development_dependency "rake"
|
@@ -106,7 +106,9 @@ end
|
|
106
106
|
class RoleAssignmentContext
|
107
107
|
extend Surrounded::Context
|
108
108
|
|
109
|
-
initialize(:user, :other_user)
|
109
|
+
initialize(:user, :other_user) do
|
110
|
+
self.instance_variable_set(:@defined_by_initializer_block, 'yup')
|
111
|
+
end
|
110
112
|
|
111
113
|
def user_ancestors
|
112
114
|
user.singleton_class.ancestors
|
@@ -207,6 +209,10 @@ describe Surrounded::Context, 'assigning roles' do
|
|
207
209
|
context = IgnoreExternalConstantsContext.new(user, special, other)
|
208
210
|
assert_equal User, context.check_special
|
209
211
|
end
|
212
|
+
|
213
|
+
it 'applies a provided block to the instance' do
|
214
|
+
assert_equal 'yup', context.instance_variable_get(:@defined_by_initializer_block)
|
215
|
+
end
|
210
216
|
end
|
211
217
|
|
212
218
|
class CollectionContext
|
@@ -284,7 +290,9 @@ begin
|
|
284
290
|
class Keyworder
|
285
291
|
extend Surrounded::Context
|
286
292
|
|
287
|
-
keyword_initialize :this, :that
|
293
|
+
keyword_initialize :this, :that do
|
294
|
+
self.instance_variable_set(:@defined_by_initializer_block, 'yes')
|
295
|
+
end
|
288
296
|
end
|
289
297
|
|
290
298
|
describe Surrounded::Context, 'keyword initializers' do
|
@@ -298,6 +306,10 @@ begin
|
|
298
306
|
}
|
299
307
|
assert_match(/missing keyword: that/, err.message)
|
300
308
|
end
|
309
|
+
|
310
|
+
it 'evaluates a given block' do
|
311
|
+
assert_equal 'yes', Keyworder.new(this: User.new('Jim'), that: User.new('Guille')).instance_variable_get(:@defined_by_initializer_block)
|
312
|
+
end
|
301
313
|
end
|
302
314
|
rescue SyntaxError
|
303
315
|
STDOUT.puts "No support for keywords"
|
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.
|
4
|
+
version: 0.9.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "'Jim Gay'"
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05-
|
11
|
+
date: 2015-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: triad
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.2.
|
19
|
+
version: 0.2.2
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.2.
|
26
|
+
version: 0.2.2
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|