arrow_backward Back to blog

Maintain: State maintenance like woah

Managing and maintaining state is a fairly common pattern (and problem!) in Ruby web apps. That’s probably why there are so many libraries out there that do it.

The way we use state machines is fairly simple – rarely do we touch on concurrency and its friends. Really, we want to do a few basic things:

1. Give states a name (“new”, “deleted,” “flagged”)
2. Test whether or not an object is in a given state (“alive?”, “dead?”).
3. Run code when states change (from “happy” to “sad,” self.buy_ice_cream)

And you can do that pretty easily with any of the existing Ruby state machines. It’s not pretty, but you can do it.

But there are a few cases that don’t help.

– What if you wanted to maintain two separate states in a single class?
– What if you wanted to call something “new” but save it as “0”?
– What if you wanted to know whether or not one instance’s state is further along than another’s?
– What happens when you want to store complex state that isn’t just a single value?

Until today, you’d need to write that yourself. But now you needn’t bother, since I’ve done it for you. It’s called **Maintain** and it’s built with a few goals in mind:

1. Be simple.


New Project Request