Class: RubyCord::PermissionOverwrite

Inherits:
Object
  • Object
show all
Defined in:
lib/rubycord/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

Returns the value of the flag.



262
263
264
265
266
267
268
269
270
271
# File 'lib/rubycord/permission.rb', line 262

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) ⇒ RubyCord::PermissionOverwrite

Initializes a permission overwrite from a hash.

Parameters:

  • hash (Hash)

    The hash to initialize the permission overwrite from.

Returns:



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/rubycord/permission.rb', line 288

def from_hash(hash)
  allow = 0
  deny = 0
  hash
    .filter do |k, v|
      self.class.bits.keys.include?(k) && [true, false].include?(v)
    end
    .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) ⇒ RubyCord::PermissionOverwrite

Union of the permission overwrites.

Parameters:

Returns:



212
213
214
215
216
217
218
219
220
221
222
# File 'lib/rubycord/permission.rb', line 212

def +(other)
  result = to_hash
  self.class.bits.each_key do |field|
    next if other[field].nil?
    result[field] = (
      other[field] ||
        raise(KeyError, "field #{field} not found in #{other.inspect}")
    )
  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.



231
232
233
234
235
236
237
# File 'lib/rubycord/permission.rb', line 231

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.



245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/rubycord/permission.rb', line 245

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

#allowObject Also known as: +@



158
159
160
161
162
# File 'lib/rubycord/permission.rb', line 158

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

#allow_valueObject



174
175
176
# File 'lib/rubycord/permission.rb', line 174

def allow_value
  @allow
end

#denyObject Also known as: -@



166
167
168
169
170
# File 'lib/rubycord/permission.rb', line 166

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

#deny_valueObject



178
179
180
# File 'lib/rubycord/permission.rb', line 178

def deny_value
  @deny
end

#inspectString

Returns Object class and attributes.

Returns:

  • (String)

    Object class and attributes.



183
184
185
# File 'lib/rubycord/permission.rb', line 183

def inspect
  "#<#{self.class} allow=#{allow} deny=#{deny}>"
end

#respond_to_missing?(method, _arg) ⇒ Boolean

Returns:

  • (Boolean)


273
274
275
# File 'lib/rubycord/permission.rb', line 273

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

#to_hashHash

Converts the permission overwrite to a hash.

Returns:

  • (Hash)

    The permission overwrite as a hash.



192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/rubycord/permission.rb', line 192

def to_hash
  self.class.bits.keys.to_h do |field|
    [
      field,
      if @allow & self.class.bits[field] != 0
        true
      elsif @deny & self.class.bits[field] != 0
        false
      end
    ]
  end
end