First of all, this question is related but not solved by
Then about the question itself : when programming in Java with IDEs like Eclipse, it's possible to have a warning when we implement a switch statement on an enum, and we forgot some cases in the switch statement (very useful after adding an extra possible value to the enum and we forget to edit all switches based on this enum)
Is it possible to have the same kind of static analysis in Ruby ? Is there a way to implement enums so that we'd get a warning (maybe after running rubocop or something) if we forget to implement a case ?
EDIT
This "enum" I'm talking about could be any type of Set
like object with a finite number of values, the most simplest form being an array of symbols, but maybe it is not enough/convenient to perform analysis with it hence why I am starting this question
On of my use case involve checking all possible errors after performing Policy checks
class CanShowArticlePolicy
def call
list_of_exceptions = [:unpublished, :deleted,
:offensive_content_detected]
# business logic that returns either true or false and add exception information exception, can be mocked as
@error = list_of_exceptions.sample
false
end
end
# in another file like a controller or service
article = Article.find(id)
policy = CanShowArticlePolicy.new(article)
if policy.call
render_article
else
# Where I'm trying to be exhaustive
case policy.error # <== Goal : detect here we are swithing on an "enum" with finite values and we should be exhaustive
when :unpublished
render_unpublished_error
when :deleted
render_gone
# <<= Here I would like to get a rubocop error because we've forgotten to handle the `:offensive_content_detected` case
end
Maybe a solution would be to have instead something like an annotation
case enum_value # @exhaustive-case with ::CanShowArticlePolicy::ErrorEnum
and the annotation would have for effect of the static analysis trying to find a ::CanShowArticlePolicy::ErrorEnum
array containing the symbols, and making sure there are as many when
statements as number of items in the frozen ErrorEnum