Sha256: 12017390ca3c64da3e6d88a49f3b5e0cadf39011d2c9efac72ad4a37f0b7b7d8

Contents?: true

Size: 1.98 KB

Versions: 24

Compression:

Stored size: 1.98 KB

Contents

# Jets::Builders::ShimVars::Shared.new(path)
#
#   @vars.functions.each do |function_name|
#     @vars.handler_for(function_name)
#   end
#
# Implements:
#
#   functions
#   handler_for(function_name)
#   js_path
#
module Jets::Builders::ShimVars
  class App < Base
    # Allow user to specify relative or full path. The right path gets used
    # internally. Example paths:
    #   app/controllers/posts_controller.rb
    #   app/jobs/hard_job.rb
    #   /tmp/jets/build/app_root/app/jobs/hard_job.rb
    #   /tmp/jets/build/app_root/app/functions/hello.rb
    def initialize(path)
      @full_path = full(path)
      @relative_path = relative(path)
    end

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

    def relative(path)
      full_path = full(path)
      full_path.sub(Jets.root.to_s, "")
               .sub(/.*internal\/app/, "app")

    end

    # process_type is key, it will either "controller" or "job".
    # It is used to deduce klass, etc.
    # We get the process_type from the path.
    # Example paths:
    #   app/controllers/posts_controller.rb
    #   app/jobs/hard_job.rb
    def process_type
      @relative_path.split('/')[1].singularize # controller or job
    end

    # Returns the public methods of the child_class.
    # Example: [:create, :update]
    def functions
      klass.lambda_functions
    end

    # Examples: PostsController, HardJob, Hello, HelloFunction
    def klass
      @klass ||= Jets::Klass.from_path(@relative_path)
    end

    def lang(meth)
      klass.tasks.find
    end

    # This gets called in the node shim js template
    def handler_for(meth)
      # possibly not include _function
      underscored_name = @relative_path.sub(%r{app/(\w+)/},'').sub('.rb','')
      "handlers/#{process_type.pluralize}/#{underscored_name}.#{meth}"
    end

    # Example return: "handlers/controllers/posts.js"
    def js_path
      @relative_path
        .sub("app", "handlers")
        .sub(/\.rb$/, ".js")
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
jets-1.1.5 lib/jets/builders/shim_vars/app.rb
jets-1.1.4 lib/jets/builders/shim_vars/app.rb
jets-1.1.3 lib/jets/builders/shim_vars/app.rb
jets-1.1.2 lib/jets/builders/shim_vars/app.rb
jets-1.1.1 lib/jets/builders/shim_vars/app.rb
jets-1.1.0 lib/jets/builders/shim_vars/app.rb
jets-1.0.18 lib/jets/builders/shim_vars/app.rb
jets-1.0.17 lib/jets/builders/shim_vars/app.rb
jets-1.0.16 lib/jets/builders/shim_vars/app.rb
jets-1.0.15 lib/jets/builders/shim_vars/app.rb
jets-1.0.13 lib/jets/builders/shim_vars/app.rb
jets-1.0.12 lib/jets/builders/shim_vars/app.rb
jets-1.0.11 lib/jets/builders/shim_vars/app.rb
jets-1.0.10 lib/jets/builders/shim_vars/app.rb
jets-1.0.9 lib/jets/builders/shim_vars/app.rb
jets-1.0.8 lib/jets/builders/shim_vars/app.rb
jets-1.0.7 lib/jets/builders/shim_vars/app.rb
jets-1.0.6 lib/jets/builders/shim_vars/app.rb
jets-1.0.5 lib/jets/builders/shim_vars/app.rb
jets-1.0.4 lib/jets/builders/shim_vars/app.rb