Class: Ribbon::EventBus::Subscription

Inherits:
Object
  • Object
show all
Includes:
Mixins::HasConfig, Mixins::HasInstance, Mixins::Serializable
Defined in:
lib/ribbon/event_bus/subscription.rb

Constant Summary

Constants included from Mixins::Serializable

Mixins::Serializable::MAGIC, Mixins::Serializable::VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Serializable

#_serializable_args, #_serializable_package, #_serialize_arg, included, #serialize

Methods included from Mixins::HasConfig

#_has_config_config, #_has_config_load_config, #config, included

Methods included from Mixins::HasInstance

#instance, #plugins

Constructor Details

#initialize(event_name, params = {}, &block) ⇒ Subscription

Returns a new instance of Subscription.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ribbon/event_bus/subscription.rb', line 17

def initialize(event_name, params={}, &block)
  @event_name = event_name.to_sym
  @_block = block

  _evaluate_params(params)

  @name ||= _path
  @locator = _generate_locator

  instance._register_subscription(self)
end

Instance Attribute Details

#event_nameObject (readonly)

Returns the value of attribute event_name.



13
14
15
# File 'lib/ribbon/event_bus/subscription.rb', line 13

def event_name
  @event_name
end

#locatorObject (readonly)

Returns the value of attribute locator.



15
16
17
# File 'lib/ribbon/event_bus/subscription.rb', line 15

def locator
  @locator
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/ribbon/event_bus/subscription.rb', line 12

def name
  @name
end

#priorityObject (readonly)

Returns the value of attribute priority.



14
15
16
# File 'lib/ribbon/event_bus/subscription.rb', line 14

def priority
  @priority
end

Class Method Details

.load_from_serialized(instance, locator) ⇒ Object



29
30
31
# File 'lib/ribbon/event_bus/subscription.rb', line 29

def self.load_from_serialized(instance, locator)
  instance.find_subscription(locator)
end

Instance Method Details

#handle(event) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/ribbon/event_bus/subscription.rb', line 33

def handle(event)
  raise Errors::UnexpectedEventError, 'wrong name' unless event.name == event_name
  raise Errors::UnexpectedEventError, 'wrong instance' unless event.instance == instance

  plugins.perform(:subscription, self, event) { |subscription, event|
    @_block.call(event)
  }
end

#to_sObject



42
43
44
# File 'lib/ribbon/event_bus/subscription.rb', line 42

def to_s
  "Subscription(#{event_name}, #{name})"
end