Module: FourthDimensional::Eventable::ClassMethods

Defined in:
lib/fourth_dimensional/eventable.rb

Instance Method Summary collapse

Instance Method Details

#event_bindingsObject

Returns a hash of event classes and the callback.

Post.on(PostAdded, -> (event) {})
Post.on(PostDeleted, -> (event) {})

Post.event_bindings # => {
  PostAdded => Proc,
  PostDeleted => Proc
}


51
52
53
# File 'lib/fourth_dimensional/eventable.rb', line 51

def event_bindings
  @event_bindings ||= {}
end

#eventsObject

Returns an array of class names for the bound events.

Post.on(PostAdded, -> (event) {})
Post.on(PostDeleted, -> (event) {})

Post.events # => [PostAdded, PostDeleted]


38
39
40
# File 'lib/fourth_dimensional/eventable.rb', line 38

def events
  event_bindings.keys
end

#on(klass, &block) ⇒ Object

Binds an event to the aggregate. Raises a KeyError if the event has already been bound.

Post.on(PostAdded, -> (event) {})
Post.on(PostAdded, -> (event) {}) # => raises KeyError


24
25
26
27
28
29
30
# File 'lib/fourth_dimensional/eventable.rb', line 24

def on(klass, &block)
  if event_bindings.has_key?(klass)
    raise KeyError.new("#{klass.name} is already bound on #{self.name}") 
  end

  event_bindings[klass] = block
end