Class: Jets::Processors::Deducer

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/processors/deducer.rb

Overview

Jets::Processors::Deducer class figures out information that allows the controller or job to be called. Sme key methods for deducer:

code - code to instance eval.  IE: PostsController.new(event, context).index
path - full path to the app code. IE: #{Jets.root}app/controllers/posts_controller.rb

Instance Method Summary collapse

Constructor Details

#initialize(handler) ⇒ Deducer

Returns a new instance of Deducer.



8
9
10
11
12
13
# File 'lib/jets/processors/deducer.rb', line 8

def initialize(handler)
  @handler = handler # handlers/controllers/posts.show
  # @handler_path: "handlers/controllers/posts"
  # @handler_method: "show"
  @handler_path, @handler_method = @handler.split('.')
end

Instance Method Details

#class_nameObject

Example underscored_class_name:

class_name = underscored_class_name
class_name = class_name # PostsController


36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jets/processors/deducer.rb', line 36

def class_name
  regexp = Regexp.new(".*handlers/#{process_type.pluralize}/")
  # Example regexp:
  #   /.*handlers\/controllers\//
  #   /.*handlers\/jobs\//
  class_name = @handler_path.sub(regexp, "")
  # Example class names:
  #   posts_controller
  #   hard_job
  #   hello
  #   hello_function

  class_name.classify
end

#codeObject



15
16
17
18
19
# File 'lib/jets/processors/deducer.rb', line 15

def code
  # code: "PostsController.process(event, context, meth: "show")"
  # code: "HardJob.process(event, context, meth: "dig")"
  %|#{class_name}.process(event, context, "#{@handler_method}")|
end

#load_classObject



51
52
53
# File 'lib/jets/processors/deducer.rb', line 51

def load_class
  Jets::Klass.from_path(path)
end

#pathObject

Input: @handler_path: handlers/jobs/hard_job.rb Output: #{Jets.root/app/jobs/hard_job.rb



23
24
25
# File 'lib/jets/processors/deducer.rb', line 23

def path
  Jets.root.to_s + @handler_path.sub("handlers", "app") + ".rb"
end

#process_typeObject

process_type is key. It can be either “controller” or “job”. It is used to deduce the rest of the methods: code, path.



29
30
31
# File 'lib/jets/processors/deducer.rb', line 29

def process_type
  @handler.split('/')[1].singularize # controller or job
end