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, #initialize

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",
]


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

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
# File 'lib/jets/commands/call/autoload_guesser.rb', line 3

def detect_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



41
42
43
44
45
46
47
48
49
# File 'lib/jets/commands/call/autoload_guesser.rb', line 41

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



103
104
105
# File 'lib/jets/commands/call/autoload_guesser.rb', line 103

def guess_classes
  autoload_paths.map(&:classify)
end

#method_nameObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/jets/commands/call/autoload_guesser.rb', line 23

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



107
108
109
# File 'lib/jets/commands/call/autoload_guesser.rb', line 107

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

#process_typeObject



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

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



63
64
65
# File 'lib/jets/commands/call/autoload_guesser.rb', line 63

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

#underscored_nameObject

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

admin-related-pages => admin_related_pages_controller


70
71
72
73
74
75
76
77
78
# File 'lib/jets/commands/call/autoload_guesser.rb', line 70

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