Class: Discorb::PermissionOverwrite

Inherits:
Object
  • Object
show all
Defined in:
lib/discorb/permission.rb

Overview

Represents a permission per channel.

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, bool = nil) -> Object



229
230
231
232
233
234
235
236
237
238
# File 'lib/discorb/permission.rb', line 229

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

Class Method Details

.from_hash(hash) -> Discorb::PermissionOverwrite

Initializes a permission overwrite from a hash.

Parameters:

  • hash (Hash)

    The hash to initialize the permission overwrite from.

Returns:



255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/discorb/permission.rb', line 255

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

Instance Method Details

#+(other) -> Discorb::PermissionOverwrite

Union of the permission overwrites.

Parameters:

Returns:



186
187
188
189
190
191
192
# File 'lib/discorb/permission.rb', line 186

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

#[](field) -> true, ...

Returns whether overwrite of the given field.

Parameters:

  • field (Symbol)

    The field to check.

Returns:

  • (true, false, nil)

    Whether the field is allowed, denied or not set.



201
202
203
204
205
206
207
# File 'lib/discorb/permission.rb', line 201

def [](field)
  if @allow & self.class.bits[field] != 0
    true
  elsif @deny & self.class.bits[field] != 0
    false
  end
end

#[]=(key, bool) -> Object

Sets the given field to the given value.

Parameters:

  • key (Symbol)

    The field to set.

  • bool (Boolean)

    The value to set.



215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/discorb/permission.rb', line 215

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 -> Object Also known as: +@



144
145
146
# File 'lib/discorb/permission.rb', line 144

def allow
  self.class.bits.keys.filter { |field| @allow & self.class.bits[field] != 0 }
end

#allow_value -> Object



156
157
158
# File 'lib/discorb/permission.rb', line 156

def allow_value
  @allow
end

#deny -> Object Also known as: -@



150
151
152
# File 'lib/discorb/permission.rb', line 150

def deny
  self.class.bits.keys.filter { |field| @deny & self.class.bits[field] != 0 }
end

#deny_value -> Object



160
161
162
# File 'lib/discorb/permission.rb', line 160

def deny_value
  @deny
end

#respond_to_missing?(method, _arg) -> Boolean

Returns:

  • (Boolean)


240
241
242
# File 'lib/discorb/permission.rb', line 240

def respond_to_missing?(method, _arg)
  self.class.bits.key?(method.to_s.delete_suffix("=").to_sym) ? true : super
end

#to_hash -> Hash

Converts the permission overwrite to a hash.

Returns:

  • (Hash)

    The permission overwrite as a hash.



169
170
171
172
173
174
175
176
177
# File 'lib/discorb/permission.rb', line 169

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