Class ActionFlow::FlowStep
In: lib/action_flow/flow_step.rb
Parent: Object

This class defines a basic skeletton for different step types used by the ActionFlow framework. All new step types must inherit of this superclass to be usable in the ActionFlow controller.

It is also the responsability of any subclass to add it‘s declaration method in the ActionFlow::Base class. See view_step.rb for an example.

Also, subclasses can change the @definition_required instance variable value to tell the ActionFlow framework that it can handle the execution without the user controller defining explicitely a step implementation. See view_step.rb(initialize) for an example.

Event outcomes are defined in the @outcomes instance variable while the error handlers are defined in the @handlers instance variable. Those variables are protected, so any subclasses of FlowStep can access them and hack the mechanism if required.

Methods

Constants

Reserved_events_regex = Regexp.new( /^render$/, 'i' )   Used to check if the event name is one of the reserved events.

Public Instance methods

Used to know if the step needs an explicitely declared step definition or if it can manage the task with a default behavior.

[Source]

# File lib/action_flow/flow_step.rb, line 60
    def definition_required?
        @definition_required
    end

Method signature to override in implementing classes. Does the ‘dirty job’ once it is required.

[Source]

# File lib/action_flow/flow_step.rb, line 51
    def execute(*args)
  
      raise (ActionFlowError.new, "You can't directly use FlowStep as a step. As a matter of fact, I don't even know how you were able in the first place anyways...")
  
    end

Returns the appropriate step name to execute upon the given error class

[Source]

# File lib/action_flow/flow_step.rb, line 134
    def handler(error_class)
      error_class.kind_of?(String) ? key = error_class : key = error_class.to_s
      handlers.has_key?(key) ? handlers.fetch(key) : raise(ActionFlowError.new, "There's no handler defined for the error class '#{key}'.")
    end

Tells if the error class passed as an argument is handled

[Source]

# File lib/action_flow/flow_step.rb, line 126
    def handles?(error_class)
      handlers.has_key?(error_class)
    end

Tells if the given event name is a possibe outcome of this step

[Source]

# File lib/action_flow/flow_step.rb, line 141
    def has_an_outcome_for?(event_name)
      outcomes.has_key? event_name.to_s
    end

Maps an event to a method call. Use in a mapping like this :

implemented_step_type :id_of_step do

  on :event => :method

end

This is also possible :

implemented_step_type :id_of_step do

  on { :event => :method,
       :event2 => :method2 }

end

[Source]

# File lib/action_flow/flow_step.rb, line 79
    def on ( hash )
    
      # Make sure the key is not used twice in a definition
      # to enforce coherence of the mapping.
      hash.each_key do |key|
        raise (ActionFlowError.new, "An event already uses the name '#{key}'. They must be unique within a step scope.") if outcomes.has_key?(key.to_s)
        raise (ActionFlowError.new, "You can't define an event named '#{key}' since it's a reserved event keyword.") if (key.to_s =~ Reserved_events_regex)
      end
    
      # Store the values in the possible outcomes hash
      hash.each { |key,value| outcomes.store key.to_s, value.to_s }
    
    end

Returns the step name associated to the given event.

[Source]

# File lib/action_flow/flow_step.rb, line 147
    def outcome(event_name)
      outcomes.has_key?(event_name.to_s) ? outcomes.fetch(event_name.to_s) : raise(ActionFlowError.new, "There's no outcome defined for the event name '#{event_name}'.")
    end

Maps an error class to a method call. Use in a mapping like this :

implemented_step_type :id_of_step do

  upon ActionFlowError.class => :method

end

This is also possible :

implemented_step_type :id_of_step do

  upon { ActionFlowError.class => :method,
         WhateverError.class => :method }

end

Only subclasses of StandardError will be rescued. This means that RuntimeError cannot be handeled.

[Source]

# File lib/action_flow/flow_step.rb, line 110
    def upon ( hash )
    
      # Make sure the key is not used twice in a definition
      # to enforce coherence of the mapping.
      hash.each_key do |key|
        raise (ActionFlowError.new, "An error of the class '#{key}' is already mapped. They must be unique within a step scope.") if handlers.has_key?(key.to_s)
      end
    
      # Store the vlaues in the possible handlers
      hash.each { |key,value| handlers.store key.to_s, value.to_s }
    
    end

Protected Instance methods

Holds the methods to call upon errors

[Source]

# File lib/action_flow/flow_step.rb, line 172
      def handlers
        @handlers ||= {}
      end

Holds the different outcomes possible, which are defined via the ‘on’ method

[Source]

# File lib/action_flow/flow_step.rb, line 165
      def outcomes
        @outcomes ||= {}
      end

[Validate]