Class: Jets::Commands::Call::AutoloadGuesser

Inherits:
BaseGuesser
  • Object
show all
Defined in:
lib/jets/commands/call/autoload_guesser.rb

Instance Method Summary collapse

Methods inherited from BaseGuesser

#class_name, #function_name, #generated_function_name, #initialize, #parent_stack, #stack_resources

Methods included from AwsServices

#apigateway, #aws_lambda, #cfn, #dynamodb, #logs, #s3, #s3_resource, #sns, #sqs, #sts

Methods included from AwsServices::StackStatus

#lookup, #stack_exists?, #stack_in_progress?

Methods included from AwsServices::GlobalMemoist

included

Constructor Details

This class inherits a constructor from Jets::Commands::Call::BaseGuesser

Instance Method Details

#autoload_pathsObject

Guesses autoload paths.

underscored_name: admin_related_pages_controller Returns:

[
  "admin_related_pages_controller",
  "admin/related_pages_controller",
  "admin_related/pages_controller",
  "admin_related_pages/controller",
]


114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/jets/commands/call/autoload_guesser.rb', line 114

def autoload_paths
  guesses = []

  parts = underscored_name.split('_')
  parts.size.times do |i|
    namespace = i == 0 ? nil : parts[0..i-1].join('/')
    class_path = parts[i..-1].join('_')
    guesses << [namespace, class_path].compact.join('/')
  end

  guesses
end

#detect_class_nameObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jets/commands/call/autoload_guesser.rb', line 3

def detect_class_name
  # First guess the class name assuming simple
  class_name = simple_detect_class_name
  return class_name if class_name

  guess_classes.each do |class_name_guess|
    begin
      class_name_guess.constantize
      return class_name_guess # if there's no error then the class is found
    rescue NameError
      if out_of_guesses(class_name_guess)
        # puts "Unable to find the class to call. Tried guessing: #{guess_classes[0..-2].join(', ')}."
        # raise # re-raise NameError for now but maybe better to provide
          # a custom error class, so we can rescue it and provide a
          # friendly message to the user
      else
        next
      end
    end
  end

  nil
end

#error_messageObject

Useful to printing out what was attempted to look up



65
66
67
68
69
70
71
72
73
# File 'lib/jets/commands/call/autoload_guesser.rb', line 65

def error_message
  guesses = guess_classes
  puts "Unable to find the function to call."
  if class_name and !method_name
    puts @method_name_error
  else
    puts "Tried: #{guesses.join(', ')}"
  end
end

#guess_classesObject



127
128
129
# File 'lib/jets/commands/call/autoload_guesser.rb', line 127

def guess_classes
  autoload_paths.map(&:classify)
end

#method_nameObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jets/commands/call/autoload_guesser.rb', line 47

def method_name
  return @method_name if defined?(@method_name)
  return nil unless class_name

  underscored_class_name = class_name.underscore.gsub('/','_')
  underscored_function_name = @provided_function_name.underscore.gsub('/','_')
  meth = underscored_function_name.sub(underscored_class_name, '')
  meth = meth.sub(/^[-_]/,'') # remove leading _ or -

  if class_name.constantize.tasks.map(&:meth).include?(meth.to_sym)
    @method_name = meth
  else
    @method_name_error ="#{class_name} class found but #{meth} method not found"
    @method_name = nil
  end
end

#out_of_guesses(guess) ⇒ Object



131
132
133
# File 'lib/jets/commands/call/autoload_guesser.rb', line 131

def out_of_guesses(guess)
  guess.include?("::Controller")
end

#process_typeObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/jets/commands/call/autoload_guesser.rb', line 75

def process_type
  if @provided_function_name =~ /[-_]controller/
    "controller"
  elsif @provided_function_name =~ /[-_]job/
    "job"
  elsif @provided_function_name =~ /[-_]rule/
    "rule"
  else
    "function"
  end
end

#process_type_patternObject



87
88
89
# File 'lib/jets/commands/call/autoload_guesser.rb', line 87

def process_type_pattern
  Regexp.new("[-_]#{process_type}[-_](.*)")
end

#simple_detect_class_nameObject

Can guess name by looking at underscore and dashes.

Example:

@provided_function_name: first_module-lost_controller-index
return: FirstModule::LostController


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jets/commands/call/autoload_guesser.rb', line 34

def simple_detect_class_name
  parts = @provided_function_name.split('-')
  namespace = parts[0..-3].join('/')
  controller = parts[-2..-2] # dont include the action
  class_name = [namespace, controller].reject {|s| s.empty?}.compact.join('/').camelize
  begin
    class_name.constantize
    return class_name
  rescue NameError
    nil
  end
end

#underscored_nameObject

Strips the action because we dont want it to guess the class name So:

admin-related-pages => admin_related_pages_controller


94
95
96
97
98
99
100
101
102
# File 'lib/jets/commands/call/autoload_guesser.rb', line 94

def underscored_name
  # strip action and concidentally the _controller_ string
  name = @provided_function_name.sub(process_type_pattern,'')
  # Ensure _controller or _job at the end except for simple functions
  unless process_type == "function"
    name = name.gsub('-','_') + "_#{process_type}"
  end
  name
end