Class: Alondra::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/alondra/channel.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Channel

Returns a new instance of Channel.



49
50
51
52
53
# File 'lib/alondra/channel.rb', line 49

def initialize(name)
  @name = name
  @em_channel = EM::Channel.new
  @connections = {}
end

Instance Attribute Details

#connectionsObject (readonly)

Returns the value of attribute connections.



5
6
7
# File 'lib/alondra/channel.rb', line 5

def connections
  @connections
end

#em_channelObject (readonly)

Returns the value of attribute em_channel.



4
5
6
# File 'lib/alondra/channel.rb', line 4

def em_channel
  @em_channel
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/alondra/channel.rb', line 3

def name
  @name
end

Class Method Details

.[](name) ⇒ Object



12
13
14
# File 'lib/alondra/channel.rb', line 12

def [](name)
  list[name] ||= Channel.new(name)
end

.default_name_for(resource_or_class, type = :member) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/alondra/channel.rb', line 31

def default_name_for(resource_or_class, type = :member)

  if resource_or_class.kind_of?(Class)
    resource_name = resource_or_class.name.pluralize.underscore
  else
    resource      = resource_or_class
    resource_name = resource.class.name.pluralize.underscore
  end

  case type
  when :member then
    "/#{resource_name}/#{resource.id}"
  when :collection then
    "/#{resource_name}/"
  end
end

.for(records) ⇒ Object



16
17
18
# File 'lib/alondra/channel.rb', line 16

def for(records)
  names_for(records).collect { |name| Channel[name] }
end

.listObject



8
9
10
# File 'lib/alondra/channel.rb', line 8

def list
  @channel_list ||= {}
end

.names_for(records) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/alondra/channel.rb', line 20

def names_for(records)
  case records
  when String
    [records]
  when Enumerable then
    records.collect { |r| Channel.default_name_for(r) }
  else
    [Channel.default_name_for(records)]
  end
end

Instance Method Details

#receive(event_or_message) ⇒ Object



80
81
82
# File 'lib/alondra/channel.rb', line 80

def receive(event_or_message)
  em_channel << event_or_message
end

#subscribe(connection) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/alondra/channel.rb', line 55

def subscribe(connection)
  sid = em_channel.subscribe do |event_or_message|
    connection.receive event_or_message
  end

  connection.channels << self
  connections[connection] = sid
end

#unsubscribe(connection) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/alondra/channel.rb', line 64

def unsubscribe(connection)
  em_channel.unsubscribe connections[connection]

  connection.channels.delete self
  connections.delete connection

  event_hash =  { :event    => :unsubscribed,
                  :resource => connection.session,
                  :resource_type => connection.session.class.name,
                  :channel  => name }


  event = Event.new(event_hash, nil, connection)
  event.fire!
end