in reply to Re^3: Moose: Where to define a method required by an attribute trait?
in thread Moose: Where to define a method required by an attribute trait?
My real code is quite longer/different, so I thought it would be better to post an example here.
My reasons: I found myself typing too much code each time I needed a special configuration class that represents a text config file and has functions to parse, validate (and in reverse also serialize) config entries.
So the idea was to define different config entry types ("file", "ip address", "email address") as traits containing a parser and a serializer function.
In the class representing the config file then I will only have to enrich the attributes with my traits to have a project specific config parser/writer:
has alarmMailsTo => ( traits => ['ConfigEmail'], # trait for type "email" conf_key => 'ALARM_MAILS_TO' # associate with a config key isa => 'ArrayRef[Str]', is => 'rw', );
A problem occurs when a trait needs some runtime information in it's parser/validator method. The problem and the workaround I have found by now is as follows:
Class that represents config file
.. with My::Config; ## some runtime information, eventually for the trait's method has validDomain => ( isa => 'Str', is => 'rw', ); has alarmMailsTo => ( traits => ['ConfigEmail'], conf_key => 'ALARM_MAILS_TO' attr_valid_domain => 'validDomain' ## give attr name to trait isa => 'ArrayRef[Str]', is => 'rw', ); ..
Class role
.. role My::Config { method load() { .. ## $attr is a Moose::Meta::Attribute $attr->parse($self, $configString); .. } .. }
Attribute Trait
.. role My::Config::ConfigEmail { has attr_valid_domain => ( isa => 'Str', is => 'rw', required => 1, ); method parse($instance!, $value!) { .. $instance ->meta ->find_attribute_by_name($self->attr_valid_domain) ->get_value($instance); .. } .. }
(Btw. I actually have a "base trait" that contains the parse() method and all "type traits" consume it)
It works, but passing an attribute name to the trait and then passing an instance around seems not very elegant to me. Is there any other way to access the class instance from within an attribute trait method?
Thanks a lot for your help so far...
-maxhq
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Moose: Where to define a method required by an attribute trait?
by stvn (Monsignor) on Apr 30, 2010 at 19:31 UTC | |
by maxhq (Novice) on May 04, 2010 at 09:38 UTC |