Class: Redis::KeyspaceNotifications::Notification
- Inherits:
-
Object
- Object
- Redis::KeyspaceNotifications::Notification
- Defined in:
- lib/redis/keyspace_notifications/notification.rb
Overview
A parsed keyspace, keyevent or subkey notification.
Immutable value object produced by Parser.parse. key and the subkeys
elements are BINARY (ASCII-8BIT) encoded strings, because Redis keys and
subkeys may contain arbitrary bytes; call force_encoding on them if your
application guarantees UTF-8 keys.
Constant Summary collapse
- FAMILIES =
%i[keyspace keyevent subkeyspace subkeyevent subkeyspaceitem subkeyspaceevent].freeze
Instance Attribute Summary collapse
- #channel ⇒ Symbol, ... readonly
- #db ⇒ Symbol, ... readonly
- #event ⇒ Symbol, ... readonly
- #family ⇒ Symbol, ... readonly
- #key ⇒ Symbol, ... readonly
- #pattern ⇒ Symbol, ... readonly
- #payload ⇒ Symbol, ... readonly
- #subkeys ⇒ Symbol, ... readonly
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Whether
otheris a Notification with equal fields. -
#hash ⇒ Integer
A hash derived from all fields.
-
#initialize(family:, db:, event:, key:, channel:, payload:, subkeys: [], pattern: nil) ⇒ Notification
constructor
A new instance of Notification.
-
#subkey ⇒ String?
Convenience accessor for single-subkey families (subkeyspaceitem).
-
#subkey_family? ⇒ Boolean
Whether this notification belongs to one of the Redis 8.8 subkey channel families (and therefore may carry subkeys).
-
#to_h ⇒ Hash{Symbol => Object}
All fields as a hash.
Constructor Details
#initialize(family:, db:, event:, key:, channel:, payload:, subkeys: [], pattern: nil) ⇒ Notification
Returns a new instance of Notification.
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/redis/keyspace_notifications/notification.rb', line 35 def initialize(family:, db:, event:, key:, channel:, payload:, subkeys: [], pattern: nil) @family = family @db = frozen_copy(db) # Integers pass through; the "*" String form gets copied @event = frozen_copy(event) @key = frozen_copy(key) @subkeys = subkeys.map { |subkey| frozen_copy(subkey) }.freeze @channel = frozen_copy(channel) @payload = frozen_copy(payload) @pattern = frozen_copy(pattern) # Deep-frozen: the value object implements #hash / #eql?, so no field may be # mutable after construction (a mutated Hash key becomes unreachable). freeze end |
Instance Attribute Details
#channel ⇒ Symbol, ... (readonly)
25 26 27 |
# File 'lib/redis/keyspace_notifications/notification.rb', line 25 def channel @channel end |
#db ⇒ Symbol, ... (readonly)
25 26 27 |
# File 'lib/redis/keyspace_notifications/notification.rb', line 25 def db @db end |
#event ⇒ Symbol, ... (readonly)
25 26 27 |
# File 'lib/redis/keyspace_notifications/notification.rb', line 25 def event @event end |
#family ⇒ Symbol, ... (readonly)
25 26 27 |
# File 'lib/redis/keyspace_notifications/notification.rb', line 25 def family @family end |
#key ⇒ Symbol, ... (readonly)
25 26 27 |
# File 'lib/redis/keyspace_notifications/notification.rb', line 25 def key @key end |
#pattern ⇒ Symbol, ... (readonly)
25 26 27 |
# File 'lib/redis/keyspace_notifications/notification.rb', line 25 def pattern @pattern end |
#payload ⇒ Symbol, ... (readonly)
25 26 27 |
# File 'lib/redis/keyspace_notifications/notification.rb', line 25 def payload @payload end |
#subkeys ⇒ Symbol, ... (readonly)
25 26 27 |
# File 'lib/redis/keyspace_notifications/notification.rb', line 25 def subkeys @subkeys end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Returns whether other is a Notification with equal fields.
78 79 80 |
# File 'lib/redis/keyspace_notifications/notification.rb', line 78 def ==(other) other.is_a?(Notification) && to_h == other.to_h end |
#hash ⇒ Integer
Returns a hash derived from all fields.
84 85 86 |
# File 'lib/redis/keyspace_notifications/notification.rb', line 84 def hash to_h.hash end |
#subkey ⇒ String?
Convenience accessor for single-subkey families (subkeyspaceitem).
52 53 54 |
# File 'lib/redis/keyspace_notifications/notification.rb', line 52 def subkey @subkeys.first end |
#subkey_family? ⇒ Boolean
Returns whether this notification belongs to one of the Redis 8.8 subkey channel families (and therefore may carry subkeys).
58 59 60 |
# File 'lib/redis/keyspace_notifications/notification.rb', line 58 def subkey_family? @family != :keyspace && @family != :keyevent end |
#to_h ⇒ Hash{Symbol => Object}
Returns all fields as a hash.
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/redis/keyspace_notifications/notification.rb', line 63 def to_h { family: @family, db: @db, event: @event, key: @key, subkeys: @subkeys, channel: @channel, payload: @payload, pattern: @pattern } end |