class Discorb::Flag

Represents a flag. @abstract

Attributes

bits[R]

@return [Hash{Integer => Symbol}] the bits of the flag.

value[R]

@return [Integer] the value of the flag.

values[R]

@return [Hash{Symbol => Boolean}] the values of the flag.

Public Class Methods

max_value() click to toggle source

Max value of the flag.

@return [Integer] the max value of the flag.

# File lib/discorb/flag.rb, line 107
def max_value
  2 ** @bits.values.max - 1
end
new(value) click to toggle source

Initialize the flag. @note This is usually called by the subclass.

@param [Integer] value The value of the flag.

# File lib/discorb/flag.rb, line 20
def initialize(value)
  @value = value
  @values = {}
  self.class.bits.each_with_index do |(bn, bv), _i|
    @values[bn] = value & (1 << bv) != 0
  end
end

Public Instance Methods

&(other) click to toggle source

Intersection of two flags.

@param [Discorb::Flag] other The other flag.

@return [Discorb::Flag] The intersection of the two flags.

# File lib/discorb/flag.rb, line 74
def &(other)
  self.class.new(@value & other.value)
end
+(other)
Alias for: |
-(other) click to toggle source

Subtraction of two flags.

@param [Discorb::Flag] other The other flag.

@return [Discorb::Flag] The subtraction of the two flags.

# File lib/discorb/flag.rb, line 63
def -(other)
  self.class.new(@value & (@value ^ other.value))
end
^(other) click to toggle source

XOR of two flags.

@param [Discorb::Flag] other The other flag.

@return [Discorb::Flag] The XOR of the two flags.

# File lib/discorb/flag.rb, line 85
def ^(other)
  self.class.new(@value ^ other.value)
end
method_missing(name, args = nil) click to toggle source

Returns the value of the flag.

Calls superclass method
# File lib/discorb/flag.rb, line 31
def method_missing(name, args = nil)
  if @values.key?(name.to_s.delete_suffix("?").to_sym)
    @values[name.to_s.delete_suffix("?").to_sym]
  else
    super
  end
end
respond_to_missing?(sym, include_private) click to toggle source
Calls superclass method
# File lib/discorb/flag.rb, line 39
def respond_to_missing?(sym, include_private)
  @values.key?(name.to_s.delete_suffix("?").to_sym) ? true : super
end
|(other) click to toggle source

Union of two flags.

@param [Discorb::Flag] other The other flag.

@return [Discorb::Flag] The union of the two flags.

# File lib/discorb/flag.rb, line 50
def |(other)
  self.class.new(@value | other.value)
end
Also aliased as: +