| 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.
| Reserved_events_regex | = | Regexp.new( /^render$/, 'i' ) | Used to check if the event name is one of the reserved events. |
Used to know if the step needs an explicitely declared step definition or if it can manage the task with a default behavior.
# 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.
# 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
# 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
# File lib/action_flow/flow_step.rb, line 126 def handles?(error_class) handlers.has_key?(error_class) 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
# 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.
# 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.
# 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