Heteromorphic Fantasy: Obie's state_machine (2012)

E34acb847338523dc088f03f0eedd1eb?s=47 Rob Howard
February 05, 2012

Heteromorphic Fantasy: Obie's state_machine (2012)

A walk-through of state machines with @obie's gem.

Code examples here: https://github.com/damncabbage/state_machine_example

E34acb847338523dc088f03f0eedd1eb?s=128

Rob Howard

February 05, 2012
Tweet

Transcript

  1. Heteromorphic Fantasy @obrie’s state_machine

  2. None
  3. machine? state What is a

  4. :caterpillar

  5. :pupa

  6. None
  7. Caterpillar

  8. Caterpillar Pupa

  9. Caterpillar Pupa Eldritch Horror

  10. Caterpillar Pupa Pupate Eldritch Horror Emerge

  11. Let’s find a tamer example.

  12. Vending Machine

  13. state_machine

  14. :idle :awaiting selection push_button insert_dollar

  15. state :idle # Free: v.state == :idle v.idle?

  16. event :push_button # Free: v.can_push_button?

  17. :idle :awaiting selection push_button insert_dollar

  18. :idle :awaiting selection push_button insert_dollar :accepting money insert_dollar insert_dollar insert_dollar

  19. :idle :awaiting selection push_button insert_dollar :accepting money insert_dollar insert_dollar insert_dollar

    crowbar :broken
  20. Extra Goodies

  21. Per-State class_eval

  22. state :details do validates :name, :presence => true validates :email,

    :presence => true end state :shipping do validates :address, :presence => true end
  23. state :on do def volume dial_rotation * 100 / max_rotation

    end end state :mute do def volume 0 end end
  24. Multiple State Machines

  25. state_machine :state do state :idle state :awaiting_selection end state_machine :coin_slot

    do state :open state :closed end
  26. State Values

  27. state_machine :alarm do state :on, :value => 1 state :off,

    :value => 0 end
  28. Real World Where can I find some Examples already?

  29. Spree Commerce http://github.com/spree/spree

  30. state_machine :initial => 'cart', :use_transactions => false do event :next

    do transition :from => 'cart', :to => 'address' transition :from => 'address', :to => 'delivery' transition :from => 'delivery', :to => 'payment', :if => :payment_required? transition :from => 'delivery', :to => 'complete' transition :from => 'confirm', :to => 'complete' transition :from => 'payment', :to => 'confirm', :if => Proc.new { |order| order.payment_method && order.payment_method.payment_profiles_supported? } transition :from => 'payment', :to => 'complete' end event :cancel { transition :to => 'canceled', :if => :allow_cancel? } event :return { transition :to => 'returned', :from => 'awaiting_return' } event :resume { transition :to => 'resumed', :from => 'canceled', :if => :allow_resume? } event :authorize_return { transition :to => 'awaiting_return' } before_transition :to => 'complete' { |order| order.process_payments! } before_transition :to => ['delivery'] do |order| order.shipments.each { |s| s.destroy unless s.shipping_method.available_to_order?(order) } end after_transition :to => 'complete', :do => :finalize! after_transition :to => 'delivery', :do => :create_tax_charge! after_transition :to => 'payment', :do => :create_shipment! after_transition :to => 'resumed', :do => :after_resume after_transition :to => 'canceled', :do => :after_cancel end
  31. @damncabbage http://robhoward.id.au obrie/state_machine damncabbage/state_machine_example GitHub