2

I'm trying to apply typing to a repo that uses the Sequel gem. It's got a non-standard Ruby syntax for a particular use case: initializing a model when a table name doesn't follow a convention

Example:

# This is necessary to get around
# "Superclasses must only contain constant literals"
FooParent = Sequel::Model(:foo_bars)

class Foo < FooParent
end

I've been able to tell Sorbet that certain attributes exist on my Foo model with this code in sorbet/rbi/shims/foo.rbi


# Raises an error:
# "Redefining constant `FooModelParent`"
class FooParent < Sequel::Model
end

This makes errors such as "Method join does not exist on T.class_of(Foo)7003" go away, but I'm not sure how to get rid of this "Redefining Constant" error or if there's a better way. Can someone help me get rid of this error, or solve the inheritance typing problem a different way?

Alexander
  • 59,041
  • 12
  • 98
  • 151
Eric Hu
  • 18,048
  • 9
  • 51
  • 67

1 Answers1

2

One way to work around this is to use the Sequel::Model.set_dataset method documented here.

Ex.

class Foo < Sequel::Model
  set_dataset :foo_bars
end

This is all Sequel::Model() does as seen here. This can still cause issues with Sequel's table validity checks, which can be fixed by calling Sequel::Model.require_valid_table = false in your Sequel initializer.

Thanks to jez on the Sorbet team for pointing this fix out!