class Bitwise::Permissions
- Bitwise::Permissions
- Reference
- Object
Overview
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
Defined in:
bitwise/permissions.crConstructors
-
.new(bitwise : Int)
Create instance with bitwise value
-
.new(*perms : Int)
Create instance with permission constant
Instance Method Summary
-
#add_perm(perm : Int, strict = false)
Add permission to instance NOTE: Add strict: true if you want that the code raise an error if perm already added
-
#add_perms(*perms : Int, strict = false)
Add multiple permissions to instance NOTE: Add strict: true if you want that the code raise an error if one perm is already added
-
#bitwise : Int32
Return bitwise value Example:
`crystal require "bitwise"
-
#bitwise_value : Int32
Return bitwise value Example:
`crystal require "bitwise"
-
#del_perm(perm : Int, strict = false)
Remove permission to instance NOTE: Add strict: true if you want that the code raise an error if perm already deleted
-
#del_perms(*perms : Int, strict = false)
Remove multiple permissions to instance NOTE: Add strict: true if you want that the code raise an error if one perm is already deleted
-
#has_perm(perm : Int)
Check if instance has the perm
-
#has_perms(*perms : Int)
Check if instance has all the perms
- #to_a : Array(Int32)
-
#to_i : Int32
Return bitwise value Example:
`crystal require "bitwise"
Constructor Detail
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 Method Detail
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)