Class: Microkit::Publisher
- Inherits:
-
Object
- Object
- Microkit::Publisher
- Defined in:
- lib/microkit/publisher.rb
Instance Method Summary collapse
-
#initialize ⇒ Publisher
constructor
A new instance of Publisher.
- #publish(current_value, prev_value) ⇒ Object
- #subscribe(callback) ⇒ Object
Constructor Details
#initialize ⇒ Publisher
Returns a new instance of Publisher.
4 5 6 |
# File 'lib/microkit/publisher.rb', line 4 def initialize @subscribers = {} end |
Instance Method Details
#publish(current_value, prev_value) ⇒ Object
12 13 14 15 16 |
# File 'lib/microkit/publisher.rb', line 12 def publish(current_value, prev_value) @subscribers.each do |key, callback| callback.call(current_value, prev_value) end end |
#subscribe(callback) ⇒ Object
7 8 9 10 11 |
# File 'lib/microkit/publisher.rb', line 7 def subscribe(callback) key = (0...12).map { (65 + rand(26)).chr }.join @subscribers[key] = callback return Subscription.new(@subscribers, key) end |