role_model 0.7.0 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +7 -0
- data/Gemfile +1 -0
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/lib/role_model/class_methods.rb +4 -10
- data/lib/role_model/implementation.rb +3 -3
- data/role_model.gemspec +12 -16
- data/spec/custom_matchers_spec.rb +5 -5
- data/spec/role_model_spec.rb +13 -9
- metadata +27 -15
- data/Gemfile.lock +0 -29
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.7.
|
1
|
+
0.7.1
|
@@ -19,15 +19,9 @@ module RoleModel
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def mask_for(*roles)
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
r.to_sym
|
26
|
-
} & valid_roles).map { |r|
|
27
|
-
2**valid_roles.index(r)
|
28
|
-
}.inject { |sum, bitvalue|
|
29
|
-
sum + bitvalue
|
30
|
-
} || 0
|
22
|
+
sanitized_roles = roles.map { |role| Array(role) }.flatten.map(&:to_sym)
|
23
|
+
|
24
|
+
(valid_roles & sanitized_roles).inject(0) { |sum, role| sum + 2**valid_roles.index(role) }
|
31
25
|
end
|
32
26
|
|
33
27
|
protected
|
@@ -40,7 +34,7 @@ module RoleModel
|
|
40
34
|
#
|
41
35
|
# declare valid roles
|
42
36
|
def roles(*roles)
|
43
|
-
self.valid_roles =
|
37
|
+
self.valid_roles = roles.flatten.map(&:to_sym)
|
44
38
|
end
|
45
39
|
end
|
46
40
|
end
|
@@ -20,10 +20,10 @@ module RoleModel
|
|
20
20
|
# has_all_roles?([:role_1, ..., :role_n])
|
21
21
|
# has_all_roles?(['role_1', ..., 'role_n'])
|
22
22
|
#
|
23
|
-
# check if ALL of the given roles have been assigned
|
23
|
+
# check if ALL of the given roles have been assigned
|
24
24
|
# this method is aliased as #is? and #has_roles?
|
25
25
|
def has_all_roles?(*roles)
|
26
|
-
roles.flatten.map
|
26
|
+
roles.flatten.map(&:to_sym).all? { |r| self.roles.include?(r) }
|
27
27
|
end
|
28
28
|
alias_method :is?, :has_all_roles?
|
29
29
|
alias_method :has_roles?, :has_all_roles?
|
@@ -39,7 +39,7 @@ module RoleModel
|
|
39
39
|
# check if any (at least ONE) of the given roles have been assigned
|
40
40
|
# this method is aliased as #is_any_of? and #has_role?
|
41
41
|
def has_any_role?(*roles)
|
42
|
-
roles.flatten.map
|
42
|
+
roles.flatten.map(&:to_sym).any? { |r| self.roles.include?(r) }
|
43
43
|
end
|
44
44
|
alias_method :is_any_of?, :has_any_role?
|
45
45
|
alias_method :has_role?, :has_any_role?
|
data/role_model.gemspec
CHANGED
@@ -4,14 +4,14 @@
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "0.7.
|
7
|
+
s.name = "role_model"
|
8
|
+
s.version = "0.7.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"]
|
12
|
-
s.date =
|
13
|
-
s.description =
|
14
|
-
s.email =
|
12
|
+
s.date = "2012-05-13"
|
13
|
+
s.description = "Ever needed to assign roles to a model, say a User, and build conditional behaviour on top of that? Enter RoleModel -- roles have never been easier! Just declare your roles and you are done. Assigned roles will be stored as a bitmask."
|
14
|
+
s.email = "martin.rehfeld@glnetworks.de"
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
17
|
"README.rdoc"
|
@@ -19,8 +19,8 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
21
|
".rspec",
|
22
|
+
".travis.yml",
|
22
23
|
"Gemfile",
|
23
|
-
"Gemfile.lock",
|
24
24
|
"LICENSE",
|
25
25
|
"README.rdoc",
|
26
26
|
"Rakefile",
|
@@ -36,18 +36,11 @@ Gem::Specification.new do |s|
|
|
36
36
|
"spec/roles_spec.rb",
|
37
37
|
"spec/spec_helper.rb"
|
38
38
|
]
|
39
|
-
s.homepage =
|
39
|
+
s.homepage = "http://github.com/martinrehfeld/role_model"
|
40
40
|
s.licenses = ["MIT"]
|
41
41
|
s.require_paths = ["lib"]
|
42
|
-
s.rubygems_version =
|
43
|
-
s.summary =
|
44
|
-
s.test_files = [
|
45
|
-
"spec/custom_matchers.rb",
|
46
|
-
"spec/custom_matchers_spec.rb",
|
47
|
-
"spec/role_model_spec.rb",
|
48
|
-
"spec/roles_spec.rb",
|
49
|
-
"spec/spec_helper.rb"
|
50
|
-
]
|
42
|
+
s.rubygems_version = "1.8.23"
|
43
|
+
s.summary = "Declare, assign and query roles with ease"
|
51
44
|
|
52
45
|
if s.respond_to? :specification_version then
|
53
46
|
s.specification_version = 3
|
@@ -55,17 +48,20 @@ Gem::Specification.new do |s|
|
|
55
48
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
56
49
|
s.add_development_dependency(%q<jeweler>, [">= 1.5.2"])
|
57
50
|
s.add_development_dependency(%q<rspec>, ["~> 2"])
|
51
|
+
s.add_development_dependency(%q<rdoc>, [">= 2.4.2"])
|
58
52
|
s.add_development_dependency(%q<autotest>, [">= 0"])
|
59
53
|
s.add_development_dependency(%q<rspec>, ["~> 2"])
|
60
54
|
else
|
61
55
|
s.add_dependency(%q<jeweler>, [">= 1.5.2"])
|
62
56
|
s.add_dependency(%q<rspec>, ["~> 2"])
|
57
|
+
s.add_dependency(%q<rdoc>, [">= 2.4.2"])
|
63
58
|
s.add_dependency(%q<autotest>, [">= 0"])
|
64
59
|
s.add_dependency(%q<rspec>, ["~> 2"])
|
65
60
|
end
|
66
61
|
else
|
67
62
|
s.add_dependency(%q<jeweler>, [">= 1.5.2"])
|
68
63
|
s.add_dependency(%q<rspec>, ["~> 2"])
|
64
|
+
s.add_dependency(%q<rdoc>, [">= 2.4.2"])
|
69
65
|
s.add_dependency(%q<autotest>, [">= 0"])
|
70
66
|
s.add_dependency(%q<rspec>, ["~> 2"])
|
71
67
|
end
|
@@ -2,10 +2,10 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
module CustomMatchers
|
4
4
|
describe ArrayIncludingMatcher do
|
5
|
-
|
5
|
+
|
6
6
|
it "should describe itself properly" do
|
7
7
|
ArrayIncludingMatcher.new(:a, :b).description.should == "array_including(:a, :b)"
|
8
|
-
end
|
8
|
+
end
|
9
9
|
|
10
10
|
describe "passing" do
|
11
11
|
it "should match the same array" do
|
@@ -15,7 +15,7 @@ module CustomMatchers
|
|
15
15
|
it "should match a array with extra stuff" do
|
16
16
|
array_including(:a).should == [:a, :b]
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
it "should match a array regardless of element position" do
|
20
20
|
array_including(:a, :b).should == [:b, :a]
|
21
21
|
end
|
@@ -38,7 +38,7 @@ module CustomMatchers
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
describe "failing" do
|
43
43
|
it "should not match a non-array" do
|
44
44
|
array_including(:a).should_not == :a
|
@@ -47,7 +47,7 @@ module CustomMatchers
|
|
47
47
|
it "should not match a array with a missing element" do
|
48
48
|
array_including(:a).should_not == [:b]
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
it "should not match an empty array with a given key" do
|
52
52
|
array_including(:a).should_not == []
|
53
53
|
end
|
data/spec/role_model_spec.rb
CHANGED
@@ -39,6 +39,10 @@ describe RoleModel do
|
|
39
39
|
subject.roles.should_not include(:baz)
|
40
40
|
end
|
41
41
|
|
42
|
+
it "should accept an argument list with roles" do
|
43
|
+
|
44
|
+
end
|
45
|
+
|
42
46
|
it "should define the bitvalue of each role by position" do
|
43
47
|
subject.roles = :foo
|
44
48
|
subject.roles_mask.should == 1
|
@@ -212,7 +216,7 @@ describe RoleModel do
|
|
212
216
|
subject.should have(2).roles
|
213
217
|
end
|
214
218
|
end
|
215
|
-
|
219
|
+
|
216
220
|
context "without roles assigned" do
|
217
221
|
it "should have 0 roles if a role is given as a symbol" do
|
218
222
|
subject.roles.delete(:foo)
|
@@ -335,9 +339,9 @@ describe RoleModel do
|
|
335
339
|
context "query for roles when none defined in model" do
|
336
340
|
[:has_any_role?, :is_any_of?, :has_role?, :has_all_roles?, :is?, :has_roles?].each do |check_role_assignment_method|
|
337
341
|
describe "##{check_role_assignment_method}" do
|
338
|
-
|
342
|
+
|
339
343
|
let(:model_class_without_roles) { Class.new }
|
340
|
-
|
344
|
+
|
341
345
|
before(:each) do
|
342
346
|
model_class_without_roles.instance_eval do
|
343
347
|
attr_accessor :roles_mask
|
@@ -345,8 +349,8 @@ describe RoleModel do
|
|
345
349
|
include RoleModel
|
346
350
|
end
|
347
351
|
end
|
348
|
-
|
349
|
-
|
352
|
+
|
353
|
+
|
350
354
|
subject { model_class_without_roles.new }
|
351
355
|
|
352
356
|
it "should return false when a role was assigned" do
|
@@ -363,9 +367,9 @@ describe RoleModel do
|
|
363
367
|
end
|
364
368
|
|
365
369
|
context "ClassMethods" do
|
366
|
-
|
370
|
+
|
367
371
|
subject { model_class }
|
368
|
-
|
372
|
+
|
369
373
|
describe ".roles" do
|
370
374
|
it "should not allow public access to set roles" do
|
371
375
|
lambda do
|
@@ -373,14 +377,14 @@ describe RoleModel do
|
|
373
377
|
end.should raise_exception(NoMethodError, /protected method.*roles.*called/)
|
374
378
|
end
|
375
379
|
end
|
376
|
-
|
380
|
+
|
377
381
|
end
|
378
382
|
|
379
383
|
context "inheritance" do
|
380
384
|
let(:superclass_instance) { model_class.new }
|
381
385
|
let(:inherited_model_class) { Class.new(model_class) }
|
382
386
|
subject { inherited_model_class.new }
|
383
|
-
|
387
|
+
|
384
388
|
it "should not allow public access to set roles" do
|
385
389
|
lambda do
|
386
390
|
inherited_model_class.roles :foo, :quux
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: role_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 1
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 7
|
9
|
-
-
|
10
|
-
version: 0.7.
|
9
|
+
- 1
|
10
|
+
version: 0.7.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Martin Rehfeld
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2012-05-13 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
requirement: &id001 !ruby/object:Gem::Requirement
|
@@ -49,6 +49,22 @@ dependencies:
|
|
49
49
|
type: :development
|
50
50
|
- !ruby/object:Gem::Dependency
|
51
51
|
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 27
|
57
|
+
segments:
|
58
|
+
- 2
|
59
|
+
- 4
|
60
|
+
- 2
|
61
|
+
version: 2.4.2
|
62
|
+
version_requirements: *id003
|
63
|
+
name: rdoc
|
64
|
+
prerelease: false
|
65
|
+
type: :development
|
66
|
+
- !ruby/object:Gem::Dependency
|
67
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
68
|
none: false
|
53
69
|
requirements:
|
54
70
|
- - ">="
|
@@ -57,12 +73,12 @@ dependencies:
|
|
57
73
|
segments:
|
58
74
|
- 0
|
59
75
|
version: "0"
|
60
|
-
version_requirements: *
|
76
|
+
version_requirements: *id004
|
61
77
|
name: autotest
|
62
78
|
prerelease: false
|
63
79
|
type: :development
|
64
80
|
- !ruby/object:Gem::Dependency
|
65
|
-
requirement: &
|
81
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
66
82
|
none: false
|
67
83
|
requirements:
|
68
84
|
- - ~>
|
@@ -71,7 +87,7 @@ dependencies:
|
|
71
87
|
segments:
|
72
88
|
- 2
|
73
89
|
version: "2"
|
74
|
-
version_requirements: *
|
90
|
+
version_requirements: *id005
|
75
91
|
name: rspec
|
76
92
|
prerelease: false
|
77
93
|
type: :development
|
@@ -87,8 +103,8 @@ extra_rdoc_files:
|
|
87
103
|
files:
|
88
104
|
- .document
|
89
105
|
- .rspec
|
106
|
+
- .travis.yml
|
90
107
|
- Gemfile
|
91
|
-
- Gemfile.lock
|
92
108
|
- LICENSE
|
93
109
|
- README.rdoc
|
94
110
|
- Rakefile
|
@@ -132,13 +148,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
132
148
|
requirements: []
|
133
149
|
|
134
150
|
rubyforge_project:
|
135
|
-
rubygems_version: 1.
|
151
|
+
rubygems_version: 1.8.23
|
136
152
|
signing_key:
|
137
153
|
specification_version: 3
|
138
154
|
summary: Declare, assign and query roles with ease
|
139
|
-
test_files:
|
140
|
-
|
141
|
-
- spec/custom_matchers_spec.rb
|
142
|
-
- spec/role_model_spec.rb
|
143
|
-
- spec/roles_spec.rb
|
144
|
-
- spec/spec_helper.rb
|
155
|
+
test_files: []
|
156
|
+
|
data/Gemfile.lock
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
GEM
|
2
|
-
remote: http://rubygems.org/
|
3
|
-
specs:
|
4
|
-
ZenTest (4.5.0)
|
5
|
-
autotest (4.4.6)
|
6
|
-
ZenTest (>= 4.4.1)
|
7
|
-
diff-lcs (1.1.2)
|
8
|
-
git (1.2.5)
|
9
|
-
jeweler (1.5.2)
|
10
|
-
bundler (~> 1.0.0)
|
11
|
-
git (>= 1.2.5)
|
12
|
-
rake
|
13
|
-
rake (0.8.7)
|
14
|
-
rspec (2.5.0)
|
15
|
-
rspec-core (~> 2.5.0)
|
16
|
-
rspec-expectations (~> 2.5.0)
|
17
|
-
rspec-mocks (~> 2.5.0)
|
18
|
-
rspec-core (2.5.1)
|
19
|
-
rspec-expectations (2.5.0)
|
20
|
-
diff-lcs (~> 1.1.2)
|
21
|
-
rspec-mocks (2.5.0)
|
22
|
-
|
23
|
-
PLATFORMS
|
24
|
-
ruby
|
25
|
-
|
26
|
-
DEPENDENCIES
|
27
|
-
autotest
|
28
|
-
jeweler (>= 1.5.2)
|
29
|
-
rspec (~> 2)
|