Module: Replay::Publisher
- Defined in:
- lib/replay/publisher.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #apply(events, raise_unhandled = true) ⇒ Object
- #key_attr ⇒ Object
- #publish(event, metadata = {}) ⇒ Object
- #to_stream_id ⇒ Object
Class Method Details
.included(base) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/replay/publisher.rb', line 3 def self.included(base) base.instance_variable_set :@application_blocks, {} base.extend ClassMethods base.extend(Replay::Events) base.class_exec do include Replay::Subscriptions end end |
Instance Method Details
#apply(events, raise_unhandled = true) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/replay/publisher.rb', line 12 def apply(events, raise_unhandled = true) return apply([events], raise_unhandled) unless events.is_a?(Array) events.each do |event| apply_method = apply_method_for(event.class) raise UnhandledEventError.new "event #{event.type} is not handled by #{self.class.name}" if (!respond_to?(apply_method) && raise_unhandled) self.send(apply_method, event) end return self end |
#key_attr ⇒ Object
44 45 46 |
# File 'lib/replay/publisher.rb', line 44 def key_attr self.class.key_attr end |
#publish(event, metadata = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/replay/publisher.rb', line 29 def publish(event, ={}) return publish([event]) unless event.is_a?(Array) event.each do |evt| = ({:published_at => Time.now}.merge!()) apply(evt) subscription_manager.notify_subscribers(to_stream_id, evt, ) end return self end |
#to_stream_id ⇒ Object
39 40 41 42 |
# File 'lib/replay/publisher.rb', line 39 def to_stream_id raise Replay::UndefinedKeyError.new("No key attribute defined for #{self.type}") unless self.key_attr self.send(self.key_attr).to_s end |