Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Play symbols #71

Closed
sunny opened this issue Jul 5, 2022 · 4 comments · Fixed by #72
Closed

Play symbols #71

sunny opened this issue Jul 5, 2022 · 4 comments · Fixed by #72
Labels
enhancement New feature or request

Comments

@sunny
Copy link
Owner

sunny commented Jul 5, 2022

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
@sunny sunny added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Jul 5, 2022
@Buzzkill-McSquare
Copy link

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.

@sunny
Copy link
Owner Author

sunny commented Jul 7, 2022

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.

@pboling
Copy link
Contributor

pboling commented Jul 8, 2022

I want to start using this yesterday! 💟 For now I'll settle for the pre-defined lambda locals.

@sunny sunny removed help wanted Extra attention is needed good first issue Good for newcomers labels Jul 8, 2022
@sunny sunny closed this as completed in #72 Jul 11, 2022
@sunny
Copy link
Owner Author

sunny commented Jul 11, 2022

Released in v3.2 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants