Skip to content

Commit

Permalink
Merge pull request #1 from thatch-health/olivier/callbacks
Browse files Browse the repository at this point in the history
Add support for callbacks
  • Loading branch information
olivier-thatch authored Jun 5, 2024
2 parents 156f69c + 08af7e6 commit f5f7a32
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## 0.0.2 (2024-06-05)

- Added support for callbacks (`before`, `after`, etc.)

## 0.0.1 (2024-06-04)

- First release
20 changes: 20 additions & 0 deletions lib/tapioca/dsl/compilers/grape_endpoints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class GrapeEndpoints < Tapioca::Dsl::Compiler
def decorate
create_classes_and_includes
create_routing_methods
create_callbacks_methods
end

class << self
Expand All @@ -84,6 +85,11 @@ def gather_constants
T::Array[Symbol],
)

CALLBACKS_METHODS = T.let(
[:before, :before_validation, :after_validation, :after, :finally].freeze,
T::Array[Symbol],
)

private

sig { returns(RBI::Scope) }
Expand Down Expand Up @@ -158,6 +164,20 @@ def create_routing_methods
return_type: "void",
)
end

sig { void }
def create_callbacks_methods
CALLBACKS_METHODS.each do |callback|
routing_methods_module.create_method(
callback.to_s,
parameters: [
create_rest_param("args", type: "T.untyped"),
create_block_param("blk", type: "T.nilable(T.proc.bind(#{EndpointClassName}).void)"),
],
return_type: "void",
)
end
end
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions spec/tapioca/dsl/compilers/grape_endpoints_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,24 @@ class TwitterAPI
extend GeneratedRoutingMethods
module GeneratedRoutingMethods
sig { params(args: T.untyped, blk: T.nilable(T.proc.bind(PrivateEndpoint).void)).void }
def after(*args, &blk); end
sig { params(args: T.untyped, blk: T.nilable(T.proc.bind(PrivateEndpoint).void)).void }
def after_validation(*args, &blk); end
sig { params(args: T.untyped, blk: T.nilable(T.proc.bind(PrivateEndpoint).void)).void }
def before(*args, &blk); end
sig { params(args: T.untyped, blk: T.nilable(T.proc.bind(PrivateEndpoint).void)).void }
def before_validation(*args, &blk); end
sig { params(args: T.untyped, blk: T.nilable(T.proc.bind(PrivateEndpoint).void)).void }
def delete(*args, &blk); end
sig { params(args: T.untyped, blk: T.nilable(T.proc.bind(PrivateEndpoint).void)).void }
def finally(*args, &blk); end
sig { params(args: T.untyped, blk: T.nilable(T.proc.bind(PrivateEndpoint).void)).void }
def get(*args, &blk); end
Expand Down

0 comments on commit f5f7a32

Please sign in to comment.