Module: GrapeSorbet::GrapeEntityName

Extended by:
T::Sig
Includes:
Kernel
Defined in:
lib/grape_sorbet/grape_entity_name.rb

Overview

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 “‘

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#entity_nameObject



62
63
64
65
66
67
68
# File 'lib/grape_sorbet/grape_entity_name.rb', line 62

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

Instance Method Details

#respond_to?(method_name, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
79
# File 'lib/grape_sorbet/grape_entity_name.rb', line 71

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