Class: Tapioca::Dsl::Compilers::GrapeEndpoints

Inherits:
Tapioca::Dsl::Compiler
  • Object
show all
Extended by:
T::Sig
Includes:
Helpers::GrapeConstantsHelper
Defined in:
lib/tapioca/dsl/compilers/grape_endpoints.rb

Overview

‘Tapioca::Compilers::GrapeEndpoints` decorates RBI files for subclasses of `Grape::API::Instance`.

For example, with the following ‘Grape::API::Instance` subclass:

~~~rb class API < Grape::API::Instance

helpers Helpers

end ~~~

this compiler will produce the RBI file ‘api.rbi` with the following content: ~~~rbi # api.rbi # typed: true

class API

extend GeneratedRoutingMethods

module GeneratedRoutingMethods
  sig do
    params(
      args: T.untyped,
      options: T.untyped,
      block: T.nilable(T.proc.bind(PrivateEndpoint).void),
    )
    .void
  end
  def get(*args, **options, &block); end

  # ...

  sig do
    params(
      param: Symbol,
      requirements: T.untyped,
      type: T.untyped,
      options: T.untyped,
      block: T.nilable(T.proc.bind(T.class_of(PrivateAPIInstance)).void),
    ).void
  end
  def route_param(param, requirements: nil, type: nil, **options, &block); end
end

class PrivateAPIInstance < ::Grape::API::Instance
  extend GeneratedRoutingMethods
end

class PrivateEndpoint < ::Grape::Endpoint
  include Helpers
end

end ~~~

Constant Summary collapse

ConstantType =
type_member { { fixed: T.class_of(::Grape::API::Instance) } }
CALLBACKS_METHODS =
T.let(
  [:before, :before_validation, :after_validation, :after, :finally].freeze,
  T::Array[Symbol],
)
HTTP_VERB_METHODS =
T.let(
  [:get, :post, :put, :patch, :delete, :head, :options].freeze,
  T::Array[Symbol],
)
NAMESPACE_METHODS =
T.let(
  [:namespace, :group, :resource, :resources, :segment].freeze,
  T::Array[Symbol],
)

Constants included from Helpers::GrapeConstantsHelper

Helpers::GrapeConstantsHelper::APIInstanceClassName, Helpers::GrapeConstantsHelper::CallbacksMethodsModuleName, Helpers::GrapeConstantsHelper::EndpointClassName, Helpers::GrapeConstantsHelper::RequestResponseMethodsModuleName, Helpers::GrapeConstantsHelper::RoutingMethodsModuleName

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gather_constantsObject



95
96
97
# File 'lib/tapioca/dsl/compilers/grape_endpoints.rb', line 95

def gather_constants
  descendants_of(::Grape::API::Instance)
end

Instance Method Details

#decorateObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/tapioca/dsl/compilers/grape_endpoints.rb', line 78

def decorate
  api_class_name = constant.name
  return unless api_class_name

  root.create_class(api_class_name) do |api|
    create_classes_and_includes(api)

    create_callbacks_methods(api)
    create_request_response_methods(api)
    create_routing_methods(api)
  end
end