Class: Evertils::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/evertils/router.rb

Instance Method Summary collapse

Constructor Details

#initialize(config_instance) ⇒ Router

Create the router object Params:

config_instance

An instance of Evertils::Cfg



6
7
8
# File 'lib/evertils/router.rb', line 6

def initialize(config_instance)
  @config = config_instance
end

Instance Method Details

#pre_execObject

Prepare for routing



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/evertils/router.rb', line 11

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

#routeObject

Perform command routing



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/evertils/router.rb', line 25

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