Class: Discorb::PermissionOverwrite
- Inherits:
-
Object
- Object
- Discorb::PermissionOverwrite
- Defined in:
- lib/discorb/permission.rb
Overview
Represents a permission per channel.
Class Method Summary collapse
-
.from_hash(hash) -> Discorb::PermissionOverwrite
Initializes a permission overwrite from a hash.
Instance Method Summary collapse
-
#+(other) -> Discorb::PermissionOverwrite
Union of the permission overwrites.
-
#[](field) -> true, ...
Returns whether overwrite of the given field.
-
#[]=(key, bool) -> Object
Sets the given field to the given value.
- #allow -> Object (also: #+@)
- #allow_value -> Object
- #deny -> Object (also: #-@)
- #deny_value -> Object
- #method_missing(method, bool = nil) -> Object
- #respond_to_missing?(method, _arg) -> Boolean
-
#to_hash -> Hash
Converts the permission overwrite to a hash.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, bool = nil) -> Object
232 233 234 235 236 237 238 239 240 241 |
# File 'lib/discorb/permission.rb', line 232 def method_missing(method, bool = nil) if self.class.bits.key?(method) self[method] elsif self.class.bits.key?(method.to_s.delete_suffix("=").to_sym) key = method.to_s.delete_suffix("=").to_sym self[key] = bool else super end end |
Class Method Details
.from_hash(hash) -> Discorb::PermissionOverwrite
Initializes a permission overwrite from a hash.
258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/discorb/permission.rb', line 258 def from_hash(hash) allow = 0 deny = 0 hash.filter { |k, v| self.class.bits.keys.include?(k) && [true, false].include?(v) }.each do |k, v| if v allow += self.class.bits[k] else deny += self.class.bits[k] end end new(allow, deny) end |
Instance Method Details
#+(other) -> Discorb::PermissionOverwrite
Union of the permission overwrites.
189 190 191 192 193 194 195 |
# File 'lib/discorb/permission.rb', line 189 def +(other) result = to_hash self.class.bits.each_key do |field| result[field] = other[field] unless other[field].nil? end self.class.from_hash(result) end |
#[](field) -> true, ...
Returns whether overwrite of the given field.
204 205 206 207 208 209 210 |
# File 'lib/discorb/permission.rb', line 204 def [](field) if @allow & self.class.bits[field] != 0 true elsif @deny & self.class.bits[field] != 0 false end end |
#[]=(key, bool) -> Object
Sets the given field to the given value.
218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/discorb/permission.rb', line 218 def []=(key, bool) case bool when true @allow |= self.class.bits[key] @deny &= ~self.class.bits[key] when false @allow &= ~self.class.bits[key] @deny |= self.class.bits[key] else @allow &= ~self.class.bits[key] @deny &= ~self.class.bits[key] end end |
#allow -> Object Also known as: +@
147 148 149 |
# File 'lib/discorb/permission.rb', line 147 def allow self.class.bits.keys.filter { |field| @allow & self.class.bits[field] != 0 } end |
#allow_value -> Object
159 160 161 |
# File 'lib/discorb/permission.rb', line 159 def allow_value @allow end |
#deny -> Object Also known as: -@
153 154 155 |
# File 'lib/discorb/permission.rb', line 153 def deny self.class.bits.keys.filter { |field| @deny & self.class.bits[field] != 0 } end |
#deny_value -> Object
163 164 165 |
# File 'lib/discorb/permission.rb', line 163 def deny_value @deny end |
#respond_to_missing?(method, _arg) -> Boolean
243 244 245 |
# File 'lib/discorb/permission.rb', line 243 def respond_to_missing?(method, _arg) self.class.bits.key?(method.to_s.delete_suffix("=").to_sym) ? true : super end |
#to_hash -> Hash
Converts the permission overwrite to a hash.
172 173 174 175 176 177 178 179 180 |
# File 'lib/discorb/permission.rb', line 172 def to_hash self.class.bits.keys.map do |field| [field, if @allow & self.class.bits[field] != 0 true elsif @deny & self.class.bits[method] != 0 false end] end.to_h end |