class Startback::Event

An Event occuring a given context and having a type and attached data.

Event instances have String types that are by default unrelated to ruby classes. Also, this Event class has a `json` information contract that allows dumping & reloading them easily. A context or context_factory may be provided in dress world to reload the event context from data, but that logic is opaque to this class.

This class is intended to be subclassed if a more specific event protocol is wanted.

Attributes

context[R]
data[R]
type[R]

Public Class Methods

json(src, world = {}) click to toggle source
# File lib/startback/event.rb, line 23
def self.json(src, world = {})
  parsed = JSON.parse(src)
  context = if world[:context]
    world[:context]
  elsif world[:context_factory]
    world[:context_factory].call(parsed)
  end
  Event.new(parsed['type'], parsed['data'], context)
end
new(type, data, context = nil) click to toggle source
# File lib/startback/event.rb, line 16
def initialize(type, data, context = nil)
  @type = type.to_s
  @data = OpenStruct.new(data)
  @context = context
end

Public Instance Methods

to_json(*args, &bl) click to toggle source
# File lib/startback/event.rb, line 33
def to_json(*args, &bl)
  h = {
    type: self.type,
    data: data.to_h
  }
  h[:context] = context if context
  h.to_json(*args, &bl)
end