Class: EventHub::Event

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/event_hub/event.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Event

Returns a new instance of Event.



10
11
12
13
14
# File 'lib/event_hub/event.rb', line 10

def initialize(hash = {})
  hash.transform_keys(&:to_s).slice(*self.class.attributes.keys).each do |attr, val|
    public_send("#{attr}=", val)
  end
end

Class Attribute Details

.attributesObject (readonly)

Returns the value of attribute attributes.



39
40
41
# File 'lib/event_hub/event.rb', line 39

def attributes
  @attributes
end

Class Method Details

.attribute(attribute, options = {}) ⇒ Object



41
42
43
44
45
46
# File 'lib/event_hub/event.rb', line 41

def attribute(attribute, options = {})
  @attributes ||= {}
  @attributes[attribute.to_s] = options

  attr_accessor(attribute)
end

.event(event = nil) ⇒ Object



28
29
30
31
# File 'lib/event_hub/event.rb', line 28

def self.event(event = nil)
  @event = event.to_s if event
  @event
end

.version(version = nil) ⇒ Object



33
34
35
36
# File 'lib/event_hub/event.rb', line 33

def self.version(version = nil)
  @version = version if version
  @version
end

Instance Method Details

#bodyObject



22
23
24
25
26
# File 'lib/event_hub/event.rb', line 22

def body
  self.class.attributes.keys.to_h do |attr|
    [attr, public_send(attr)]
  end.to_json
end

#publishObject



16
17
18
19
20
# File 'lib/event_hub/event.rb', line 16

def publish
  run_callbacks :publish do
    EventHub.publish(self)
  end
end