class Discorb::Flag
Represents a flag. @abstract
Attributes
@return [Hash{Integer => Symbol}] the bits of the flag.
@return [Integer] the value of the flag.
@return [Hash{Symbol => Boolean}] the values of the flag.
Public Class Methods
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
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
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
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
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
Returns the value of the flag.
# 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
# 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
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