Zyph Martin Design Studios Search


Zyph Martin Design Studios Recent Entries

THINGS I'M THANKFUL FOR

Posted on November 22 2012


BEST THING I'VE SEEN IN A LONG TIME.

Posted on April 11 2012


TELLING IT LIKE IT IS.

Posted on January 26 2012


COMING SOON TO A BROWSER NEAR YOU!

Posted on December 01 2011


SAY GOODBYE TO FLASH...

Posted on June 09 2011


VIEW ARCHIVES -->


RAILS 3, MONGOID, AND STATE MACHINE



Mongoid_statemachine

[UPDATE] Ryan the author of stateflow has updated the library to work with Rails 2.3.x and Rails 3 you no longer need to modify anything to get it working with Rails 3.

 

It is amazing that we have had a blog for about 9 months and I have only posted 1 blog post. I am going to try and make a point to change that and start posting some more about what I am working on, what goodies I am using, and anything that might be useful to someone else or myself later down the road. So Rails 3 is out and it is great. I upgraded our site to a release candidate of it a week or so ago and everything went well with that, but I will talk about that in another post. Along with upgrading our site to Rails 3 I also changed from MongoMapper to MongoID. I think that they are both great libraries, but there were some things that just flowed better for me personally with MongoID. 

 

Right now I am working on a project that I have a need for a state machine library. I googled and found stateflow. Stateflow supports MongoID as a persistence layer. I only needed to modify the persistence file for MongoID to adjust the callback for Rails 3 since the :before_validation_on_create has changed

#[UPDATED] Removed because you know longer need to do modify anything. Ryan has updated the library.

So far in my small initial tests all seems to be working fine for me. I will post back if I run into anything that isn't working for me.

Stateflow.persistence = :mongoid

class Light
  include Mongoid::Document
  include Stateflow
  
  field :state
  
  stateflow do
    state_column :state
    initial :green
    
    state :green
    state :yellow do
      exit :catch_runners
    end
    state :red
    
    event :change_color do
      transitions :from => :green, :to => :yellow
      transitions :from => :yellow, :to => :red
      transitions :from => :red, :to => :green
    end
  end
  
  def catch_runners
    puts "That'll be $250."
  end
  
end

light = Light.new
light.current_state    # :green
light.change_color!    # true
light.current_state    # :yellow
light.change_color!    # true
"That'll be $250."
light.current_state    # :red


Back To Blog - Posted on August 31 2010 by Brandon Martin




blog comments powered by Disqus