Sha256: 7793b53771b0fd9e4f4a47cdad593d5fb866d8b82ad2255d6fbe6e4ea32064da

Contents?: true

Size: 1.54 KB

Versions: 3

Compression:

Stored size: 1.54 KB

Contents

module Controll::Flow
  class Master    
    class Executor < Controll::Executor::Base
      Flow = Controll::Flow

      NoEventsDefinedError  = Flow::NoEventsDefinedError
      NoMappingFoundError   = Flow::NoMappingFoundError

      attr_reader :event, :action_handlers
        
      def initialize initiator, options
        raise ArgumentError, "Must take an options arg" unless options.kind_of?(Hash)
        raise ArgumentError, "Must take an :event option" unless options[:event]
        raise ArgumentError, "Must take an :action_handlers option" unless options[:action_handlers]
        super
        @event = options[:event]
        @action_handlers = options[:action_handlers]
      end

      def execute        
        action_handlers.each do |action_handler|
          begin          
            action_handler_clazz = handler_class(action_handler)
            next unless action_handler_clazz
            return action_handler_clazz.action(controller, event)
          rescue NoEventsDefinedError => e
            errors << e
          rescue NoMappingFoundError => e
            errors << e
          end
        end
        fallback
      end

      def errors
        @errors ||= []
      end

      protected

      def fallback
        fallback_class.new controller, event
      end

      def fallback_class
        Flow::Action::Fallback
      end

      def handler_class action_handler
        clazz = "#{initiator.class}::#{action_handler.to_s.camelize}"
        clazz.constantize
      rescue NameError
        nil
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
controll-0.3.2 lib/controll/flow/master/executor.rb
controll-0.3.1 lib/controll/flow/master/executor.rb
controll-0.3.0 lib/controll/flow/master/executor.rb