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
- #inspect -> Object
-
#method_missing(method, bool = nil) -> Object
Returns the value of the flag.
- #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
Returns the value of the flag.
248 249 250 251 252 253 254 255 256 257 |
# File 'lib/discorb/permission.rb', line 248 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.
274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/discorb/permission.rb', line 274 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.
202 203 204 205 206 207 208 |
# File 'lib/discorb/permission.rb', line 202 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.
217 218 219 220 221 222 223 |
# File 'lib/discorb/permission.rb', line 217 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.
231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/discorb/permission.rb', line 231 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: +@
153 154 155 |
# File 'lib/discorb/permission.rb', line 153 def allow self.class.bits.keys.filter { |field| @allow & self.class.bits[field] != 0 } end |
#allow_value -> Object
165 166 167 |
# File 'lib/discorb/permission.rb', line 165 def allow_value @allow end |
#deny -> Object Also known as: -@
159 160 161 |
# File 'lib/discorb/permission.rb', line 159 def deny self.class.bits.keys.filter { |field| @deny & self.class.bits[field] != 0 } end |
#deny_value -> Object
169 170 171 |
# File 'lib/discorb/permission.rb', line 169 def deny_value @deny end |
#inspect -> Object
173 174 175 |
# File 'lib/discorb/permission.rb', line 173 def inspect "#<#{self.class} allow=#{allow} deny=#{deny}>" end |
#respond_to_missing?(method, _arg) -> Boolean
259 260 261 |
# File 'lib/discorb/permission.rb', line 259 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.
182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/discorb/permission.rb', line 182 def to_hash self.class.bits.keys.to_h do |field| [ field, if @allow & self.class.bits[field] != 0 true elsif @deny & self.class.bits[field] != 0 false end, ] end end |