Class: BasicSubscriber::Subscriber
- Inherits:
-
Object
- Object
- BasicSubscriber::Subscriber
- Defined in:
- lib/basic_subscriber/subscriber.rb
Constant Summary collapse
- NAME_SEPARATOR =
'.'
Instance Attribute Summary collapse
-
#event_name ⇒ Object
readonly
Returns the value of attribute event_name.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
Class Method Summary collapse
- .known_events ⇒ Object
- .on(*event_names, &block) ⇒ Object
- .scope(scope_name) ⇒ Object
- .trigger(event_name, **payload) ⇒ Object
Instance Method Summary collapse
-
#initialize(event_name, **payload) ⇒ Subscriber
constructor
A new instance of Subscriber.
- #scope_name ⇒ Object
- #unscoped_event_name ⇒ Object
Constructor Details
#initialize(event_name, **payload) ⇒ Subscriber
Returns a new instance of Subscriber.
59 60 61 62 |
# File 'lib/basic_subscriber/subscriber.rb', line 59 def initialize(event_name, **payload) @event_name = event_name @payload = payload end |
Instance Attribute Details
#event_name ⇒ Object (readonly)
Returns the value of attribute event_name.
5 6 7 |
# File 'lib/basic_subscriber/subscriber.rb', line 5 def event_name @event_name end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
5 6 7 |
# File 'lib/basic_subscriber/subscriber.rb', line 5 def payload @payload end |
Class Method Details
.known_events ⇒ Object
34 35 36 |
# File 'lib/basic_subscriber/subscriber.rb', line 34 def known_events subscriptions.keys end |
.on(*event_names, &block) ⇒ Object
16 17 18 |
# File 'lib/basic_subscriber/subscriber.rb', line 16 def on(*event_names, &block) event_names.each { |name| subscriptions[scoped_name(name)].push block } end |
.scope(scope_name) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/basic_subscriber/subscriber.rb', line 20 def scope(scope_name) scope_name = scope_name.to_sym if block_given? previous_scope = current_scope.dup subscope scope_name yield self.current_scope = previous_scope else parent_scope = current_scope[1..-1] self.current_scope = parent_scope if parent_scope&.any? subscope scope_name end end |
.trigger(event_name, **payload) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/basic_subscriber/subscriber.rb', line 8 def trigger(event_name, **payload) event_name = event_name.to_sym subscriber = new event_name, payload subscriptions[event_name].each { |block| subscriber.instance_exec(&block) } nil end |
Instance Method Details
#scope_name ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/basic_subscriber/subscriber.rb', line 68 def scope_name return @scope_name if defined? @scope_name @scope_name = begin scope_name = split_event_name[2] scope_name == '' ? nil : scope_name.to_sym end end |
#unscoped_event_name ⇒ Object
64 65 66 |
# File 'lib/basic_subscriber/subscriber.rb', line 64 def unscoped_event_name @unscoped_event_name ||= split_event_name.first.to_sym end |