Module: BigMachine

Extended by:
ActiveSupport::Concern
Defined in:
lib/big_machine.rb,
lib/big_machine/lock.rb,
lib/big_machine/state.rb,
lib/big_machine/version.rb,
lib/big_machine/active_record.rb,
lib/big_machine/available_methods.rb

Defined Under Namespace

Modules: ActiveRecord, AvailableMethods, ClassMethods, Lock Classes: State

Constant Summary collapse

VERSION =
"1.2.0"

Instance Method Summary collapse

Instance Method Details

#current_stateObject



41
42
43
44
45
# File 'lib/big_machine.rb', line 41

def current_state
  set_current_state(self.class.initial_state_class) unless @current_state

  @current_state
end

#forward_current_stateObject



53
54
55
56
# File 'lib/big_machine.rb', line 53

def forward_current_state
  extend SingleForwardable
  def_delegators :current_state, *current_state.class.available_methods
end

#rollback(previous_state) ⇒ Object



69
70
71
# File 'lib/big_machine.rb', line 69

def rollback(previous_state)
  @current_state = @previous_state
end

#set_current_state(new_state_class) ⇒ Object



47
48
49
50
51
# File 'lib/big_machine.rb', line 47

def set_current_state(new_state_class)
  @current_state = new_state_class.new self

  forward_current_state
end

#transition_to(next_state_class, *args, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/big_machine.rb', line 58

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