Class: Jets::Lambda::Definition
- Inherits:
-
Object
- Object
- Jets::Lambda::Definition
- Includes:
- Util::Camelize
- Defined in:
- lib/jets/lambda/definition.rb
Constant Summary collapse
- @@lang_exts =
{ ruby: ".rb", python: ".py", node: ".js" }
Instance Attribute Summary collapse
-
#associated_resources ⇒ Object
readonly
Returns the value of attribute associated_resources.
-
#class_name ⇒ Object
Returns the value of attribute class_name.
-
#iam_policy ⇒ Object
readonly
Returns the value of attribute iam_policy.
-
#lang ⇒ Object
readonly
Returns the value of attribute lang.
-
#managed_iam_policy ⇒ Object
readonly
Returns the value of attribute managed_iam_policy.
-
#meth ⇒ Object
readonly
Returns the value of attribute meth.
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
-
#provisioned_concurrency ⇒ Object
readonly
Returns the value of attribute provisioned_concurrency.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #base_replacements ⇒ Object
- #build_function_iam? ⇒ Boolean
- #full_handler(handler_function) ⇒ Object
-
#get_type ⇒ Object
The get_type method works for controller and event classes.
- #handler_base ⇒ Object
- #handler_path ⇒ Object
-
#initialize(class_name, meth, options = {}) ⇒ Definition
constructor
A new instance of Definition.
- #lang_ext ⇒ Object
- #name ⇒ Object
- #namespace ⇒ Object
- #poly_src_path ⇒ Object
- #public_meth? ⇒ Boolean
- #replacements ⇒ Object
Methods included from Util::Camelize
Constructor Details
#initialize(class_name, meth, options = {}) ⇒ Definition
Returns a new instance of Definition.
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/jets/lambda/definition.rb', line 10 def initialize(class_name, meth, = {}) @class_name = class_name.to_s @meth = meth @options = @type = [:type] || get_type # controller, event, or function @properties = camelize([:properties]) || {} @provisioned_concurrency = [:provisioned_concurrency] @iam_policy = [:iam_policy] @managed_iam_policy = [:managed_iam_policy] @lang = [:lang] || :ruby @associated_resources = [:associated_resources] || {} @replacements = [:replacements] || {} # added to baseline replacements end |
Instance Attribute Details
#associated_resources ⇒ Object (readonly)
Returns the value of attribute associated_resources.
6 7 8 |
# File 'lib/jets/lambda/definition.rb', line 6 def associated_resources @associated_resources end |
#class_name ⇒ Object
Returns the value of attribute class_name.
5 6 7 |
# File 'lib/jets/lambda/definition.rb', line 5 def class_name @class_name end |
#iam_policy ⇒ Object (readonly)
Returns the value of attribute iam_policy.
6 7 8 |
# File 'lib/jets/lambda/definition.rb', line 6 def iam_policy @iam_policy end |
#lang ⇒ Object (readonly)
Returns the value of attribute lang.
6 7 8 |
# File 'lib/jets/lambda/definition.rb', line 6 def lang @lang end |
#managed_iam_policy ⇒ Object (readonly)
Returns the value of attribute managed_iam_policy.
6 7 8 |
# File 'lib/jets/lambda/definition.rb', line 6 def managed_iam_policy @managed_iam_policy end |
#meth ⇒ Object (readonly)
Returns the value of attribute meth.
6 7 8 |
# File 'lib/jets/lambda/definition.rb', line 6 def meth @meth end |
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
6 7 8 |
# File 'lib/jets/lambda/definition.rb', line 6 def properties @properties end |
#provisioned_concurrency ⇒ Object (readonly)
Returns the value of attribute provisioned_concurrency.
6 7 8 |
# File 'lib/jets/lambda/definition.rb', line 6 def provisioned_concurrency @provisioned_concurrency end |
#type ⇒ Object
Returns the value of attribute type.
5 6 7 |
# File 'lib/jets/lambda/definition.rb', line 5 def type @type end |
Instance Method Details
#base_replacements ⇒ Object
112 113 114 |
# File 'lib/jets/lambda/definition.rb', line 112 def base_replacements {namespace: namespace} end |
#build_function_iam? ⇒ Boolean
42 43 44 |
# File 'lib/jets/lambda/definition.rb', line 42 def build_function_iam? !!(@iam_policy || @managed_iam_policy) end |
#full_handler(handler_function) ⇒ Object
89 90 91 |
# File 'lib/jets/lambda/definition.rb', line 89 def full_handler(handler_function) "#{handler_base}.#{handler_function}" end |
#get_type ⇒ Object
The get_type method works for controller and event classes.
Usually able to get the type from the class name. Examples:
PostsController => controller
CoolEvent => event
However, for function types, we are not able to get the type for multiple of reasons. First, function types are allowed to be named with or without _function. Examples:
path => class => type
app/functions/hello.rb => Hello => function
app/functions/hello_function.rb => HelloFunction => function
The second reason is that functions are not regular ruby classes. Instead they are anonymous classes created with Class.new. When classes are created with Class.new the method_added hook has “” (blank string) as the self class name. We add the class_type to the task later on as we are constructing the class as part of the Class.new logic.
For controller and event standard ruby classes though it can easily be determinated as part of initialization. So we get the type for convenience then.
For anonymous function classes, we just set to nil and will later fix in FunctionConstructor.
Returns: “controller”, “event” or nil
83 84 85 86 87 |
# File 'lib/jets/lambda/definition.rb', line 83 def get_type unless @class_name.empty? # when anonymous class is created with Class.new @class_name.underscore.split("_").last # controller, event or rule end end |
#handler_base ⇒ Object
97 98 99 100 101 |
# File 'lib/jets/lambda/definition.rb', line 97 def handler_base base = "handlers/#{@type.pluralize}/#{@class_name.underscore}" base += "/#{@lang}" if @lang != :ruby base += "/#{@meth}" end |
#handler_path ⇒ Object
93 94 95 |
# File 'lib/jets/lambda/definition.rb', line 93 def handler_path "#{handler_base}#{lang_ext}" end |
#lang_ext ⇒ Object
51 52 53 |
# File 'lib/jets/lambda/definition.rb', line 51 def lang_ext @@lang_exts[@lang] end |
#name ⇒ Object
24 25 26 |
# File 'lib/jets/lambda/definition.rb', line 24 def name @meth end |
#namespace ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/jets/lambda/definition.rb', line 116 def namespace if @meth @meth.to_s.camelize else @class_name.gsub("::", "").camelize end end |
#poly_src_path ⇒ Object
103 104 105 |
# File 'lib/jets/lambda/definition.rb', line 103 def poly_src_path handler_path.sub("handlers/", "app/") end |
#public_meth? ⇒ Boolean
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/jets/lambda/definition.rb', line 28 def public_meth? # For anonymous classes (app/functions/hello.rb) the class name will be blank. # These types of classes are treated specially and has only one handler method # that is registered. So we know it is public. return true if @class_name.nil? || @class_name == "" # Consider all polymorphic methods public for now return true if @lang != :ruby klass = @class_name.constantize public_methods = klass.public_instance_methods public_methods.include?(meth.to_sym) end |
#replacements ⇒ Object
107 108 109 110 |
# File 'lib/jets/lambda/definition.rb', line 107 def replacements # Merge in the custom replacements specific to each app class: ConfigRule, Job, etc. base_replacements.merge(@replacements) end |