surrounded 0.3.1 → 0.4.0

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: 06fdae60ee9aa6572db5ba8151e49bfba7257501
4
- data.tar.gz: dbed795a64a2ed83a405725e4a0bbf826952cc32
3
+ metadata.gz: 36e7bbdfc696cc211fbf80ab3f1c63f60b9d36ee
4
+ data.tar.gz: a43d73f2915efb6a66dd4cb027a5f9bab34c058f
5
5
  SHA512:
6
- metadata.gz: b930aaf822c3a9b77c8fb9390e8dd32d64077b3ede2f6cd8f347f429b07ad8b60c899585cf1453a0c7359d3eab48af0b73c64f19a33a10213d5ce6cb2b27fcfe
7
- data.tar.gz: 00d58b00643d05d17f4ece1f7b1092104f4747535fe67368441fcfe57f0a52d2ebc31a4fba39569601d7d99d37a321cedc8f7be3fe713d88ee7d687d71b0d5ff
6
+ metadata.gz: a3b5072dac15714ce52bfa92d6bff0ccf3756f80ec9cf3ca9c722e50220f27658f58ede2c89f36f2fa29e2e0c0a0524a13021e301c68064a13b50742590de4b6
7
+ data.tar.gz: 1e52f4a952f68aa1ca5773647115752237ea362c485859ab80964f1e5ea44f9a0bd4737072203fd346fc22b8607874fbb4852ec25d4619f677ef9b4cbc4dbe18
@@ -1,6 +1,17 @@
1
1
  require 'set'
2
2
  require 'surrounded/context/role_map'
3
- require 'redcard'
3
+
4
+ # Some features are only available in versions of Ruby
5
+ # where this method is true
6
+ def module_method_rebinding?
7
+ return @__module_method_rebinding__ if defined?(@__module_method_rebinding__)
8
+ sample_method = Enumerable.instance_method(:to_a)
9
+ @__module_method_rebinding__ = begin
10
+ !!sample_method.bind(Object.new)
11
+ rescue TypeError
12
+ false
13
+ end
14
+ end
4
15
 
5
16
  module Surrounded
6
17
  module Context
@@ -17,9 +28,11 @@ module Surrounded
17
28
  @default_role_type = type
18
29
  end
19
30
 
20
- def new(*)
21
- instance = super
22
- instance.instance_variable_set('@__apply_role_policy', __apply_role_policy)
31
+ def new(*args, &block)
32
+ instance = allocate
33
+ instance.send(:preinitialize)
34
+ instance.send(:initialize, *args, &block)
35
+ instance.send(:postinitialize)
23
36
  instance
24
37
  end
25
38
 
@@ -63,7 +76,7 @@ module Surrounded
63
76
  klass.send(:include, Surrounded)
64
77
  end
65
78
 
66
- if RedCard.check '2.0'
79
+ if module_method_rebinding?
67
80
  def interface(name, &block)
68
81
  class_basename = name.to_s.gsub(/(?:^|_)([a-z])/){ $1.upcase }
69
82
  interface_name = class_basename + 'Interface'
@@ -90,10 +103,10 @@ module Surrounded
90
103
 
91
104
  class_eval "
92
105
  def initialize(#{setup_args.join(',')})
93
- @__apply_role_policy = :#{__apply_role_policy}
106
+ preinitialize
94
107
  arguments = method(__method__).parameters.map{|arg| eval(arg[1].to_s) }
95
108
  map_roles(#{setup_args}.zip(arguments))
96
- apply_roles if __apply_role_policy == :initialize
109
+ postinitialize
97
110
  end
98
111
  "
99
112
  end
@@ -146,6 +159,14 @@ module Surrounded
146
159
 
147
160
  private
148
161
 
162
+ def preinitialize
163
+ @__apply_role_policy = self.class.send(:__apply_role_policy)
164
+ end
165
+
166
+ def postinitialize
167
+ apply_roles if __apply_role_policy == :initialize
168
+ end
169
+
149
170
  def role_map
150
171
  @role_map ||= RoleMap.new
151
172
  end
@@ -1,3 +1,3 @@
1
1
  module Surrounded
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
3
3
  end
data/surrounded.gemspec CHANGED
@@ -19,7 +19,6 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency "triad", "~> 0.1.2"
22
- spec.add_dependency "redcard","~> 1.1.0"
23
22
 
24
23
  spec.add_development_dependency "bundler", "~> 1.3"
25
24
  spec.add_development_dependency "rake"
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- if RedCard.check '2.0'
3
+ if test_rebinding_methods?
4
4
 
5
5
  # If you want to use wrappers, here's how you could
6
6
  class ProxyContext
@@ -52,4 +52,4 @@ describe ProxyContext do
52
52
  end
53
53
  end
54
54
 
55
- end # RedCard.check '2.0'
55
+ end
@@ -1,6 +1,5 @@
1
1
  require 'test_helper'
2
2
 
3
-
4
3
  describe Surrounded::Context, '.role' do
5
4
  class RoleContextTester
6
5
  extend Surrounded::Context
@@ -20,7 +19,7 @@ describe Surrounded::Context, '.role' do
20
19
  error = assert_raises(NameError){
21
20
  RoleContextTester::Admin
22
21
  }
23
- assert_match /private constant/, error.message
22
+ assert_match(/private constant/, error.message)
24
23
  end
25
24
  end
26
25
 
@@ -42,10 +41,10 @@ describe Surrounded::Context, '.role' do
42
41
  error = assert_raises(NameError){
43
42
  WrapperRoleContext::Admin
44
43
  }
45
- assert_match /private constant/, error.message
44
+ assert_match(/private constant/, error.message)
46
45
  end
47
46
  end
48
- if RedCard.check '2.0'
47
+ if test_rebinding_methods?
49
48
  class InterfaceContext
50
49
  extend Surrounded::Context
51
50
 
@@ -80,7 +79,7 @@ describe Surrounded::Context, '.role' do
80
79
  error = assert_raises(NameError){
81
80
  InterfaceContext::AdminInterface
82
81
  }
83
- assert_match /private constant/, error.message
82
+ assert_match(/private constant/, error.message)
84
83
  end
85
84
 
86
85
  it 'creates a private accessor method' do
data/test/test_helper.rb CHANGED
@@ -25,4 +25,17 @@ class TestContext
25
25
  trigger :access_other_object do
26
26
  user.other_user.name
27
27
  end
28
+ end
29
+
30
+ # This is a different implementation of module_method_rebinding?
31
+ # created in order to check that the behavior of the code is correct.
32
+ #
33
+ # This method is used in tests and module_method_rebinding? is used
34
+ # in the library code.
35
+ def test_rebinding_methods?
36
+ unbound = Enumerable.instance_method(:count)
37
+ unbound.bind(Object.new)
38
+ true
39
+ rescue TypeError
40
+ false
28
41
  end
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.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - '''Jim Gay'''
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-17 00:00:00.000000000 Z
11
+ date: 2013-08-20 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.1.2
27
- - !ruby/object:Gem::Dependency
28
- name: redcard
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ~>
32
- - !ruby/object:Gem::Version
33
- version: 1.1.0
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ~>
39
- - !ruby/object:Gem::Version
40
- version: 1.1.0
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: bundler
43
29
  requirement: !ruby/object:Gem::Requirement