Closed smaximov closed 3 years ago
Nice observation @smaximov.
I wonder why Chat
is not a subclass of Base
? @atipugin is it on purpose or just a mistake?
I wonder why Chat is not a subclass of Base?
I believe that's because Chat
includes Virtus.model(finalize: false)
and Base
includes Virtus.model
(without finalize: false
).
@smaximov good point, thanks! Wonder how it worked for years ;)
@ivanovaleksey you say! It was your commit - https://github.com/atipugin/telegram-bot-ruby/commit/6d9264eb9bba3bc487b95408b0003655269e5f98 :)
Maybe it's worth to investigate why Chat
is different, fix it and make it a subclass of Base
again? I'm not familiar with Virtus but I can look into it this weekend.
@atipugin I don't remember why I extracted Comparable
module and deleted inheritable for Chat
class.
Maybe there was some kind of a circular dependency.
@ivanovaleksey Yes i think it's because Chat
depends on Message
and vice versa. finalize: false
defers constant resolving until we explicitly call Virtus.finalize
.
The current implementation of
Compactable#to_compact_hash
checks if a value inherits fromTypes::Base
, which unfortunately is not the case forTypes::Chat
. So when we call#to_compact_hash
for anyTypes::Base
object that recursively contains aTypes::Chat
object, all values except for the chat object are converted to hash.Steps to reproduce
Expected behavior
The whole update is recursively converted to hash:
Actual behavior
The following hash is printed (note that
:chat
is not converted to hash):Alternative
Since including a module inserts it into the inheritance chain, we could also check if the value is a subclass of
Types::Compactable
:But I settled on calling
#respond_to?
because it's considered more idiomatic.