role_model 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -3,12 +3,8 @@
3
3
  Ever needed to assign roles to a model, say a User, and build conditional
4
4
  behaviour on top of that?
5
5
 
6
- Enter RoleModel -- roles have never been easier! No need to build a separate
7
- Role model, managing seed data and the like. Just declare your roles and you
8
- are done.
9
-
10
6
  Assigned roles will be stored as a bitmask in an configurable attribute
11
- (default: <tt>roles_mask</tt>). Here's everything you need to know:
7
+ (default: <tt>roles_mask</tt>). Here's how to get started:
12
8
 
13
9
  # given a User class with a roles_mask attribute
14
10
  require 'rubygems'
@@ -18,6 +14,7 @@ Assigned roles will be stored as a bitmask in an configurable attribute
18
14
  attr_accessor :roles_mask # in real life this would be an persisted attribute / DB-column
19
15
 
20
16
  include RoleModel
17
+
21
18
  # optionally set the integer attribute to store the roles in,
22
19
  # :roles_mask is the default
23
20
  roles_attribute :roles_mask
@@ -27,23 +24,36 @@ Assigned roles will be stored as a bitmask in an configurable attribute
27
24
  roles :admin, :manager, :author
28
25
  end
29
26
 
27
+ #
28
+ # Test drive
29
+ #
30
+
30
31
  >> u = User.new
31
32
  => #<User ...>
32
33
 
34
+ # role assignment
33
35
  >> u.roles = [:admin] # ['admin'] works as well
34
36
  => [:admin]
35
37
 
36
- >> u.roles # aliased to role_symbols for DeclarativeAuthorization
37
- => [:admin]
38
+ # adding roles
39
+ >> u.roles << :manager
40
+ => [:admin, :manager]
38
41
 
39
- >> u.has_role? :manager
42
+ # quering roles...
43
+
44
+ # ... retrieve all assigned roles
45
+ >> u.roles # aliased to role_symbols for DeclarativeAuthorization
46
+ => [:admin, :manager]
47
+
48
+ # ... check for individual roles
49
+ >> u.has_role? :author
40
50
  => false
41
-
42
51
  >> u.has_role? :admin
43
52
  => true
44
-
53
+
54
+ # see the internal bitmask representation (3 = 0b0011)
45
55
  >> u.roles_mask
46
- => 1
56
+ => 3
47
57
 
48
58
  Once you have included RoleModel, your model is perfectly fit to be used
49
59
  together with a role-based authorization solution, such as
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -0,0 +1,14 @@
1
+ module RoleModel
2
+ class Roles < Array
3
+ attr_reader :model_instance
4
+
5
+ def initialize(sender, *roles)
6
+ super(*roles)
7
+ @model_instance = sender
8
+ end
9
+
10
+ def <<(role)
11
+ model_instance.roles = super
12
+ end
13
+ end
14
+ end
data/lib/role_model.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'role_model/roles'
2
+
1
3
  module RoleModel
2
4
 
3
5
  INHERITABLE_CLASS_ATTRIBUTES = [:roles_attribute_name, :valid_roles]
@@ -6,7 +8,7 @@ module RoleModel
6
8
  base.extend ClassMethods
7
9
  base.class_eval do
8
10
  class << self
9
- attr_accessor *::RoleModel::INHERITABLE_CLASS_ATTRIBUTES
11
+ attr_accessor(*::RoleModel::INHERITABLE_CLASS_ATTRIBUTES)
10
12
  end
11
13
  roles_attribute :roles_mask
12
14
  end
@@ -18,7 +20,7 @@ module RoleModel
18
20
 
19
21
  # query assigned roles
20
22
  def roles
21
- self.class.valid_roles.reject { |r| ((self.send(self.class.roles_attribute_name) || 0) & 2**self.class.valid_roles.index(r)).zero? }
23
+ Roles.new(self, self.class.valid_roles.reject { |r| ((self.send(self.class.roles_attribute_name) || 0) & 2**self.class.valid_roles.index(r)).zero? })
22
24
  end
23
25
  alias role_symbols roles
24
26
 
