Class: Ribbon::EventBus::Instance
- Inherits:
-
Object
- Object
- Ribbon::EventBus::Instance
show all
- Includes:
- Mixins::Serializable, Plugins::ComponentMixin
- Defined in:
- lib/ribbon/event_bus/instance.rb
Overview
Instance
Represents an instance of the EventBus. Allows multiple Instances to be created with separate configuration, subscriptions, etc. Primarily intended to help testing, but there are practical use-cases as well.
Constant Summary
Mixins::Serializable::MAGIC, Mixins::Serializable::VERSION
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#_serializable_args, #_serializable_package, #_serialize_arg, included, #serialize
Constructor Details
#initialize(name = nil) ⇒ Instance
Returns a new instance of Instance.
27
28
29
30
31
32
33
34
|
# File 'lib/ribbon/event_bus/instance.rb', line 27
def initialize(name=nil)
if name
@name = name.to_sym
EventBus._register_instance(self) if @name
end
_load_default_config
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
24
25
26
|
# File 'lib/ribbon/event_bus/instance.rb', line 24
def name
@name
end
|
#publishers ⇒ Object
Returns the value of attribute publishers.
25
26
27
|
# File 'lib/ribbon/event_bus/instance.rb', line 25
def publishers
@publishers
end
|
Class Method Details
.load_from_serialized(name) ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/ribbon/event_bus/instance.rb', line 36
def self.load_from_serialized(name)
if name
EventBus.instance(name)
else
raise Errors::InstanceError, "Can't deserialize an unnamed Instance"
end
end
|
Instance Method Details
#_register_subscription(subscription) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/ribbon/event_bus/instance.rb', line 84
def _register_subscription(subscription)
if _subscriptions_by_locators[subscription.locator]
raise Errors::SubscriptionError, "duplicate locator: #{subscription.locator.inspect}"
else
_subscriptions_by_locators[subscription.locator] = subscription
end
_registered_subscriptions_to(subscription.event_name)
.push(subscription)
.sort! { |x, y| x.priority <=> y.priority }
end
|
#config(&block) ⇒ Object
44
45
46
47
48
49
50
51
|
# File 'lib/ribbon/event_bus/instance.rb', line 44
def config(&block)
(@__config ||= Config.new).tap { |config|
if block_given?
config.define(&block)
_process_config
end
}
end
|
#find_publisher(publisher) ⇒ Object
75
76
77
78
|
# File 'lib/ribbon/event_bus/instance.rb', line 75
def find_publisher(publisher)
klass = Publishers.load(publisher)
publishers && publishers.find { |pub| pub.is_a?(klass) }
end
|
#find_subscription(locator) ⇒ Object
71
72
73
|
# File 'lib/ribbon/event_bus/instance.rb', line 71
def find_subscription(locator)
_subscriptions_by_locators[locator]
end
|
#has_publisher?(publisher) ⇒ Boolean
80
81
82
|
# File 'lib/ribbon/event_bus/instance.rb', line 80
def has_publisher?(publisher)
!!find_publisher(publisher)
end
|
#plugin(*args) ⇒ Object
53
54
55
|
# File 'lib/ribbon/event_bus/instance.rb', line 53
def plugin(*args)
config { plugin(*args) }
end
|
#publish(*args) ⇒ Object
57
58
59
60
|
# File 'lib/ribbon/event_bus/instance.rb', line 57
def publish(*args)
raise Errors::NoPublishersDefinedError unless publishers && !publishers.empty?
_args_to_event(*args).publish
end
|
#subscribe_to(event_name, params = {}, &block) ⇒ Object
62
63
64
|
# File 'lib/ribbon/event_bus/instance.rb', line 62
def subscribe_to(event_name, params={}, &block)
Subscription.new(event_name, params.merge(instance: self), &block)
end
|
#subscriptions_to(event_or_name) ⇒ Object
66
67
68
69
|
# File 'lib/ribbon/event_bus/instance.rb', line 66
def subscriptions_to(event_or_name)
event_name = event_or_name.is_a?(Event) ? event_or_name.name : event_or_name.to_sym
_registered_subscriptions_to(event_name).dup
end
|