Class: Discorb::Flag Abstract
- Inherits:
-
Object
- Object
- Discorb::Flag
- Defined in:
- lib/discorb/flag.rb
Overview
Represents a flag.
Direct Known Subclasses
Application::Flag, Message::Flag, Permission, Presence::Activity::Flag, SystemChannelFlag, User::Flag
Class Attribute Summary collapse
-
.bits -> Hash{Integer => Symbol}
readonly
The bits of the flag.
Instance Attribute Summary collapse
-
#value -> Integer
readonly
The value of the flag.
-
#values -> Hash{Symbol => Boolean}
(also: #to_h)
readonly
The values of the flag.
Class Method Summary collapse
-
.from_keys(*keys) -> Object
Initialize a new flag with keys.
-
.max_value -> Integer
Max value of the flag.
Instance Method Summary collapse
-
#&(other) -> Discorb::Flag
Intersection of two flags.
-
#-(other) -> Discorb::Flag
Subtraction of two flags.
-
#^(other) -> Discorb::Flag
XOR of two flags.
-
#initialize(value) -> Flag
constructor
Initialize the flag.
- #inspect -> Object
-
#method_missing(name, *_args, **_kwargs) -> Object
Returns the value of the flag.
- #respond_to_missing?(sym, include_private) -> Boolean
- #to_i -> Object
-
#|(other) -> Discorb::Flag
(also: #+)
Union of two flags.
-
#~ -> Discorb::Flag
Negation of the flag.
Constructor Details
#initialize(value) -> Flag
This is usually called by the subclass.
Initialize the flag.
21 22 23 24 25 26 27 |
# File 'lib/discorb/flag.rb', line 21 def initialize(value) @value = value @values = {} self.class.bits.each do |bn, bv| @values[bn] = value & (1 << bv) != 0 end 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.
32 33 34 35 36 37 38 |
# File 'lib/discorb/flag.rb', line 32 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.
109 110 111 |
# File 'lib/discorb/flag.rb', line 109 def bits @bits end |
Instance Attribute Details
#value -> Integer (readonly)
Returns 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.
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.
123 124 125 |
# File 'lib/discorb/flag.rb', line 123 def from_keys(*keys) new(keys.sum { |k| 1 << @bits[k] }) end |
.max_value -> Integer
Max value of the flag.
116 117 118 |
# File 'lib/discorb/flag.rb', line 116 def max_value 2 ** @bits.values.max - 1 end |
Instance Method Details
#&(other) -> Discorb::Flag
Intersection of two flags.
75 76 77 |
# File 'lib/discorb/flag.rb', line 75 def &(other) self.class.new(@value & other.value) end |
#-(other) -> Discorb::Flag
Subtraction of two flags.
64 65 66 |
# File 'lib/discorb/flag.rb', line 64 def -(other) self.class.new(@value & (@value ^ other.value)) end |
#^(other) -> Discorb::Flag
XOR of two flags.
86 87 88 |
# File 'lib/discorb/flag.rb', line 86 def ^(other) self.class.new(@value ^ other.value) end |
#inspect -> Object
103 104 105 |
# File 'lib/discorb/flag.rb', line 103 def inspect "#<#{self.class}: #{@value}>" end |
#respond_to_missing?(sym, include_private) -> Boolean
40 41 42 |
# File 'lib/discorb/flag.rb', line 40 def respond_to_missing?(sym, include_private) @values.key?(name.to_s.delete_suffix("?").to_sym) ? true : super end |
#to_i -> Object
99 100 101 |
# File 'lib/discorb/flag.rb', line 99 def to_i @value end |
#|(other) -> Discorb::Flag Also known as: +
Union of two flags.
51 52 53 |
# File 'lib/discorb/flag.rb', line 51 def |(other) self.class.new(@value | other.value) end |
#~ -> Discorb::Flag
Negation of the flag.
95 96 97 |
# File 'lib/discorb/flag.rb', line 95 def ~ self.class.new(~@value) end |