envato / event_sourcery

A library for building event sourced applications in Ruby
MIT License
84 stars 10 forks source link

Allow changing the event class using Event#with #190

Closed twe4ked closed 6 years ago

twe4ked commented 6 years ago

With typed events you can't change the event type, let's allow changing the event class.

old_event = ItemAdded.new

new_event = old_event.with(event_class: ItemRemoved)
new_event.type # => "item_removed"
new_event.class # => ItemRemoved
joesustaric commented 6 years ago

Hey @twe4ked stupid question incoming. (possibly missing some context I think) What scenario would this feature be useful in?

twe4ked commented 6 years ago

I was doing some reading on upcasting events and some of the patterns I found modified the event body and returned a different event type.

I might be jumping the gun a bit adding this functionality without actually using it. The reason I decided to open the PR is because I find it confusing at the moment that passing a type to Event#with is ignored. This essentially fixes that.

joesustaric commented 6 years ago

@twe4ked ah ha gotcha, didn't understand upcasters but just read Steves other PR so makes sense now.

So the use case would be that an upcasting function on an event may need to change the event type for whatever reason, can now do so easily. 👍