Class: QueueBus::SubscriptionList
- Inherits:
-
Object
- Object
- QueueBus::SubscriptionList
- Defined in:
- lib/queue_bus/subscription_list.rb
Overview
Manages a set of subscriptions.
The subscriptions are stored in redis but not by this class. Instead this class uses two functions ‘to_redis` and `from_redis` to facilitate serialization without accessing redis directly.
To create a new SubscriptionList, use the static function ‘from_redis` and pass it a hash that came from redis.
To get a value fro redis, take your loaded SubscriptionList and call ‘to_redis` on it. The returned value can be used to store in redis.
Class Method Summary collapse
Instance Method Summary collapse
- #add(sub) ⇒ Object
- #all ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ SubscriptionList
constructor
A new instance of SubscriptionList.
- #key(key) ⇒ Object
- #matches(attributes) ⇒ Object
- #size ⇒ Object
- #to_redis ⇒ Object
Constructor Details
#initialize ⇒ SubscriptionList
Returns a new instance of SubscriptionList.
37 38 39 |
# File 'lib/queue_bus/subscription_list.rb', line 37 def initialize @subscriptions = {} end |
Class Method Details
.from_redis(redis_hash) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/queue_bus/subscription_list.rb', line 17 def from_redis(redis_hash) out = SubscriptionList.new redis_hash.each do |_key, value| sub = Subscription.from_redis(value) out.add(sub) if sub end out end |
Instance Method Details
#add(sub) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/queue_bus/subscription_list.rb', line 41 def add(sub) if @subscriptions.key?(sub.key) raise "Duplicate key: #{sub.key} already exists " \ "in the #{sub.queue_name} queue!" end @subscriptions[sub.key] = sub end |
#all ⇒ Object
61 62 63 |
# File 'lib/queue_bus/subscription_list.rb', line 61 def all @subscriptions.values end |
#empty? ⇒ Boolean
53 54 55 |
# File 'lib/queue_bus/subscription_list.rb', line 53 def empty? size.zero? end |
#key(key) ⇒ Object
57 58 59 |
# File 'lib/queue_bus/subscription_list.rb', line 57 def key(key) @subscriptions[key.to_s] end |
#matches(attributes) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/queue_bus/subscription_list.rb', line 65 def matches(attributes) out = [] all.each do |sub| out << sub if sub.matches?(attributes) end out end |
#size ⇒ Object
49 50 51 |
# File 'lib/queue_bus/subscription_list.rb', line 49 def size @subscriptions.size end |
#to_redis ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/queue_bus/subscription_list.rb', line 29 def to_redis out = {} @subscriptions.values.each do |sub| out[sub.key] = sub.to_redis end out end |