lib/big_machine.rb in big_machine-1.0.1 vs lib/big_machine.rb in big_machine-1.1.0

- old
+ new

@@ -16,11 +16,11 @@ include BigMachine::ActiveRecord end end module ClassMethods - def active_record_model? + def active_record_model? defined?(::ActiveRecord::Base) && self.ancestors.include?(::ActiveRecord::Base) end def big_machine(options) self.initial_state = options[:initial_state] @@ -53,11 +53,21 @@ def forward_current_state extend SingleForwardable def_delegators :current_state, *current_state.class.transition_methods end - def transition_to(next_state_class) - current_state.exit + def transition_to(next_state_class, *args, &block) + return unless current_state.exit *args + + previous_state = current_state set_current_state next_state_class + + rollback(previous_state) and return unless current_state.enter *args + + block.call self if block_given? + end + + def rollback(previous_state) + @current_state = @previous_state end end