class

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

new(bitwise : Int)

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
Source
new(*perms : Int)

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
Source

Instance methods

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

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")
Source
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

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")
Source
bitwise

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)
Source
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)
Source
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

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")
Source
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

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\"")
Source
has_perm(perm : Int)

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```
Source
has_perms(*perms : Int)

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```
Source
to_a
Source
to_i

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)
Source