Class: Bustle::Subscriptions

Inherits:
Object
  • Object
show all
Includes:
Concern::ByPublisher, Concern::Filterable, Concern::ForSubscriber
Defined in:
lib/bustle/subscriptions.rb

Constant Summary collapse

RESOURCE_NAME =
'Bustle::Subscription'

Class Method Summary collapse

Class Method Details

.add(publisher, subscriber) ⇒ Object



17
18
19
20
21
# File 'lib/bustle/subscriptions.rb', line 17

def add(publisher, subscriber)
  add!(publisher, subscriber)
rescue
  get(publisher, subscriber)
end

.add!(publisher, subscriber) ⇒ Object



10
11
12
13
14
15
# File 'lib/bustle/subscriptions.rb', line 10

def add!(publisher, subscriber)
  Subscription.to_adapter.create!(
    :publisher_id  => publisher.id,
    :subscriber_id => subscriber.id
  )
end

.get(publisher, subscriber) ⇒ Object



23
24
25
26
27
28
# File 'lib/bustle/subscriptions.rb', line 23

def get(publisher, subscriber)
  Subscription.to_adapter.find_first(
    :publisher_id  => publisher.id,
    :subscriber_id => subscriber.id
  )
end

.remove(publisher, subscriber) ⇒ Object



39
40
41
42
# File 'lib/bustle/subscriptions.rb', line 39

def remove(publisher, subscriber)
  subscription = get(publisher, subscriber)
  subscription.destroy unless subscription.nil?
end

.remove!(publisher, subscriber) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/bustle/subscriptions.rb', line 30

def remove!(publisher, subscriber)
  subscription = get(publisher, subscriber)
  if subscription.nil?
    raise "Subscription between #{publisher} and #{subscriber} does not exist therefore cannot be removed."
  else
    subscription.destroy
  end
end