data/role_model.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{role_model}
8
- s.version = "0.2.0"
8
+ s.version = "0.3.0"
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 = %q{2010-05-25}
12
+ s.date = %q{2010-05-29}
13
13
  s.description = %q{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
14
  s.email = %q{martin.rehfeld@glnetworks.de}
15
15
  s.extra_rdoc_files = [
@@ -24,8 +24,10 @@ Gem::Specification.new do |s|
24
24
  "Rakefile",
25
25
  "VERSION",
26
26
  "lib/role_model.rb",
27
+ "lib/role_model/roles.rb",
27
28
  "role_model.gemspec",
28
29
  "spec/role_model_spec.rb",
30
+ "spec/roles_spec.rb",
29
31
  "spec/spec.opts",
30
32
  "spec/spec_helper.rb"
31
33
  ]
@@ -36,6 +38,7 @@ Gem::Specification.new do |s|
36
38
  s.summary = %q{Declare, assign and query roles with ease}
37
39
  s.test_files = [
38
40
  "spec/role_model_spec.rb",
41
+ "spec/roles_spec.rb",
39
42
  "spec/spec_helper.rb"
40
43
  ]
41
44
 
@@ -9,7 +9,7 @@ describe RoleModel do
9
9
  attr_accessor :roles_mask
10
10
  attr_accessor :custom_roles_mask
11
11
  include RoleModel
12
- roles :foo, :bar
12
+ roles :foo, :bar, :third
13
13
  end
14
14
  end
15
15
 
@@ -113,6 +113,67 @@ describe RoleModel do
113
113
  subject.roles = :baz
114
114
  subject.roles.should be_empty
115
115
  end
116
+
117
+ context "with previouly assigned roles" do
118
+ before(:each) do
119
+ subject.roles = :foo
120
+ subject.roles.should include(:foo)
121
+ subject.should have(1).roles
122
+ end
123
+
124
+ it "should set set assigned roles regardless overwriting previouly assigned roles" do
125
+ subject.roles = :bar
126
+ subject.roles.should include(:bar)
127
+ subject.should have(1).roles
128
+ end
129
+ end
130
+ end
131
+
132
+ describe "#<<" do
133
+ subject { model_class.new }
134
+
135
+ context "with roles :foo and :bar already assigned" do
136
+ before(:each) do
137
+ subject.roles = [:foo, :bar]
138
+ end
139
+
140
+ it "should add a role given as a symbol" do
141
+ subject.roles << :third
142
+ subject.roles.should include(:foo, :bar, :third)
143
+ subject.should have(3).roles
144
+ end
145
+
146
+ it "should add a role given as a string" do
147
+ subject.roles << 'third'
148
+ subject.roles.should include(:foo, :bar, :third)
149
+ subject.should have(3).roles
150
+ end
151
+ end
152
+
153
+ context "without any previouly assigned roles" do
154
+ it "should add a single symbol" do
155
+ subject.roles << :foo
156
+ subject.roles.should include(:foo)
157
+ subject.should have(1).roles
158
+ end
159
+
160
+ it "should add a single string" do
161
+ subject.roles << 'foo'
162
+ subject.roles.should include(:foo)
163
+ subject.should have(1).roles
164
+ end
165
+
166
+ it "should allow chaining of <<" do
167
+ subject.roles << :foo << :bar
168
+ subject.roles.should include(:foo, :bar)
169
+ subject.should have(2).roles
170
+ end
171
+ end
172
+
173
+ it "should silently ignore undefined roles" do
174
+ subject.roles << :baz
175
+ subject.roles.should be_empty
176
+ end
116
177
  end
117
178
 
118
179
  describe "#has_role?" do
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe RoleModel::Roles do
4
+
5
+ let(:model_instance) { Class.new.new }
6
+ let(:array) { [:foo, :bar] }
7
+ subject { RoleModel::Roles.new(model_instance, array) }
8
+
9
+ its(:model_instance) { should == model_instance }
10
+ it { should include(:foo, :bar) }
11
+ it { should be_kind_of(Array) }
12
+
13
+ describe "#<<" do
14
+ let(:roles_of_model_instance) { [:one, :two] }
15
+
16
+ it "should add the given element to the model_instance.roles by re-assigning all roles" do
17
+ model_instance.should_receive(:roles=).with([:foo, :bar, :baz])
18
+ subject << :baz
19
+ end
20
+ end
21
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
7
+ - 3
8
8
  - 0
9
- version: 0.2.0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Martin Rehfeld
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-25 00:00:00 +02:00
17
+ date: 2010-05-29 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -48,8 +48,10 @@ files:
48
48
  - Rakefile
49
49
  - VERSION
50
50
  - lib/role_model.rb
51
+ - lib/role_model/roles.rb
51
52
  - role_model.gemspec
52
53
  - spec/role_model_spec.rb
54
+ - spec/roles_spec.rb
53
55
  - spec/spec.opts
54
56
  - spec/spec_helper.rb
55
57
  has_rdoc: true
@@ -84,4 +86,5 @@ specification_version: 3
84
86
  summary: Declare, assign and query roles with ease
85
87
  test_files:
86
88
  - spec/role_model_spec.rb
89
+ - spec/roles_spec.rb
87
90
  - spec/spec_helper.rb