Class: Jets::Stack::Function

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/jets/stack/function.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ Function

Returns a new instance of Function.



6
7
8
# File 'lib/jets/stack/function.rb', line 6

def initialize(template)
  @template = template
end

Instance Attribute Details

#templateObject (readonly)

Returns the value of attribute template.



5
6
7
# File 'lib/jets/stack/function.rb', line 5

def template
  @template
end

Instance Method Details

#base_search_expressionObject



50
51
52
53
54
55
# File 'lib/jets/stack/function.rb', line 50

def base_search_expression
  attributes = @template.values.first
  handler = attributes['Properties']['Handler']
  handler.split('.')[0..-2].join('.') + '.*' # search_expression
  # Example: handlers/shared/functions/jets/s3_bucket_config.*
end

#handler_destObject

Relative path app/shared/functions/kevin.py => handlers/shared/functions/kevin.py



64
65
66
67
# File 'lib/jets/stack/function.rb', line 64

def handler_dest
  return unless source_file
  source_file.sub(%r{.*/app/}, "handlers/")
end

#internal?Boolean

Internal flag is mainly used to disable WARN messages

Returns:

  • (Boolean)


58
59
60
# File 'lib/jets/stack/function.rb', line 58

def internal?
  !!Dir.glob(internal_search_expression).first
end

#internal_search_expressionObject



45
46
47
48
# File 'lib/jets/stack/function.rb', line 45

def internal_search_expression
  internal = File.expand_path("../internal", File.dirname(__FILE__))
  base_search_expression.sub('handlers/shared/', "#{internal}/app/shared/")
end

#langObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jets/stack/function.rb', line 16

def lang
  return if internal?

  if source_file
    # Detect language from file extension
    ext = File.extname(source_file).sub(/^\./,'').to_sym
    lang_map[ext]
  else
    puts "WARN: Unable to find a source file for function. Looked at: #{search_expression}".color(:yellow)
  end
end

#lang_mapObject



28
29
30
31
32
33
34
# File 'lib/jets/stack/function.rb', line 28

def lang_map
  {
    rb: :ruby,
    py: :python,
    js: :node,
  }
end

#methObject



10
11
12
13
14
# File 'lib/jets/stack/function.rb', line 10

def meth
  attributes = @template.values.first
  handler = attributes['Properties']['Handler']
  handler.split('.').last
end

#search_expressionObject



41
42
43
# File 'lib/jets/stack/function.rb', line 41

def search_expression
  base_search_expression.sub('handlers/shared/', "#{Jets.root}/app/shared/")
end

#source_fileObject



36
37
38
# File 'lib/jets/stack/function.rb', line 36

def source_file
  Dir.glob(search_expression).first
end