README.markdown in soveran-micromachine-0.0.6 vs README.markdown in soveran-micromachine-0.0.7
- old
+ new
@@ -9,11 +9,11 @@
There are many finite state machine implementations for Ruby, and they
all provide a nice DSL for declaring events, exceptions, callbacks,
and all kinds of niceties in general.
But if all you want is a finite state machine, look no further: this is only
-15 lines of code and provides everything a finite state machine must have, and
+22 lines of code and provides everything a finite state machine must have, and
nothing more.
Usage
-----
@@ -34,14 +34,59 @@
machine.on(:confirmed) do
puts "Confirmed"
end
+Adding MicroMachine to your models
+----------------------------------
+
+The most popular pattern among Ruby libraries that tackle this problem
+is to extend the model and transform it into a finite state machine.
+Instead of working as a mixin, MicroMachine's implementation is by
+composition: you instantiate a finite state machine (or many!) inside
+your model and you are in charge of querying and persisting the state.
+Here's an example of how to use it with an ActiveRecord model:
+
+ class Event < ActiveRecord::Base
+ before_save :persist_confirmation
+
+ def confirm!
+ confirmation.trigger(:confirm)
+ end
+
+ def cancel!
+ confirmation.trigger(:cancel)
+ end
+
+ def reset!
+ confirmation.trigger(:reset)
+ end
+
+ def confirmation
+ @confirmation ||= begin
+ @confirmation = MicroMachine.new(confirmation_state || "pending")
+ @confirmation.transitions_for[:confirm] = { "pending" => "confirmed" }
+ @confirmation.transitions_for[:cancel] = { "confirmed" => "cancelled" }
+ @confirmation.transitions_for[:reset] = { "confirmed" => "pending", "cancelled" => "pending" }
+ @confirmation
+ end
+ end
+
+ private
+
+ def persist_confirmation
+ self.confirmation_state = confirmation.state
+ end
+ end
+
+This example asumes you have a :confirmation_state attribute in your
+model. This may look like a very verbose implementation, but you gain a
+lot in flexibility.
+
Installation
------------
- $ gem sources -a http://gems.github.com (you only have to do this once)
- $ sudo gem install soveran-micromachine
+ $ sudo gem install micromachine
License
-------
Copyright (c) 2009 Michel Martens for Citrusbyte