Class: Discorb::Flag Abstract
- Inherits:
-
Object
- Object
- Discorb::Flag
- Defined in:
- lib/discorb/flag.rb
Overview
This class is abstract.
Represents a flag.
Direct Known Subclasses
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}
readonly
The values of the flag.
Class Method Summary collapse
-
.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.
-
#method_missing(name, args = nil) -> Object
Returns the value of the flag.
- #respond_to_missing?(sym, include_private) -> Boolean
-
#|(other) -> Discorb::Flag
(also: #+)
Union of two flags.
-
#~@ -> Discorb::Flag
Negation of the flag.
Constructor Details
#initialize(value) -> Flag
Note:
This is usually called by the subclass.
Initialize the flag.
20 21 22 23 24 25 26 |
# 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, args = nil) -> Object
Returns the value of the flag.
31 32 33 34 35 36 37 |
# 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 |
Class Attribute Details
.bits -> Hash{Integer => Symbol} (readonly)
Returns the bits of the flag.
100 101 102 |
# File 'lib/discorb/flag.rb', line 100 def bits @bits end |
Instance Attribute Details
#value -> Integer (readonly)
Returns the value of the flag.
12 13 14 |
# File 'lib/discorb/flag.rb', line 12 def value @value end |
#values -> Hash{Symbol => Boolean} (readonly)
Returns the values of the flag.
10 11 12 |
# File 'lib/discorb/flag.rb', line 10 def values @values end |
Class Method Details
.max_value -> Integer
Max value of the flag.
107 108 109 |
# File 'lib/discorb/flag.rb', line 107 def max_value 2 ** @bits.values.max - 1 end |
Instance Method Details
#&(other) -> Discorb::Flag
Intersection of two flags.
74 75 76 |
# File 'lib/discorb/flag.rb', line 74 def &(other) self.class.new(@value & other.value) end |
#-(other) -> Discorb::Flag
Subtraction of two flags.
63 64 65 |
# File 'lib/discorb/flag.rb', line 63 def -(other) self.class.new(@value & (@value ^ other.value)) end |
#^(other) -> Discorb::Flag
XOR of two flags.
85 86 87 |
# File 'lib/discorb/flag.rb', line 85 def ^(other) self.class.new(@value ^ other.value) end |
#respond_to_missing?(sym, include_private) -> Boolean
39 40 41 |
# 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) -> Discorb::Flag Also known as: +
Union of two flags.
50 51 52 |
# File 'lib/discorb/flag.rb', line 50 def |(other) self.class.new(@value | other.value) end |
#~@ -> Discorb::Flag
Negation of the flag.
94 95 96 |
# File 'lib/discorb/flag.rb', line 94 def ~@ self.class.new(~@value) end |