role_model 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +2 -2
- data/VERSION +1 -1
- data/lib/role_model/roles.rb +5 -2
- data/lib/role_model.rb +1 -0
- data/role_model.gemspec +4 -2
- data/spec/custom_matchers.rb +24 -0
- data/spec/role_model_spec.rb +26 -20
- data/spec/roles_spec.rb +3 -5
- data/spec/spec_helper.rb +2 -1
- metadata +4 -2
data/README.rdoc
CHANGED
@@ -32,7 +32,7 @@ Assigned roles will be stored as a bitmask in an configurable attribute
|
|
32
32
|
=> #<User ...>
|
33
33
|
|
34
34
|
# role assignment
|
35
|
-
>> u.roles = [:admin]
|
35
|
+
>> u.roles = [:admin] # ['admin'] works as well
|
36
36
|
=> [:admin]
|
37
37
|
|
38
38
|
# adding roles
|
@@ -48,7 +48,7 @@ Assigned roles will be stored as a bitmask in an configurable attribute
|
|
48
48
|
# ... check for individual roles
|
49
49
|
>> u.has_role? :author
|
50
50
|
=> false
|
51
|
-
>> u.
|
51
|
+
>> u.is_a? :admin # has_role? is aliased as is_a?
|
52
52
|
=> true
|
53
53
|
|
54
54
|
# see the internal bitmask representation (3 = 0b0011)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.1
|
data/lib/role_model/roles.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
1
3
|
module RoleModel
|
2
|
-
class Roles <
|
4
|
+
class Roles < ::Set
|
3
5
|
attr_reader :model_instance
|
4
6
|
|
5
7
|
def initialize(sender, *roles)
|
@@ -8,7 +10,8 @@ module RoleModel
|
|
8
10
|
end
|
9
11
|
|
10
12
|
def <<(role)
|
11
|
-
model_instance.roles = super
|
13
|
+
model_instance.roles = super.to_a
|
14
|
+
self
|
12
15
|
end
|
13
16
|
end
|
14
17
|
end
|
data/lib/role_model.rb
CHANGED
data/role_model.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{role_model}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Martin Rehfeld"]
|
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
|
|
26
26
|
"lib/role_model.rb",
|
27
27
|
"lib/role_model/roles.rb",
|
28
28
|
"role_model.gemspec",
|
29
|
+
"spec/custom_matchers.rb",
|
29
30
|
"spec/role_model_spec.rb",
|
30
31
|
"spec/roles_spec.rb",
|
31
32
|
"spec/spec.opts",
|
@@ -37,7 +38,8 @@ Gem::Specification.new do |s|
|
|
37
38
|
s.rubygems_version = %q{1.3.6}
|
38
39
|
s.summary = %q{Declare, assign and query roles with ease}
|
39
40
|
s.test_files = [
|
40
|
-
"spec/
|
41
|
+
"spec/custom_matchers.rb",
|
42
|
+
"spec/role_model_spec.rb",
|
41
43
|
"spec/roles_spec.rb",
|
42
44
|
"spec/spec_helper.rb"
|
43
45
|
]
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module CustomMatchers
|
2
|
+
class ArrayIncludingMatcher
|
3
|
+
def initialize(*expected)
|
4
|
+
@expected = Array[*expected]
|
5
|
+
end
|
6
|
+
|
7
|
+
def ==(actual)
|
8
|
+
@expected.each do | value |
|
9
|
+
return false unless actual.include?(value)
|
10
|
+
end
|
11
|
+
true
|
12
|
+
rescue NoMethodError => ex
|
13
|
+
return false
|
14
|
+
end
|
15
|
+
|
16
|
+
def description
|
17
|
+
"array_including(#{@expected.inspect.sub(/^\[/,"").sub(/\]$/,"")})"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def array_including(*args)
|
22
|
+
ArrayIncludingMatcher.new(*args)
|
23
|
+
end
|
24
|
+
end
|
data/spec/role_model_spec.rb
CHANGED
@@ -148,6 +148,10 @@ describe RoleModel do
|
|
148
148
|
subject.roles.should include(:foo, :bar, :third)
|
149
149
|
subject.should have(3).roles
|
150
150
|
end
|
151
|
+
|
152
|
+
it "should not show a role twice in the return value" do
|
153
|
+
(subject.roles << :foo).should have(2).roles
|
154
|
+
end
|
151
155
|
end
|
152
156
|
|
153
157
|
context "without any previouly assigned roles" do
|
@@ -168,34 +172,36 @@ describe RoleModel do
|
|
168
172
|
subject.roles.should include(:foo, :bar)
|
169
173
|
subject.should have(2).roles
|
170
174
|
end
|
171
|
-
end
|
172
175
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
+
it "should silently ignore undefined roles" do
|
177
|
+
subject.roles << :baz
|
178
|
+
subject.roles.should be_empty
|
179
|
+
end
|
176
180
|
end
|
177
181
|
end
|
178
182
|
|
179
|
-
|
180
|
-
|
183
|
+
[:has_role?, :is_a?].each do |check_role_assignment_method|
|
184
|
+
describe "##{check_role_assignment_method}" do
|
185
|
+
subject { model_class.new }
|
181
186
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
187
|
+
it "should return true when the given role was assigned" do
|
188
|
+
subject.roles = :foo
|
189
|
+
subject.send(check_role_assignment_method, :foo).should be_true
|
190
|
+
end
|
186
191
|
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
192
|
+
it "should return false when the given role was not assigned" do
|
193
|
+
subject.roles = :bar
|
194
|
+
subject.send(check_role_assignment_method, :foo).should be_false
|
195
|
+
end
|
191
196
|
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
197
|
+
it "should return false when no role was assigned" do
|
198
|
+
subject.send(check_role_assignment_method, :foo).should be_false
|
199
|
+
subject.send(check_role_assignment_method, :bar).should be_false
|
200
|
+
end
|
196
201
|
|
197
|
-
|
198
|
-
|
202
|
+
it "should return false when asked for an undefined role" do
|
203
|
+
subject.send(check_role_assignment_method, :baz).should be_false
|
204
|
+
end
|
199
205
|
end
|
200
206
|
end
|
201
207
|
|
data/spec/roles_spec.rb
CHANGED
@@ -6,15 +6,13 @@ describe RoleModel::Roles do
|
|
6
6
|
let(:array) { [:foo, :bar] }
|
7
7
|
subject { RoleModel::Roles.new(model_instance, array) }
|
8
8
|
|
9
|
-
its(:model_instance) { should
|
9
|
+
its(:model_instance) { should equal(model_instance) }
|
10
10
|
it { should include(:foo, :bar) }
|
11
|
-
it { should
|
11
|
+
it { should respond_to(:each) }
|
12
12
|
|
13
13
|
describe "#<<" do
|
14
|
-
let(:roles_of_model_instance) { [:one, :two] }
|
15
|
-
|
16
14
|
it "should add the given element to the model_instance.roles by re-assigning all roles" do
|
17
|
-
model_instance.should_receive(:roles=).with(
|
15
|
+
model_instance.should_receive(:roles=).with(array_including(:foo, :bar, :baz))
|
18
16
|
subject << :baz
|
19
17
|
end
|
20
18
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -3,7 +3,8 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|
3
3
|
require 'role_model'
|
4
4
|
require 'spec'
|
5
5
|
require 'spec/autorun'
|
6
|
+
require File.dirname(__FILE__) + "/custom_matchers"
|
6
7
|
|
7
8
|
Spec::Runner.configure do |config|
|
8
|
-
|
9
|
+
config.include(CustomMatchers)
|
9
10
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 1
|
9
|
+
version: 0.3.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Martin Rehfeld
|
@@ -50,6 +50,7 @@ files:
|
|
50
50
|
- lib/role_model.rb
|
51
51
|
- lib/role_model/roles.rb
|
52
52
|
- role_model.gemspec
|
53
|
+
- spec/custom_matchers.rb
|
53
54
|
- spec/role_model_spec.rb
|
54
55
|
- spec/roles_spec.rb
|
55
56
|
- spec/spec.opts
|
@@ -85,6 +86,7 @@ signing_key:
|
|
85
86
|
specification_version: 3
|
86
87
|
summary: Declare, assign and query roles with ease
|
87
88
|
test_files:
|
89
|
+
- spec/custom_matchers.rb
|
88
90
|
- spec/role_model_spec.rb
|
89
91
|
- spec/roles_spec.rb
|
90
92
|
- spec/spec_helper.rb
|