MooseX::TypeMap

Two ago I began writing the beginnings of what was to be the first Cantella::Data::Tabular renderer class. The idea was to render a Cantella::Data::Tabular::Table object into a plain text table. I failed miserably. Within 5 seconds I got wrapped up on issues of formatting and how to render data and data-types. Eventually I resorted to #moose for ideas and rafl pointed me towards some code of his. We agreed that it would be mutually beneficial to use if I got to use his code as long as I separated it from its original package, and packaged it separately so it could stand alone.

The idea behind MooseX::TypeMap is simple: it holds a series of "Entry" objects which have type_constraint and data attributes. You can then give MooseX::TypeMap an instance of a Moose::Meta::TypeConstraint and it will give you back the data associated with that type-constraint or, optionally, it's closest super-type that TypeMap knows. That's it. It's simple and it's short and it's hardly very exciting, but it will make my life easier.

Take, for example, a Cantella::Data::Tabular renderer, which needs to render different cells based on their associated type-constraint. Different people may want to have different rendering rules, and a good way to implement the conversion would be using MooseX::TypeMap to automatically fetch a closure or printf format based on the cell's type. A sane default could be shipped with the formatter and the user could either modify it or replace it to match his/her needs in only a couple of lines of code.

2 Comments

Thank you for blogging on this! :)

When will it be available on CPAN?

Leave a comment

About Guillermo Roditi

user-pic I blog about Perl.