class Bitwise::Permissions

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.cr

Constructors

Instance Method Summary

Constructor Detail

def self.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

[View source]
def self.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

[View source]

Instance Method Detail

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

[View source]
def 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")

[View source]
def bitwise : Int32 #

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)

[View source]
def bitwise_value : Int32 #

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)

[View source]
def 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")

[View source]
def 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\"")

[View source]
def 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```

[View source]
def 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```

[View source]
def to_a : Array(Int32) #

[View source]
def to_i : Int32 #

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)

[View source]