Bitwise::Permissions
Inherits Reference < Object
A library to handle permissions bitwisely NOTE: More examples in the documentation
require "bitwise"
class Permissions < Bitwise::Permissions
READ = 0
WRITE = 1
EDIT = 2
DELETE = 3
end
perms = Permissions.new(Permissions::READ, Permissions::WRITE)
perms.has_perm Permissions::READ # true
perms.has_perm Permissions::DELETE # false
perms.add_perm Permissions::DELETE
perms.has_perm Permissions::DELETE # true
perms.del_perm Permissions::DELETE
perms.has_perm Permissions::DELETE # false
perms.to_i #=> 3 | (bitwise value, easier to store)
perms2 = Permission.new(3)
perms2.to_a #=> [0, 1] | (READ, WRITE)
# More examples in the documentation
Constructors
Create instance with bitwise value
require "bitwise"
class Permissions < Bitwise::Permissions
READ = 0
WRITE = 1
EDIT = 2
DELETE = 3
end
perms = Permissions.new(3) # Has perm READ and WRITE
Create instance with permission constant
require "bitwise"
class Permissions < Bitwise::Permissions
READ = 0
WRITE = 1
EDIT = 2
DELETE = 3
end
perms = Permissions.new(Permissions::READ, Permissions::WRITE) # Has perm READ and WRITE
Instance methods
Add permission to instance NOTE: Add strict: true if you want that the code raise an error if perm already added
require "bitwise"
class Permissions < Bitwise::Permissions
READ = 0
WRITE = 1
EDIT = 2
DELETE = 3
end
perms = Permissions.new(Permissions::READ, Permissions::WRITE)
perms.add_perm Permissions::DELETE # Add the permission DELETE to the instance
perms.add_perm Permissions::DELETE, strict: true #=> ArgumentError("Perm already added")
Add multiple permissions to instance NOTE: Add strict: true if you want that the code raise an error if one perm is already added
require "bitwise"
class Permissions < Bitwise::Permissions
READ = 0
WRITE = 1
EDIT = 2
DELETE = 3
end
perms = Permissions.new(Permissions::READ, Permissions::WRITE)
perms.add_perms Permissions::EDIT, Permissions::DELETE # Add the permissions EDIT and DELETE to the instance
perms.add_perms Permissions::EDIT, Permissions::DELETE, strict: true #=> ArgumentError("Perm \"2\" already added")
Return bitwise value Example:
require "bitwise"
class Permissions < Bitwise::Permissions
READ = 0
WRITE = 1
EDIT = 2
DELETE = 3
end
perms = Permissions.new(Permissions::READ, Permissions::WRITE)
perms.bitwise #=> 3 # (bitwise value)
Return bitwise value Example:
require "bitwise"
class Permissions < Bitwise::Permissions
READ = 0
WRITE = 1
EDIT = 2
DELETE = 3
end
perms = Permissions.new(Permissions::READ, Permissions::WRITE)
perms.bitwise_value #=> 3 # (bitwise value)
Remove permission to instance NOTE: Add strict: true if you want that the code raise an error if perm already deleted
require "bitwise"
class Permissions < Bitwise::Permissions
READ = 0
WRITE = 1
EDIT = 2
DELETE = 3
end
perms = Permissions.new(Permissions::READ, Permissions::WRITE)
perms.del_perm Permissions::WRITE # Remove the permission WRITE to the instance
perms.del_perm Permissions::WRITE, strict: true #=> ArgumentError("Instance doesn't have this perm")
Remove multiple permissions to instance NOTE: Add strict: true if you want that the code raise an error if one perm is already deleted
require "bitwise"
class Permissions < Bitwise::Permissions
READ = 0
WRITE = 1
EDIT = 2
DELETE = 3
end
perms = Permissions.new(Permissions::READ, Permissions::WRITE)
perms.del_perm Permissions::READ, Permissions::WRITE # Remove the permission WRITE to the instance
perms.del_perm Permissions::READ, Permissions::WRITE, strict: true #=> ArgumentError("Instance doesn't have the perm\"0\"")
Check if instance has the perm
require "bitwise"
class Permissions < Bitwise::Permissions
READ = 0
WRITE = 1
EDIT = 2
DELETE = 3
end
perms = Permissions.new(Permissions::READ, Permissions::WRITE)
perms.has_perm Permissions::READ # true
perms.has_perm Permissions::DELETE # false```
Check if instance has all the perms
require "bitwise"
class Permissions < Bitwise::Permissions
READ = 0
WRITE = 1
EDIT = 2
DELETE = 3
end
perms = Permissions.new(Permissions::READ, Permissions::WRITE)
perms.has_perms Permissions::READ, Permission::WRITE #=> true
perms.has_perms Permissions::READ, Permissions::DELETE #=> false```
Return bitwise value Example:
require "bitwise"
class Permissions < Bitwise::Permissions
READ = 0
WRITE = 1
EDIT = 2
DELETE = 3
end
perms = Permissions.new(Permissions::READ, Permissions::WRITE)
perms.to_i #=> 3 # (bitwise value)