Class: RubyCord::Flag Abstract
- Inherits:
-
Object
- Object
- RubyCord::Flag
- Defined in:
- lib/rubycord/flag.rb
Overview
Represents a flag.
Direct Known Subclasses
Application::Flag, Guild::SystemChannelFlag, Message::Flag, Permission, User::Activity::Flag, 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) ⇒ RubyCord::Flag
Intersection of two flags.
-
#-(other) ⇒ RubyCord::Flag
Subtraction of two flags.
-
#^(other) ⇒ RubyCord::Flag
XOR of two flags.
-
#initialize(value) ⇒ Flag
constructor
Initialize the flag.
-
#inspect ⇒ String
Object class and attributes.
-
#method_missing(name, *_args, **_kwargs) ⇒ Object
Returns the value of the flag.
- #respond_to_missing?(sym, include_private) ⇒ Boolean
- #to_i ⇒ Object
-
#|(other) ⇒ RubyCord::Flag
(also: #+)
Union of two flags.
-
#~ ⇒ RubyCord::Flag
Negation of the flag.
Constructor Details
#initialize(value) ⇒ Flag
This is usually called by the subclass.
Initialize the flag.
22 23 24 25 26 |
# File 'lib/rubycord/flag.rb', line 22 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.
31 32 33 34 35 36 37 |
# File 'lib/rubycord/flag.rb', line 31 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/rubycord/flag.rb', line 109 def bits @bits end |
Instance Attribute Details
#value ⇒ Integer (readonly)
Returns the value of the flag.
14 15 16 |
# File 'lib/rubycord/flag.rb', line 14 def value @value end |
#values ⇒ Hash{Symbol => Boolean} (readonly) Also known as: to_h
Returns the values of the flag.
11 12 13 |
# File 'lib/rubycord/flag.rb', line 11 def values @values end |
Class Method Details
.from_keys(*keys) ⇒ Object
Initialize a new flag with keys.
123 124 125 |
# File 'lib/rubycord/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/rubycord/flag.rb', line 116 def max_value (2**@bits.values.max) - 1 end |
Instance Method Details
#&(other) ⇒ RubyCord::Flag
Intersection of two flags.
74 75 76 |
# File 'lib/rubycord/flag.rb', line 74 def &(other) self.class.new(@value & other.value) end |
#-(other) ⇒ RubyCord::Flag
Subtraction of two flags.
63 64 65 |
# File 'lib/rubycord/flag.rb', line 63 def -(other) self.class.new(@value & (@value ^ other.value)) end |
#^(other) ⇒ RubyCord::Flag
XOR of two flags.
85 86 87 |
# File 'lib/rubycord/flag.rb', line 85 def ^(other) self.class.new(@value ^ other.value) end |
#inspect ⇒ String
Returns Object class and attributes.
103 104 105 |
# File 'lib/rubycord/flag.rb', line 103 def inspect "#<#{self.class}: #{@value}>" end |
#respond_to_missing?(sym, include_private) ⇒ Boolean
39 40 41 |
# File 'lib/rubycord/flag.rb', line 39 def respond_to_missing?(sym, include_private) @values.key?(name.to_s.delete_suffix("?").to_sym) ? true : super end |
#to_i ⇒ Object
98 99 100 |
# File 'lib/rubycord/flag.rb', line 98 def to_i @value end |
#|(other) ⇒ RubyCord::Flag Also known as: +
Union of two flags.
50 51 52 |
# File 'lib/rubycord/flag.rb', line 50 def |(other) self.class.new(@value | other.value) end |
#~ ⇒ RubyCord::Flag
Negation of the flag.
94 95 96 |
# File 'lib/rubycord/flag.rb', line 94 def ~ self.class.new(~@value) end |