state-machines / state_machines

Adds support for creating state machines for attributes on any Ruby class
https://github.com/state-machines/state_machines
MIT License
814 stars 91 forks source link

Passing event kwargs to guards option #101

Open Eyvgeniy opened 5 months ago

Eyvgeniy commented 5 months ago

Add passing kwargs from event to guard

class Vehicle
  state_machine :state, initial: :parked do
    ...
    event :crash do
      transition all - [:parked, :stalled] => :stalled, if: ->(vehicle, kwargs) {kwargs[:arg] == true} # kwargs from event fire
    end
  end
end

vehicle = Vehicle.new
vehicle.crash(arg: false) # false
joegaudet commented 5 months ago

+1 for htis!