Play symbols
sunny opened this issue · comments
This is a feature idea for using Ruby symbols as play
arguments.
Currently
Sometimes, we need to interlace small actions with larger ones inside a play. For that, instead of giving it actors we can give it lambda actions.
For example :
class PlaceOrder < ApplicationActor
input :order
input :log, default: true
play -> actor do
actor.order.currency ||= Current.currency
actor.order.save! if actor.order.changes.any?
end,
Orders::Create,
Orders::Notify,
-> actor { Logger.log("Order #{actor.order.id} created") if actor.log }
end
Ideally
In some cases it could be nice to be able to give it instance methods as well:
class PlaceOrder < ApplicationActor
input :order
input :log, default: true
play :set_current_currency,
Orders::Create,
Orders::Notify,
:log_order_creation
private
def set_current_currency
order.currency ||= Current.currency
order.save! if order.changes.any?
end
def log_order_creation
Logger.log("Order #{order.id} created") if log
end
end
An alternative with the current implementation is to define your lambdas before you use them.
class PlaceOrder < ApplicationActor
input :order
input :log, default: true
set_current_currency = ->(actor) do
actor.order.currency ||= Current.currency
actor.order.save! if order.changes.any?
end
log_order_creation = ->(actor) do
Logger.log("Order #{actor.order.id} created") if actor.log
end
play set_current_currency,
Orders::Create,
Orders::Notify,
log_order_creation
end
Unfortunately this does require defining your play
sequence at the bottom of the actor so it isn't immediately clear what it's doing. Being able to define and use instance methods also feel more like the ruby (read: OO) way.
Oh, indeed, that could be a good alternative for the time being.
What I also like about instance methods though is that they have access to inputs, which makes them terser.
I want to start using this yesterday! 💟 For now I'll settle for the pre-defined lambda locals.
Released in v3.2 🚀