adomokos / light-service

Series of Actions with an emphasis on simplicity.
MIT License
837 stars 67 forks source link

Surprising `add_to_context` orchestrator behaviour #210

Closed gee-forr closed 3 years ago

gee-forr commented 3 years ago

Hiya @adomokos !

Got a weird one... I sometimes have organizers that use iterate and need to get the promised values from each iteration into something in the context.

Here's a dummy example:

# In an imaginary organizer called ApplyRatingsToBook
def self.actions
  [
    add_to_context(book_ratings: []),
    iterate(:ratings, [
      Book::ApplyRating # <- thus guy expects a `Book` and `Rating`, and promises a 'BookRating' for e.g.
      execute(->(ctx) { ctx.book_ratings << ctx.book_rating }) # I'm collecting all `book_ratings` for something outside of the organizer
  ]
end

The surprising behaviour

I would assume that when I add something to the context using add_to_context like above, it's available as an accessor in much the same way that expects and promises works, except it is only accessible as a key on the Context object which is ultimately returned from the organizer.

A way forward

If you agree with the above, I'd love to put together a PR that updates the Organizer add_to_context feature to turn it into a context accessor as well as the standard key.

Let me know :) and thanks again :)

adomokos commented 3 years ago

I like that, please go for it!