I want to coerce a Str into a DBIx::Class::Row object for an attribute in my Moose class. To do this I need to perform a lookup on the DBIC schema to find the row. I want to push an error onto an ArrayRef attribute if the lookup failed.
I currently pass the schema in as an attribute to my class.
With coercion it appears I cannot access the object so I cannot push onto the error arrayref attribute or use the schema object to perform my lookup.
An alternative I tried was to use 'around' to lookup and set the attribute when set, but this of course does not get called when the attribute value is passed via the constructor.
Is this possible, or does someone have an alternative implementation to do what I want to achieve?
You can catch and mutate a value being stored when passed into a constructor with an attribute initializer. (However, it is only run when the attribute is set in the constructor, not at any other time.) Documentation for initializers can be found in Class::MOP::Attribute.
Since this only catches cases where the attribute is set via the constructor, you still need to catch the other cases where the attribute is set. This can be done with a method modifier as you said, but you can combine the two into one method, wrapped around the autogenerated accessor: