geekq / workflow

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

allow event arguments to be taken into account when selecting the event #227

Closed aserafin closed 4 months ago

aserafin commented 2 years ago

This allows access to event arguments when we're evaluating which event to run in the scope of

state :off
  event :turn_on, :transition_to => :on, :if => :sufficient_battery_level?

  event :turn_on, :transition_to => :low_battery, :if => proc { |device, _power_adapter| device.battery_level > 0 }
end

# corresponding instance method
def sufficient_battery_level?(power_adapter)
  power_adapter || battery_level > 10
end

@geekq I imagine this potentially is a breaking change but I run into a situation when event arguments had to be taken into account for conditional event - let me know what you think 👍 🙇

geekq commented 2 years ago

Thanks for your contribution! Backward compatibility is very important (people have used this library for many years, and some do not upgrade to the newest Ruby or Rails versions). The compatibility should be covered (checked) by the test suite though. Good thing would be to switch from travis-CI (ceased) to GitHub actions, before integrating any new contributions. Maybe something inspired by https://github.com/geekq/workflow-activerecord/blob/develop/.github/workflows/test.yml so any regression can be prevented.

For the new behavior we'll need test cases as well.

geekq commented 2 years ago

Blocked by gh-229

geekq commented 2 years ago

Please rebase your PR, so we can see the results of the tests for different Ruby versions

geekq commented 4 months ago

Added unit tests, documentation, rebased and merged