Method: Fabulator::Core::Structurals::StateMachine#run_transition

Defined in:
lib/fabulator/core/structurals/state_machine.rb

#run_transition(best_transition) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/fabulator/core/structurals/state_machine.rb', line 84

def run_transition(best_transition)
  return if best_transition.nil? || best_transition.empty?
  current_state = @states[@state]
  t = best_transition[:transition]
  @missing_params = best_transition[:missing]
  @errors = best_transition[:messages]
  if @missing_params.empty? && @errors.empty?
    @state = t.state
    # merge valid and context
    best_transition[:valid].sort_by { |a| a.path.length }.each do |item|
      p = item.path.gsub(/^[^:]+::/, '').split('/') - [ '' ]
      n = @context.traverse_path(p, true).first
      n.prune
      n.copy(item)
    end
    # run_post of state we're leaving
    begin
      current_state.run_post(@context)
      t.run(@context)
      # run_pre for the state we're going to
      new_state = @states[@state]
      new_state.run_pre(@context) if !new_state.nil?
    rescue Fabulator::StateChangeException => e # catch state change
      new_state = @states[e]
      begin
        if !new_state.nil?
          @state = new_state.name
          new_state.run_pre(@context)
        end
      rescue Fabulator::StateChangeException => e
        new_state = @states[e] 
        retry
      end
    end
  end
end