Class: Discorb::Flag Abstract

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

Overview

This class is abstract.

Represents a flag.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) -> Flag

Note:

This is usually called by the subclass.

Initialize the flag.

Parameters:

  • value (Integer)

    The value of the flag.



21
22
23
24
25
# File 'lib/discorb/flag.rb', line 21

def initialize(value)
  @value = value
  @values = {}
  self.class.bits.each { |bn, bv| @values[bn] = value & (1 << bv) != 0 }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *_args, **_kwargs) -> Object

Returns the value of the flag.



30
31
32
33
34
35
36
# File 'lib/discorb/flag.rb', line 30

def method_missing(name, *_args, **_kwargs)
  if @values.key?(name.to_s.delete_suffix("?").to_sym)
    @values[name.to_s.delete_suffix("?").to_sym]
  else
    super
  end
end

Class Attribute Details

.bits -> Hash{Integer => Symbol} (readonly)

Returns the bits of the flag.

Returns:

  • (Hash{Integer => Symbol})

    the bits of the flag.



107
108
109
# File 'lib/discorb/flag.rb', line 107

def bits
  @bits
end

Instance Attribute Details

#value -> Integer (readonly)

Returns the value of the flag.

Returns:

  • (Integer)

    the value of the flag.



13
14
15
# File 'lib/discorb/flag.rb', line 13

def value
  @value
end

#values -> Hash{Symbol => Boolean} (readonly) Also known as: to_h

Returns the values of the flag.

Returns:

  • (Hash{Symbol => Boolean})

    the values of the flag.



10
11
12
# File 'lib/discorb/flag.rb', line 10

def values
  @values
end

Class Method Details

.from_keys(*keys) -> Object

Initialize a new flag with keys.



121
122
123
# File 'lib/discorb/flag.rb', line 121

def from_keys(*keys)
  new(keys.sum { |k| 1 << @bits[k] })
end

.max_value -> Integer

Max value of the flag.

Returns:

  • (Integer)

    the max value of the flag.



114
115
116
# File 'lib/discorb/flag.rb', line 114

def max_value
  (2**@bits.values.max) - 1
end

Instance Method Details

#&(other) -> Discorb::Flag

Intersection of two flags.

Parameters:

Returns:



73
74
75
# File 'lib/discorb/flag.rb', line 73

def &(other)
  self.class.new(@value & other.value)
end

#-(other) -> Discorb::Flag

Subtraction of two flags.

Parameters:

Returns:



62
63
64
# File 'lib/discorb/flag.rb', line 62

def -(other)
  self.class.new(@value & (@value ^ other.value))
end

#^(other) -> Discorb::Flag

XOR of two flags.

Parameters:

Returns:



84
85
86
# File 'lib/discorb/flag.rb', line 84

def ^(other)
  self.class.new(@value ^ other.value)
end

#inspect -> Object



101
102
103
# File 'lib/discorb/flag.rb', line 101

def inspect
  "#<#{self.class}: #{@value}>"
end

#respond_to_missing?(sym, include_private) -> Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/discorb/flag.rb', line 38

def respond_to_missing?(sym, include_private)
  @values.key?(name.to_s.delete_suffix("?").to_sym) ? true : super
end

#to_i -> Object



97
98
99
# File 'lib/discorb/flag.rb', line 97

def to_i
  @value
end

#|(other) -> Discorb::Flag Also known as: +

Union of two flags.

Parameters:

Returns:



49
50
51
# File 'lib/discorb/flag.rb', line 49

def |(other)
  self.class.new(@value | other.value)
end

#~ -> Discorb::Flag

Negation of the flag.

Returns:



93
94
95
# File 'lib/discorb/flag.rb', line 93

def ~
  self.class.new(~@value)
end