-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.rb
38 lines (35 loc) · 1.08 KB
/
event.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module Shark
class Event
# The topic this event is being fired on
attr_accessor :topic
# The type of event being represented
attr_accessor :type
# The positional arguments for this event
attr_accessor :args
# The keyword arguments for this event
attr_accessor :kwargs
# The object that caused the creation of this event
attr_accessor :originator
def initialize topic:, type:, args:[], kwargs:{}, originator:
@topic = topic
@type = type
@args = args
@kwargs = kwargs
@originator = originator
end
# Create a Hash representation of this event, attempting to convert all
# `args` and `kwargs` to hash representations as well.
def to_h
{
topic: topic,
type: type,
args: args.map{ |arg| arg.respond_to?(:to_h) ? arg.to_h(nested: false) : arg },
kwargs: Hash[kwargs.map{|k, v| [k, v.respond_to?(:to_h) ? v.to_h(nested: false) : v] }],
originator: originator
}
end
def to_json
to_h.to_json
end
end
end