Class: Jets::Commands::Call::AnonymousGuesser

Inherits:
BaseGuesser
  • Object
show all
Defined in:
lib/jets/commands/call/anonymous_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

#detect_class_nameObject



3
4
5
6
7
8
9
10
# File 'lib/jets/commands/call/anonymous_guesser.rb', line 3

def detect_class_name
  found_path = function_paths.find do |path|
    File.exist?("#{Jets.root}/#{path}")
  end

  klass = Jets::Klass.from_path(found_path) if found_path
  klass.to_s
end

#error_messageObject

Useful to printing out what was attempted to look up



28
29
30
31
32
33
34
35
36
# File 'lib/jets/commands/call/anonymous_guesser.rb', line 28

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

#function_filenames(meth = nil, primary_namespace = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/jets/commands/call/anonymous_guesser.rb', line 38

def function_filenames(meth=nil, primary_namespace=nil)
  guesses = []
  parts = meth.split('_')

  if primary_namespace.nil?
    guesses << meth

    if parts.size == 1 # already on final_primary_namespace
      return guesses # end of recursion
    else
      next_primary_namespace =  parts.first
      guesses += function_filenames(meth, next_primary_namespace) # start of recursion
      return guesses # return early
    end
  end

  next_meth = meth.sub("#{primary_namespace}_", '')
  next_parts = next_meth.split('_')

  # Takes the next_parts and creates guesses with the parts joined by '/'
  # with the primary_namespace prepended.  So if next_parts is
  # ["long", "name", "function"] and primary_namespace is "complex"
  #
  # guesses that get added:
  #
  #   [
  #     "complex/long_name_function",
  #     "complex/long/name_function",
  #     "complex/long/name/function",
  #   ]
  n = next_parts.size + 1
  next_parts.size.times do |i|
    namespace = i == 0 ? nil : next_parts[0..i-1].join('/')
    class_path = next_parts[i..-1].join('_')
    guesses << [primary_namespace, namespace, class_path].compact.join('/')
  end

  final_primary_namespace = parts[0..-2].join('_')
  if primary_namespace == final_primary_namespace
    return guesses # end of recursion
  else
    namespace_size = parts.size - next_parts.size
    next_primary_namespace = parts[0..namespace_size].join('_')
    guesses += function_filenames(meth, next_primary_namespace)
    return guesses
  end
end

#function_pathsObject



86
87
88
89
90
91
92
93
# File 'lib/jets/commands/call/anonymous_guesser.rb', line 86

def function_paths
  # drop the last word for starting filename
  starting_filename = @provided_function_name.underscore.split('_')[0..-2].join('_')
  filenames = function_filenames(starting_filename)
  filenames.map do |name|
    "app/functions/#{name}.rb"
  end
end

#method_nameObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jets/commands/call/anonymous_guesser.rb', line 12

def method_name
  return @method_name if defined?(@method_name)

  full_function_name = @provided_function_name.underscore
  underscored_class_name = class_name.underscore
  meth = full_function_name.sub("#{underscored_class_name}_","")

  if meth == class_name.constantize.handler.to_s
    @method_name = meth
  else
    @method_name_error = "#{class_name} class found but #{meth} method not found"
    @method_name = nil
  end
end