geekq / workflow

Ruby finite-state-machine-inspired API for modeling workflow
MIT License
1.74k stars 207 forks source link

Callback symbols and transition callbacks by convention #241

Closed tlloydthwaites closed 1 month ago

tlloydthwaites commented 4 months ago

When building a workflow spec dynamically we can't specify procs, but we can specify symbols for methods we want to run.

This PR allows us to specify a symbol when calling on_entry and on_exit.

It also tries to call before_transition, on_transition, after_transition, and on_error as methods on the workflow object as well as using the standard procs.

geekq commented 1 month ago

Thanks for your contribution! This pull request contains two unrelated things:

  1. For general hooks like before_transition, on_error, on_entry etc. in addition to blocks also support methods by naming convention
  2. Allow symbols instead of specifing procs when defining workflow from some hash programmatically

For the first one I have no objections except minor incompatibility issue in case somebody defined e.g. an on_error method to do something unrelated to workflow. Formally we would need to wait until a major 4.0 release and add some deprecation warning already. Or maybe use more specific method names like on_workflow_error instead of on_error, where it is more unlikely for somebody to already have such a method for a different purpose. After carefully considering pros and contra, I would stick with existing functionality for now.

For the second part, I think it brings a lot of additional complexity to the very core of workflow by storing symbols instead of Procs sometimes.

Dynamically composing workflow definition from a json file, could be useful for some people though. Thank you for providing an extensive example! The desired result can be achieved with existing features of workflow, without changes to the library like that: https://github.com/geekq/workflow/blob/develop/test/workflow_from_json_test.rb I started with your original test file and made some minor adjustments to take advantage of the existing workflow model.