Sha256: a53a07df146775c9c5ceb142330114bf23e6c63a925dc5a313a5f98b7fc4b235

Contents?: true

Size: 1.59 KB

Versions: 21

Compression:

Stored size: 1.59 KB

Contents

module Evertils
  class Router
    # Create the router object
    # Params:
    # +config_instance+:: An instance of Evertils::Cfg
    def initialize(config_instance)
      @config = config_instance
    end

    # Prepare for routing
    def pre_exec
      @request = Request.new

      begin
        # include the controller
        require "evertils/controllers/#{@request.controller}"
        # include helpers
        require "evertils/helpers/#{@request.controller}" if File.exist? "evertils/helpers/#{@request.controller}"
      rescue LoadError
        Notify.error("Controller not found: #{@request.controller}")
      end
    end

    # Perform command routing
    def route
      pre_exec

      # Create object context and pass it the required command line arguments
      begin
        unless @request.controller.nil?
          controller = Evertils::Controller.const_get @request.controller.capitalize

          # create an instance of the requested controller
          context = controller.new(@config, @request)

          if context.can_exec? @request.command
            # Set things up
            context.pre_exec

            # Run the requested action
            context.exec

            # Run cleanup commands
            context.post_exec
          end
        end
      rescue NoMethodError => e
        Notify.error("#{e}\n#{e.backtrace.join("\n")}", show_time: false)
      rescue RuntimeError => e
        Notify.error("#{e}\n#{e.backtrace.join("\n")}", show_time: false)
      rescue NameError => e
        Notify.error("#{e}\n#{e.backtrace.join("\n")}", show_time: false)
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
evertils-0.3.27 lib/evertils/router.rb
evertils-0.3.26 lib/evertils/router.rb
evertils-0.3.25 lib/evertils/router.rb
evertils-0.3.24 lib/evertils/router.rb
evertils-0.3.23 lib/evertils/router.rb
evertils-0.3.22 lib/evertils/router.rb
evertils-0.3.21 lib/evertils/router.rb
evertils-0.3.20 lib/evertils/router.rb
evertils-0.3.19 lib/evertils/router.rb
evertils-0.3.18 lib/evertils/router.rb
evertils-0.3.17 lib/evertils/router.rb
evertils-0.3.16 lib/evertils/router.rb
evertils-0.3.15 lib/evertils/router.rb
evertils-0.3.14.1 lib/evertils/router.rb
evertils-0.3.14 lib/evertils/router.rb
evertils-0.3.13 lib/evertils/router.rb
evertils-0.3.12 lib/evertils/router.rb
evertils-0.3.11 lib/evertils/router.rb
evertils-0.3.10 lib/evertils/router.rb
evertils-0.3.9 lib/evertils/router.rb