Module: WizardMachine

Defined in:
lib/wizard_machine.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.__wizard_machine_processing_classObject



3
4
5
# File 'lib/wizard_machine.rb', line 3

def self.__wizard_machine_processing_class
  @__wizard_machine_processing_class
end

.__wizard_machine_processing_class=(klass) ⇒ Object



7
8
9
10
# File 'lib/wizard_machine.rb', line 7

def self.__wizard_machine_processing_class=(klass)
  raise "__wizard_machine_processing_class is already set (#{@__wizard_machine_processing_class.name}). Make sure you are calling setup_wizard_events! (#{klass.name}). " if !klass.nil? && !@__wizard_machine_processing_class.nil?
  @__wizard_machine_processing_class = klass
end

.included(base) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/wizard_machine.rb', line 12

def self.included(base)
  WizardMachine.__wizard_machine_processing_class = base
  base.send :include, AASM
  base.extend(ClassMethods)

  # base.aasm_column :state #default is :aasm_state

  base.after_create :created!
  
  base.aasm_initial_state :new
  base.aasm_state :new
end

Instance Method Details

#all_state_namesObject



120
121
122
# File 'lib/wizard_machine.rb', line 120

def all_state_names
  self.class.all_state_names
end

#next_state!Object



104
105
106
# File 'lib/wizard_machine.rb', line 104

def next_state!
  set_state! next_state_name
end

#next_state_indexObject



96
97
98
# File 'lib/wizard_machine.rb', line 96

def next_state_index
  wizard_complete? ? nil : state_index + 1
end

#next_state_nameObject



100
101
102
# File 'lib/wizard_machine.rb', line 100

def next_state_name
  all_state_names[next_state_index]
end

#previous_state!Object



112
113
114
# File 'lib/wizard_machine.rb', line 112

def previous_state!
  set_state! previous_wizard_state
end

#previous_state_indexObject



92
93
94
# File 'lib/wizard_machine.rb', line 92

def previous_state_index
  state_index.zero? ? 0 : state_index - 1
end

#previous_wizard_stateObject



108
109
110
# File 'lib/wizard_machine.rb', line 108

def previous_wizard_state
  all_state_names[previous_state_index]
end

#set_state!(state) ⇒ Object



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

def set_state!(state)
  raise "No state with name: #{state.inspect}. wizard_states: #{self.wizard_state_names.inspect}" unless valid_state?(state)
  self.send "#{state}!"
end

#state_indexObject

TODO consider using wizard_state_names for the index. does it matter? this should just be internal, so it shouldn’t really matter (I think).



79
80
81
# File 'lib/wizard_machine.rb', line 79

def state_index
  all_state_names.index wizard_state
end

#valid_state?(state) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/wizard_machine.rb', line 73

def valid_state?(state)
  state = state.intern if state.respond_to? :intern
  all_state_names.include? state
end

#wizard_complete?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/wizard_machine.rb', line 116

def wizard_complete?
  :wizard_complete == wizard_state
end

#wizard_current_stepObject



88
89
90
# File 'lib/wizard_machine.rb', line 88

def wizard_current_step
  state_index
end

#wizard_stateObject



83
84
85
86
# File 'lib/wizard_machine.rb', line 83

def wizard_state
  raise "Model is saved in an invalid state. You must have changed some of the wizard_states. Invalid state: #{aasm_current_state.inspect}. wizard_states: #{self.wizard_state_names.inspect}" unless valid_state?(aasm_current_state)
  aasm_current_state
end

#wizard_state_namesObject



124
125
126
# File 'lib/wizard_machine.rb', line 124

def wizard_state_names
  self.class.wizard_state_names
end