-
Notifications
You must be signed in to change notification settings - Fork 2
/
omnes.rb
51 lines (47 loc) · 1.2 KB
/
omnes.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
39
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true
require "omnes/bus"
require "omnes/configurable"
require "omnes/event"
require "omnes/subscriber"
require "omnes/version"
# Pub/sub library for Ruby.
#
# There're two ways to make use of the pub/sub features Omnes provides:
#
# - Standalone, through an {Omnes::Bus} instance.
# - Mixing in the behavior in another class by including the {Omnes} module.
#
# Refer to {Omnes::Bus} documentation for the available methods. The only
# difference for the mixing use case is that the methods are directly called in
# the including instance.
#
# ```
# class MyClass
# include Omnes
#
# def initialize
# register(:foo)
# end
#
# def call
# publish(:foo, bar: :baz)
# end
# end
# ```
#
# Refer to {Omnes::Subscriber} for how to provide event handlers through methods
# defined in a class.
module Omnes
extend Configurable
nest_config Subscriber
nest_config Event
# @api private
def self.included(klass)
klass.define_method(:omnes_bus) { @omnes_bus ||= Bus.new(cal_loc_start: 2) }
Bus.instance_methods(false).each do |method|
klass.define_method(method) do |*args, **kwargs, &block|
omnes_bus.send(method, *args, **kwargs, &block)
end
end
end
end