Class: Alondra::Command

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, command_hash) ⇒ Command

Returns a new instance of Command.



7
8
9
10
11
12
# File 'lib/alondra/command.rb', line 7

def initialize(connection, command_hash)
  @connection = connection

  @name         = command_hash[:command].to_sym
  @channel_name = command_hash[:channel]
end

Instance Attribute Details

#channel_nameObject (readonly)

Returns the value of attribute channel_name.



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

def channel_name
  @channel_name
end

#connectionObject (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#channelObject



14
15
16
# File 'lib/alondra/command.rb', line 14

def channel
  @channel ||= Channel[channel_name]
end

#execute!Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/alondra/command.rb', line 18

def execute!
  case name
  when :subscribe then
    channel.subscribe @connection
    fire_event :subscribed
  when :unsubscribe then
    channel.unsubscribe @connection
    fire_event :unsubscribed
  end
end

#fire_event(event_type) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/alondra/command.rb', line 29

def fire_event(event_type)
  event_hash = {
    :event         => event_type,
    :resource      => @connection.session,
    :resource_type => @connection.session.class.name,
    :channel       => @channel_name
  }

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