class Announcer::Instance
Represents an instance of the Announcer
. Allows multiple Instances to be created with separate configuration, subscriptions, etc. Primarily intended to help testing, but there are practical use-cases as well.
Attributes
name[R]
publishers[R]
Public Class Methods
load_from_serialized(name)
click to toggle source
# File lib/announcer/instance.rb, line 35 def self.load_from_serialized(name) if name Announcer.instance(name) else raise Errors::InstanceError, "Can't deserialize an unnamed Instance" end end
new(name=nil)
click to toggle source
# File lib/announcer/instance.rb, line 26 def initialize(name=nil) if name @name = name.to_sym Announcer._register_instance(self) if @name end _load_default_config end
Public Instance Methods
_register_subscription(subscription)
click to toggle source
# File lib/announcer/instance.rb, line 83 def _register_subscription(subscription) if _subscriptions_by_identifiers[subscription.identifier] # This is not expected to occur raise Errors::SubscriptionError, "duplicate identifier: #{subscription.identifier.inspect}" else _subscriptions_by_identifiers[subscription.identifier] = subscription end _registered_subscriptions_to(subscription.event_name) .push(subscription) .sort! { |x, y| x.priority <=> y.priority } end
config(&block)
click to toggle source
# File lib/announcer/instance.rb, line 43 def config(&block) (@__config ||= Config.new).tap { |config| if block_given? config.define(&block) _process_config end } end
find_publisher(publisher)
click to toggle source
# File lib/announcer/instance.rb, line 74 def find_publisher(publisher) klass = Publishers.load(publisher) publishers && publishers.find { |pub| pub.is_a?(klass) } end
find_subscription(identifier)
click to toggle source
# File lib/announcer/instance.rb, line 70 def find_subscription(identifier) _subscriptions_by_identifiers[identifier] end
has_publisher?(publisher)
click to toggle source
# File lib/announcer/instance.rb, line 79 def has_publisher?(publisher) !!find_publisher(publisher) end
plugin(*args)
click to toggle source
# File lib/announcer/instance.rb, line 52 def plugin(*args) config { plugin(*args) } end
publish(*args)
click to toggle source
# File lib/announcer/instance.rb, line 56 def publish(*args) raise Errors::NoPublishersDefinedError unless publishers && !publishers.empty? _args_to_event(*args).publish end
subscribe_to(event_name, params={}, &block)
click to toggle source
# File lib/announcer/instance.rb, line 61 def subscribe_to(event_name, params={}, &block) Subscription.new(event_name, params.merge(instance: self), &block) end
subscriptions_to(event_or_name)
click to toggle source
# File lib/announcer/instance.rb, line 65 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
Private Instance Methods
_args_to_event(name_or_event, params={})
click to toggle source
Attempt to convert *args to an event object.
# File lib/announcer/instance.rb, line 127 def _args_to_event(name_or_event, params={}) raise ArgumentError, 'event parameters must be a hash' unless params.is_a?(Hash) case name_or_event when Event name_or_event.tap { |e| e.instance_variable_set(:@instance, self) } else Event.new(name_or_event, params.merge(instance: self)) end end
_load_default_config()
click to toggle source
# File lib/announcer/instance.rb, line 122 def _load_default_config config.merge_config_file!(DEFAULT_CONFIG_PATH) end
_load_publishers()
click to toggle source
# File lib/announcer/instance.rb, line 143 def _load_publishers Publishers.load_for_instance(self) end
_process_config()
click to toggle source
# File lib/announcer/instance.rb, line 138 def _process_config @publishers = _load_publishers.dup.freeze _update_plugins end
_registered_subscriptions_to(event_name)
click to toggle source
# File lib/announcer/instance.rb, line 114 def _registered_subscriptions_to(event_name) (@__registered_subscriptions ||= {})[event_name] ||= [] end
_subscriptions_by_identifiers()
click to toggle source
# File lib/announcer/instance.rb, line 118 def _subscriptions_by_identifiers @__registered_subscriptions_by_identifier ||= {} end
_translate_object_to_plugin(object)
click to toggle source
# File lib/announcer/instance.rb, line 97 def _translate_object_to_plugin(object) case object when String, Symbol _translate_string_to_plugin(object.to_s) end end
_translate_string_to_plugin(string)
click to toggle source
# File lib/announcer/instance.rb, line 104 def _translate_string_to_plugin(string) begin Plugins.const_get( string.split('_').map(&:capitalize).join + 'Plugin' ) rescue nil # Let the Plugins gem handle this. end end
_update_plugins()
click to toggle source
# File lib/announcer/instance.rb, line 147 def _update_plugins plugins.clear if config.plugin? config.plugin.each { |plugin| plugin = [plugin] unless plugin.is_a?(Array) plugins.add(*plugin) } end end