Sha256: 5dba9db2df5494515851787a1f388f5ff28a11a5789c94ef0b2bf29d195288dc

Contents?: true

Size: 1.82 KB

Versions: 38

Compression:

Stored size: 1.82 KB

Contents

# Builds an anonymous class that represents a single Lambda function from code
# in app/functions.
#
# The build method returns an anonymous class using Class.new that contains
# the methods defined in the app/functions/hello.rb code.
#
# Ruby Class.new docs:
#
# Creates a new anonymous (unnamed) class with the given superclass (or
# Object if no parameter is given). You can give a class a name by
# assigning the class object to a constant.
# http://ruby-doc.org/core-2.1.1/Class.html#method-c-new
#
# So assigning the result of build to a constant makes for a prettier
# class name. Example:
#
#   constructor = FunctionConstructor.new(code_path)
#   HelloFunction = constructor.build
#
# The class name will be HelloFunction instead of (anonymous). Then usage would
# be:
#
#   hello_function = HelloFunction.new
#   hello_function(hello_function.handler, event, context)
#
# Or call with the process class method:
#
#   HelloFunction.process(event, context, "world")
module Jets::Lambda
  class FunctionConstructor
    def initialize(code_path)
      @code_path = full(code_path)
    end

    def full(path)
      path = "#{Jets.root}#{path}" unless path.include?(Jets.root.to_s)
      path
    end

    def build
      code = IO.read(@code_path)
      function_klass = Class.new(Jets::Lambda::Function)
      function_klass.module_eval(code)
      adjust_tasks(function_klass)
      function_klass # assign this to a Constant for a pretty class name
    end

    # For anonymous classes method_added during task registeration contains ""
    # for the class name.  We adjust it here.
    def adjust_tasks(klass)
      class_name = @code_path.sub(/.*app\/functions\//,'').sub(/\.rb$/, '')
      class_name = class_name.classify
      klass.tasks.each do |task|
        task.class_name = class_name
        task.type = "function"
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
jets-0.8.18 lib/jets/lambda/function_constructor.rb
jets-0.8.17 lib/jets/lambda/function_constructor.rb
jets-0.8.15 lib/jets/lambda/function_constructor.rb
jets-0.8.14 lib/jets/lambda/function_constructor.rb
jets-0.8.13 lib/jets/lambda/function_constructor.rb
jets-0.8.12 lib/jets/lambda/function_constructor.rb
jets-0.8.11 lib/jets/lambda/function_constructor.rb
jets-0.8.10 lib/jets/lambda/function_constructor.rb
jets-0.8.9 lib/jets/lambda/function_constructor.rb
jets-0.8.8 lib/jets/lambda/function_constructor.rb
jets-0.8.6 lib/jets/lambda/function_constructor.rb
jets-0.8.5 lib/jets/lambda/function_constructor.rb
jets-0.8.4 lib/jets/lambda/function_constructor.rb
jets-0.8.3 lib/jets/lambda/function_constructor.rb
jets-0.8.2 lib/jets/lambda/function_constructor.rb
jets-0.8.1 lib/jets/lambda/function_constructor.rb
jets-0.8.0 lib/jets/lambda/function_constructor.rb
jets-0.7.1 lib/jets/lambda/function_constructor.rb
jets-0.7.0 lib/jets/lambda/function_constructor.rb
jets-0.6.9 lib/jets/lambda/function_constructor.rb