Class: Jets::Resource::ChildStack::AppClass

Inherits:
Base
  • Object
show all
Defined in:
lib/jets/resource/child_stack/app_class.rb

Direct Known Subclasses

Shared

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#outputs, #template_url

Methods inherited from Base

#replacements, #resource

Constructor Details

#initialize(s3_bucket, options = {}) ⇒ AppClass

Returns a new instance of AppClass.



8
9
10
11
# File 'lib/jets/resource/child_stack/app_class.rb', line 8

def initialize(s3_bucket, options={})
  super
  @path = options[:path]
end

Class Method Details

.common_parametersObject



59
60
61
62
63
64
65
66
# File 'lib/jets/resource/child_stack/app_class.rb', line 59

def self.common_parameters
  parameters = {
    IamRole: "!GetAtt IamRole.Arn",
    S3Bucket: "!Ref S3Bucket",
  }
  parameters[:GemLayer] = "!Ref GemLayer" unless Jets.poly_only?
  parameters
end

Instance Method Details

#app_logical_idObject

map the path to a camelized logical_id. Example:

/tmp/jets/demo/templates/demo-dev-2-posts_controller.yml to
PostsController


102
103
104
105
106
# File 'lib/jets/resource/child_stack/app_class.rb', line 102

def app_logical_id
  regexp = Regexp.new(".*#{Jets.config.project_namespace}-app-")
  controller_name = @path.sub(regexp, '').sub('.yml', '')
  controller_name.underscore.camelize
end

#controller?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/jets/resource/child_stack/app_class.rb', line 81

def controller?
  @path.include?('_controller.yml')
end

#controller_paramsObject



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/jets/resource/child_stack/app_class.rb', line 68

def controller_params
  return {} if Jets::Router.routes.empty?

  params = {
    RestApi: "!GetAtt ApiGateway.Outputs.RestApi",
  }
  scoped_routes.each do |route|
    resource = Jets::Resource::ApiGateway::Resource.new(route.path)
    params[resource.logical_id] = "!GetAtt ApiGateway.Outputs.#{resource.logical_id}"
  end
  params
end

#current_app_classObject



91
92
93
94
95
96
97
# File 'lib/jets/resource/child_stack/app_class.rb', line 91

def current_app_class
  templates_prefix = "#{Jets::Naming.template_path_prefix}-app-"
  @path.sub(templates_prefix, '')
    .sub(/\.yml$/,'')
    .gsub('-','/')
    .classify
end

#definitionObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jets/resource/child_stack/app_class.rb', line 13

def definition
  logical_id = app_logical_id
  defintion = {
    logical_id => {
      type: "AWS::CloudFormation::Stack",
      properties: {
        template_url: template_url,
        parameters: parameters,
      }
    }
  }
  defintion[logical_id][:depends_on] = depends_on if depends_on
  defintion
end

#dependency_outputs(dependency) ⇒ Object



48
49
50
# File 'lib/jets/resource/child_stack/app_class.rb', line 48

def dependency_outputs(dependency)
  dependency.to_s.classify.constantize.output_keys
end

#depends_onObject



28
29
30
31
32
33
34
35
# File 'lib/jets/resource/child_stack/app_class.rb', line 28

def depends_on
  klass = current_app_class.constantize
  return unless klass.depends_on

  klass.depends_on.map do |shared_stack|
    shared_stack.to_s.camelize # logical_id
  end
end

#depends_on_paramsObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/jets/resource/child_stack/app_class.rb', line 37

def depends_on_params
  params = {}
  depends_on.each do |dependency|
    dependency_outputs(dependency).each do |output|
      dependency_class = dependency.to_s.classify
      params[output] = "!GetAtt #{dependency_class}.Outputs.#{output}"
    end
  end
  params
end

#parametersObject



52
53
54
55
56
57
# File 'lib/jets/resource/child_stack/app_class.rb', line 52

def parameters
  common = self.class.common_parameters
  common.merge!(controller_params) if controller?
  common.merge!(depends_on_params) if depends_on
  common
end

#scoped_routesObject



85
86
87
88
89
# File 'lib/jets/resource/child_stack/app_class.rb', line 85

def scoped_routes
  @routes ||= Jets::Router.routes.select do |route|
    route.controller_name == current_app_class
  end
end

#template_filenameObject



108
109
110
# File 'lib/jets/resource/child_stack/app_class.rb', line 108

def template_filename
  @path
end