Skip to content

Commit

Permalink
Entity name setter method
Browse files Browse the repository at this point in the history
  • Loading branch information
olivier-thatch committed Jul 15, 2024
1 parent c137f5e commit f5fdb73
Show file tree
Hide file tree
Showing 15 changed files with 1,648 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ group :test do
gem "minitest-reporters", "~> 1.4"

gem "simplecov", require: false

gem "grape-entity", require: false
end
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ GEM
mustermann-grape (~> 1.1.0)
rack (>= 2)
zeitwerk
grape-entity (1.0.1)
activesupport (>= 3.0.0)
multi_json (>= 1.3.2)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
io-console (0.7.2)
Expand All @@ -67,6 +70,7 @@ GEM
builder
minitest (>= 5.0)
ruby-progressbar
multi_json (1.15.0)
mustermann (3.0.0)
ruby2_keywords (~> 0.0.1)
mustermann-grape (1.1.0)
Expand Down Expand Up @@ -169,6 +173,7 @@ PLATFORMS

DEPENDENCIES
bundler-audit
grape-entity
grape_sorbet!
irb
minitest (~> 5.0)
Expand Down
1 change: 1 addition & 0 deletions lib/grape_sorbet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ module GrapeSorbet
end

require_relative "grape_sorbet/version"
require_relative "grape_sorbet/grape_entity_name"
89 changes: 89 additions & 0 deletions lib/grape_sorbet/grape_entity_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# typed: strict
# frozen_string_literal: true

require "sorbet-runtime"

module GrapeSorbet
# This is a patch for Grape::Entity to make it easier to define custom entity names when using Sorbet.
#
# This patch defines a class setter `entity_name=` that can be used to define a custom entity name in a single line,
# while still working as expected with grape-swagger:
# ```
# class Link < Grape::Entity
# self.entity_name = "LinkedStatus"
# end
# ```
#
# Without this patch, you would have to define a custom entity name like this:
# ```
# class Link < Grape::Entity
# extend T::Sig
#
# sig { returns(String) }
# def self.entity_name
# "LinkedStatus"
# end
# end
# ```
#
# or even more verbose, if you're using the `Style/ClassMethodsDefinitions` Rubocop rule with
# `EnforcedStyle: self_class`:
# ```
# class Link < Grape::Entity
# class << self
# extend T::Sig
#
# sig { returns(String) }
# def entity_name
# "LinkedStatus"
# end
# end
# end
# ```
module GrapeEntityName
extend T::Sig

include Kernel

# Sets the entity name.
#
# Used by grape-swagger to define a custom name to use in the OpenAPI schema instead of generating it from the
# fully qualified class name.
#
# @param entity_name [String] The custom entity name.
sig { params(entity_name: String).returns(String) }
attr_writer :entity_name

# Returns the custom entity name if one has been set (using `entity_name=`), otherwise raises an error.
#
# @return [String] The custom entity name.
# @raise [StandardError] If no custom entity name has been set.
sig { returns(String) }
def entity_name
@entity_name = T.let(@entity_name, T.nilable(String))

return @entity_name unless @entity_name.nil?

raise "entity_name has not been set for #{self}, call `#{self}.entity_name = \"...\"` to set it"
end

sig { params(method_name: T.any(String, Symbol), include_all: T.untyped).returns(T::Boolean) }
def respond_to?(method_name, include_all = false)
# grape-swagger checks if the model class responds to `:entity_name`, so we need to return false if
# `@entity_name` is nil (meaning `entity_name=` was never called).
if method_name.to_sym == :entity_name
return !!(defined?(@entity_name) && !@entity_name.nil?)
end

super
end
end
end

begin
require "grape-entity"
rescue LoadError
return
else
Grape::Entity.extend(GrapeSorbet::GrapeEntityName)
end
17 changes: 17 additions & 0 deletions sorbet/rbi/dsl/grape/dsl/api.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions sorbet/rbi/dsl/grape/dsl/callbacks.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions sorbet/rbi/dsl/grape/dsl/helpers.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions sorbet/rbi/dsl/grape/dsl/middleware.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions sorbet/rbi/dsl/grape/dsl/request_response.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions sorbet/rbi/dsl/grape/dsl/routing.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions sorbet/rbi/dsl/grape/dsl/validations.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f5fdb73

Please sign in to comment.