mirror of
https://github.com/Fishwaldo/role_model.git
synced 2025-03-15 19:42:43 +00:00
ability to retrieve the roles associated to a bitmask
This commit is contained in:
parent
16f90778b9
commit
0c82b94b32
3 changed files with 31 additions and 2 deletions
2
VERSION
2
VERSION
|
@ -1 +1 @@
|
|||
0.8.1
|
||||
0.8.2
|
||||
|
|
|
@ -23,7 +23,11 @@ module RoleModel
|
|||
|
||||
(valid_roles & sanitized_roles).inject(0) { |sum, role| sum + 2**valid_roles.index(role) }
|
||||
end
|
||||
|
||||
|
||||
def roles_from_mask(mask)
|
||||
valid_roles.reject { |role| (mask.to_i & 2**valid_roles.index(role)).zero? }
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
# :call-seq:
|
||||
|
|
|
@ -51,6 +51,31 @@ describe RoleModel do
|
|||
end
|
||||
end
|
||||
|
||||
describe '.roles_from_mask(mask)' do
|
||||
subject { model_class.roles_from_mask(mask) }
|
||||
|
||||
before do
|
||||
model_class.instance_eval do
|
||||
include RoleModel
|
||||
roles :foo, :bar, :third
|
||||
end
|
||||
end
|
||||
|
||||
context 'when roles_mask is 0' do
|
||||
let(:mask) { 0 }
|
||||
it 'returns empty set' do
|
||||
subject.should eq []
|
||||
end
|
||||
end
|
||||
|
||||
context 'when valid roles_mask' do
|
||||
let(:mask) { 3 }
|
||||
it 'returns the list of roles associated with a roles_mask' do
|
||||
subject.should eq [:foo, :bar]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
[:roles, :role_symbols].each do |role_query_method|
|
||||
describe "##{role_query_method}" do
|
||||
let(:model) { model_class.new }
|
||||
|
|
Loading…
Add table
Reference in a new issue