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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3ebcadc33e8f51d132862e8b648a6d4a2bd15cc0
4
- data.tar.gz: 1626655dd9057674364a2e3ba8ced482e89b156e
3
+ metadata.gz: 38ccce9051567f5c868f6bf3886a1ed6a7f88886
4
+ data.tar.gz: 55256f9f23f0e5369426b6a5b8abc5ab8e08f3ae
5
5
  SHA512:
6
- metadata.gz: 514db8e6ef0df0df0e9ffba94df4ea3217c6fd8eb37d4bc187913599695b4bfdfd5610062ffb8a31f700d884eef4ef91890204052906f60641ff164e46f19046
7
- data.tar.gz: a982f5d589c10ca7984273f4ced59520252a8425b5266b35beca3e1fdd5e93823ec80ce65f2397fb460514625c2c9c6a1883e95d106c6e41906cfe28f6e4520b
6
+ metadata.gz: ff8264560362b3b41d266abc4498937ca5521c725533febd0d28916af5349235faeb45cb84df0775ce0ee4ae32dbf83e3d9c4e0b8e0bd2d8021e7690e5747898
7
+ data.tar.gz: 6a96f9ddf345233bcbac765eb28eceec2da8d60756d7adaece9093b111abca093af8915b90dc9b071a6ff922b4531efc933f4487be379f5fd8613ed9c63a6cc7
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 'Jim Gay'
1
+ Copyright (c) 2013-2015 'Jim Gay'
2
2
 
3
3
  MIT License
4
4
 
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 fine the actual method definitions created in a module and then find that module included in your class.
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
- private_attr_reader(*setup_args)
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 keyword_initialize(*setup_args)
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
- mod.class_eval %{
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
@@ -118,7 +118,7 @@ module Surrounded
118
118
 
119
119
  def rebind(options_hash)
120
120
  clear_instance_variables
121
- initialize(options_hash)
121
+ initialize(**options_hash)
122
122
  rescue ArgumentError
123
123
  initialize(*options_hash.values)
124
124
  self
@@ -1,5 +1,5 @@
1
1
  module Surrounded
2
- VERSION = "0.9.6"
2
+ VERSION = "0.9.7"
3
3
 
4
4
  def self.version
5
5
  VERSION
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.0"
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.6
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-23 00:00:00.000000000 Z
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.0
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.0
26
+ version: 0.2.2
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement