daily pastebin goal
16%
SHARE
TWEET

Untitled

a guest Mar 20th, 2019 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Action
  2.   attr_reader :name, :payload, :callback
  3.  
  4.   def initialize(**args, &block)
  5.     @name = args[:name]
  6.     @payload = args[:payload] || {}
  7.     @callback = block || proc {}
  8.   end
  9.  
  10.   def process(payload)
  11.     @callback.call(@payload.merge!(payload))
  12.   end
  13. end
  14.  
  15. class EventBroker
  16.   def initialize
  17.     @events = {}
  18.   end
  19.  
  20.   def subscribe(actions = [])
  21.     actions.each do |action|
  22.       @events[action.name.to_sym] = action
  23.     end
  24.   end
  25.  
  26.   def raise(name, **args)
  27.     payload = args[:payload]
  28.  
  29.     @events[name.to_sym].process(payload)
  30.   end
  31. end
  32.  
  33. event_broker = EventBroker.new
  34.  
  35. actions = []
  36. actions << Action.new(name: "ActionOne", payload: { arg: 1 }) do |payload|
  37.   p payload
  38. end
  39.  
  40. event_broker.subscribe(actions)
  41. event_broker.raise("ActionOne", payload: { timeout: 100, retry: false })
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top