Class: TokenOfFire::Subscriptions

Inherits:
Object
  • Object
show all
Defined in:
lib/token_of_fire/subscriptions.rb

Instance Method Summary collapse

Constructor Details

#initialize(subscriptions = {}) ⇒ Subscriptions

Returns a new instance of Subscriptions.



6
7
8
9
10
# File 'lib/token_of_fire/subscriptions.rb', line 6

def initialize(subscriptions={})
  @subscriptions = subscriptions

  @subscriptions_by_event_name = {}
end

Instance Method Details

#get_subscriptions(event_name = nil, scope = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/token_of_fire/subscriptions.rb', line 40

def get_subscriptions(event_name=nil, scope=nil)
  # $stdout.puts "---\nSearching for: #{event_name}"
  # $stdout.puts "  scope: #{scope}"
  uuids = @subscriptions_by_event_name[event_name]
  return [] if not uuids
  @subscriptions.select { |k| uuids.include? k }.select { |k, v|
    # $stdout.puts "  check match: #{v[:scope]}"
    s1 = Set.new(scope)
    s2 = Set.new(v[:scope])
    value = s1.subset? s2
    # $stdout.puts "    #{value}"
    value
  }
end

#subscribe(event_name, scope, handler, method_name) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/token_of_fire/subscriptions.rb', line 16

def subscribe(event_name, scope, handler, method_name)
  gen_uuid = SecureRandom.uuid
  @subscriptions[gen_uuid] ||= {}
  @subscriptions[gen_uuid] = {
    :event_name => event_name,
    :scope => scope,
    :handler => handler,
    :method_name => method_name
  }
  # $stdout.puts "subscription added: #{@subscriptions[gen_uuid]}"

  @subscriptions_by_event_name[event_name] ||= []
  @subscriptions_by_event_name[event_name] << gen_uuid

  gen_uuid
end

#subscriptionsObject



12
13
14
# File 'lib/token_of_fire/subscriptions.rb', line 12

def subscriptions
  @subscriptions
end

#unsubscribe(uuid) ⇒ Object



33
34
35
36
37
38
# File 'lib/token_of_fire/subscriptions.rb', line 33

def unsubscribe(uuid)
  @subscriptions.reject! { |k| k == uuid }
  @subscriptions_by_event_name.each do |s_by_event_name|
    s_by_event_name.reject! { |v| v == uuid }
  end
end