The previous ~60 lines might be a candidate for metaprogramming. I'm usually careful with `define_method`, but this is a lot of duplication.