Class: Jets::Builders::Deducer

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

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Deducer

Allow user to specify relative or full path. The right path gets used internally. Example paths:

app/controllers/posts_controller.rb
app/jobs/hard_job.rb
/tmp/jets/build/app_root/app/jobs/hard_job.rb
/tmp/jets/build/app_root/app/functions/hello.rb


10
11
12
13
# File 'lib/jets/builders/deducer.rb', line 10

def initialize(path)
  @full_path = full(path)
  @relative_path = relative(path)
end

Instance Method Details

#full(path) ⇒ Object



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

def full(path)
  path = "#{Jets.root}#{path}" unless path.starts_with?("/")
  path
end

#functionsObject

Returns the public methods of the child_class. Example: [:create, :update]



39
40
41
42
43
44
45
46
47
48
# File 'lib/jets/builders/deducer.rb', line 39

def functions
  # Example: require: /tmp/jets/demo/app_root/app/controllers/admin/posts_controller.rb
  class_name = @full_path.sub(/.*\/app\/\w+\//,'').sub(/\.rb/,'') # admin/posts_controller
  class_name = class_name.classify # Admin::PostsController
  # Autoload instead of require to avoid accidentallly double requiring and
  # it resulting WARNINGs when there are constants already defined
  klass = class_name.constantize # autoload

  klass.lambda_functions
end

#handler_for(meth) ⇒ Object

This gets called in the node shim js template



60
61
62
63
64
# File 'lib/jets/builders/deducer.rb', line 60

def handler_for(meth)
  # possibly not include _function
  underscored_name = @relative_path.sub(%r{app/(\w+)/},'').sub('.rb','')
  "handlers/#{process_type.pluralize}/#{underscored_name}.#{meth}"
end

#js_pathObject

Example return: “handlers/controllers/posts.js”



67
68
69
70
71
# File 'lib/jets/builders/deducer.rb', line 67

def js_path
  @relative_path
    .sub("app", "handlers")
    .sub(/\.rb$/, ".js")
end

#klassObject

Examples: PostsController, HardJob, Hello, HelloFunction



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

def klass
  @klass ||= Jets::Klass.from_path(@relative_path)
end

#lang(meth) ⇒ Object



55
56
57
# File 'lib/jets/builders/deducer.rb', line 55

def lang(meth)
  klass.tasks.find
end

#process_typeObject

process_type is key, it will either “controller” or “job”. It is used to deduce klass, etc. We get the process_type from the path. Example paths:

app/controllers/posts_controller.rb
app/jobs/hard_job.rb


33
34
35
# File 'lib/jets/builders/deducer.rb', line 33

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

#relative(path) ⇒ Object



20
21
22
23
24
25
# File 'lib/jets/builders/deducer.rb', line 20

def relative(path)
  full_path = full(path)
  full_path.sub(Jets.root.to_s, "")
           .sub(/.*internal\/app/, "app")

end