class Discorb::PermissionOverwrite

Represents a permission per channel.

Attributes

bits[R]

@!visibility private

Public Class Methods

from_hash(hash) click to toggle source

Initializes a permission overwrite from a hash.

@param [Hash] hash The hash to initialize the permission overwrite from.

@return [Discorb::PermissionOverwrite] The permission overwrite.

# File lib/discorb/permission.rb, line 261
def from_hash(hash)
  allow = 0
  deny = 0
  hash.filter { |k, v| self.class.bits.keys.include?(k) && [true, false].include?(v) }.each do |k, v|
    if v
      allow += self.class.bits[k]
    else
      deny += self.class.bits[k]
    end
  end

  new(allow, deny)
end
new(allow, deny) click to toggle source

@!visibility private

# File lib/discorb/permission.rb, line 142
def initialize(allow, deny)
  @allow = allow
  @deny = deny
end

Public Instance Methods

+(other) click to toggle source

Union of the permission overwrites.

@param [Discorb::PermissionOverwrite] other The other permission overwrite.

@return [Discorb::PermissionOverwrite] The union of the permission overwrites.

# File lib/discorb/permission.rb, line 189
def +(other)
  result = to_hash
  self.class.bits.each_key do |field|
    result[field] = other[field] unless other[field].nil?
  end
  self.class.from_hash(result)
end
+@()
Alias for: allow
-@()
Alias for: deny
[](field) click to toggle source

Returns whether overwrite of the given field.

@param [Symbol] field The field to check.

@return [true, false, nil] Whether the field is allowed, denied or not set.

# File lib/discorb/permission.rb, line 204
def [](field)
  if @allow & self.class.bits[field] != 0
    true
  elsif @deny & self.class.bits[field] != 0
    false
  end
end
[]=(key, bool) click to toggle source

Sets the given field to the given value.

@param [Symbol] key The field to set. @param [Boolean] bool The value to set.

# File lib/discorb/permission.rb, line 218
def []=(key, bool)
  case bool
  when true
    @allow |= self.class.bits[key]
    @deny &= ~self.class.bits[key]
  when false
    @allow &= ~self.class.bits[key]
    @deny |= self.class.bits[key]
  else
    @allow &= ~self.class.bits[key]
    @deny &= ~self.class.bits[key]
  end
end
allow() click to toggle source
# File lib/discorb/permission.rb, line 147
def allow
  self.class.bits.keys.filter { |field| @allow & self.class.bits[field] != 0 }
end
Also aliased as: +@
allow_value() click to toggle source
# File lib/discorb/permission.rb, line 159
def allow_value
  @allow
end
deny() click to toggle source
# File lib/discorb/permission.rb, line 153
def deny
  self.class.bits.keys.filter { |field| @deny & self.class.bits[field] != 0 }
end
Also aliased as: -@
deny_value() click to toggle source
# File lib/discorb/permission.rb, line 163
def deny_value
  @deny
end
method_missing(method, bool = nil) click to toggle source

(see Flag#method_missing)

Calls superclass method
# File lib/discorb/permission.rb, line 235
def method_missing(method, bool = nil)
  if self.class.bits.key?(method)
    self[method]
  elsif self.class.bits.key?(method.to_s.delete_suffix("=").to_sym)
    key = method.to_s.delete_suffix("=").to_sym
    self[key] = bool
  else
    super
  end
end
respond_to_missing?(method, _arg) click to toggle source
Calls superclass method
# File lib/discorb/permission.rb, line 246
def respond_to_missing?(method, _arg)
  self.class.bits.key?(method.to_s.delete_suffix("=").to_sym) ? true : super
end
to_hash() click to toggle source

Converts the permission overwrite to a hash.

@return [Hash] The permission overwrite as a hash.

# File lib/discorb/permission.rb, line 172
def to_hash
  self.class.bits.keys.map do |field|
    [field, if @allow & self.class.bits[field] != 0
      true
    elsif @deny & self.class.bits[method] != 0
      false
    end]
  end.to_h
end