Class: Alondra::Event

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_hash, from_json = nil, connection = nil) ⇒ Event

Returns a new instance of Event.



9
10
11
12
13
14
15
16
# File 'lib/alondra/event.rb', line 9

def initialize(event_hash, from_json = nil, connection = nil)
  @connection   = connection
  @type         = event_hash[:event].to_sym
  @json_encoded = from_json

  set_resource_from(event_hash)
  set_channel_from(event_hash)
end

Instance Attribute Details

#channel_nameObject (readonly)

Returns the value of attribute channel_name.



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

def channel_name
  @channel_name
end

#connectionObject (readonly)

Returns the value of attribute connection.



7
8
9
# File 'lib/alondra/event.rb', line 7

def connection
  @connection
end

#resourceObject (readonly)

Returns the value of attribute resource.



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

def resource
  @resource
end

#resource_typeObject (readonly)

Returns the value of attribute resource_type.



6
7
8
# File 'lib/alondra/event.rb', line 6

def resource_type
  @resource_type
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#as_jsonObject



33
34
35
36
37
38
39
40
# File 'lib/alondra/event.rb', line 33

def as_json
  {
    :event         => type,
    :resource_type => resource_type,
    :resource      => resource.as_json,
    :channel       => channel_name
  }
end

#channelObject



18
19
20
# File 'lib/alondra/event.rb', line 18

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

#fire!Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/alondra/event.rb', line 22

def fire!
  if connection
    # We are inside the Alondra Server
    EM.schedule do
      MessageQueue.instance.receive self
    end
  else
    MessageQueueClient.push self
  end
end

#to_jsonObject



42
43
44
# File 'lib/alondra/event.rb', line 42

def to_json
  @json_encoded ||= ActiveSupport::JSON.encode(as_json)
end