Class: Zulu::Topic
- Inherits:
-
Object
- Object
- Zulu::Topic
- Defined in:
- lib/zulu/topic.rb
Constant Summary collapse
- KEY_PREFIX =
"topic".freeze
- UPCOMING_KEY =
"#{KEY_PREFIX}:upcoming".freeze
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #destroy ⇒ Object
- #id ⇒ Object
-
#initialize(options = {}) ⇒ Topic
constructor
A new instance of Topic.
- #next_time(now = Time.now) ⇒ Object
- #parser ⇒ Object
- #reset_next(now = Time.now) ⇒ Object
- #save ⇒ Object
- #subscribe(subscription_id) ⇒ Object
- #subscriptions ⇒ Object
- #subscriptions_count ⇒ Object
- #topic ⇒ Object
- #unsubscribe(subscription_id) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Topic
Returns a new instance of Topic.
14 15 16 17 |
# File 'lib/zulu/topic.rb', line 14 def initialize(={}) @id = [:id] @topic = [:topic] end |
Class Method Details
.happening(now = Time.now) ⇒ Object
10 11 12 |
# File 'lib/zulu/topic.rb', line 10 def self.happening(now=Time.now) Zulu.redis.zrangebyscore(UPCOMING_KEY, 0, now.to_i) end |
Instance Method Details
#==(other) ⇒ Object
19 20 21 |
# File 'lib/zulu/topic.rb', line 19 def ==(other) id == other.id end |
#destroy ⇒ Object
67 68 69 70 71 72 |
# File 'lib/zulu/topic.rb', line 67 def destroy Zulu.redis.multi do Zulu.redis.del "#{KEY_PREFIX}:#{id}" Zulu.redis.zrem(UPCOMING_KEY, id) end end |
#id ⇒ Object
23 24 25 |
# File 'lib/zulu/topic.rb', line 23 def id @id ||= Digest::MD5.hexdigest @topic end |
#next_time(now = Time.now) ⇒ Object
52 53 54 |
# File 'lib/zulu/topic.rb', line 52 def next_time(now=Time.now) parser.next_time(now) end |
#parser ⇒ Object
48 49 50 |
# File 'lib/zulu/topic.rb', line 48 def parser @parser ||= Rufus::CronLine.new(topic) end |
#reset_next(now = Time.now) ⇒ Object
56 57 58 |
# File 'lib/zulu/topic.rb', line 56 def reset_next(now=Time.now) Zulu.redis.zadd(UPCOMING_KEY, next_time(now).to_i, id) end |
#save ⇒ Object
60 61 62 63 64 65 |
# File 'lib/zulu/topic.rb', line 60 def save Zulu.redis.multi do Zulu.redis.set "#{KEY_PREFIX}:#{id}", topic reset_next end end |
#subscribe(subscription_id) ⇒ Object
39 40 41 |
# File 'lib/zulu/topic.rb', line 39 def subscribe(subscription_id) Zulu.redis.sadd("#{KEY_PREFIX}:#{id}:subscriptions", subscription_id) end |
#subscriptions ⇒ Object
35 36 37 |
# File 'lib/zulu/topic.rb', line 35 def subscriptions Zulu.redis.smembers("#{KEY_PREFIX}:#{id}:subscriptions") end |
#subscriptions_count ⇒ Object
31 32 33 |
# File 'lib/zulu/topic.rb', line 31 def subscriptions_count Zulu.redis.scard("#{KEY_PREFIX}:#{id}:subscriptions") end |
#topic ⇒ Object
27 28 29 |
# File 'lib/zulu/topic.rb', line 27 def topic @topic ||= Zulu.redis.get "#{KEY_PREFIX}:#{id}" end |
#unsubscribe(subscription_id) ⇒ Object
43 44 45 46 |
# File 'lib/zulu/topic.rb', line 43 def unsubscribe(subscription_id) Zulu.redis.srem("#{KEY_PREFIX}:#{id}:subscriptions", subscription_id) destroy if subscriptions_count == 0 end |