-
Notifications
You must be signed in to change notification settings - Fork 92
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
Dynamically pick state machine? #98
Comments
you can make multiple instance of your state machine or as per you, It seems like you want to conditionally select a state machine definition for a class based on a tenant-specific condition. While the Ruby StateMachine gem doesn't provide built-in support for this specific use case, you can achieve your goal by using dynamic class methods to define state machines. Here's one way to do it: class Vehicle
def initialize
super
state_machine = if tenant_a?
StateMachineA.new(self)
else
StateMachineB.new(self)
end
end
end
class StateMachineBase
def initialize(vehicle)
@vehicle = vehicle
end
end
class StateMachineA < StateMachineBase
def self.define
state_machine :state, initial: :parked do
event :start_car do
# put in gear
end
end
end
end
class StateMachineB < StateMachineBase
def self.define
state_machine :state, initial: :working do
event :repair_car do
# mark as broken
# fix car
end
event :start_car do
# mark as working
end
end
end
end In this approach:
This way, you can dynamically select and use a state machine based on your tenant conditions without relying on a single, static definition for all instances of |
Hello,
We have a multi-tenant application and would like to hard-code our state machine definitions, but be able to dynamically pick which state machine gets selected based on our tenant. I think the dynamic definition in the docs is overkill (and focuses primarily on dynamic transitions while we want the entire thing to be dynamic). Is there a nice way to say, for this class, use this state machine under condition A and use that state machine under condition B?
Ideally, we would like something along the lines of:
So that we can change the behaviour of
vehicle.start_car
based on which definition file we're using.Is something like this possible?
The text was updated successfully, but these errors were encountered: