Re: Prototype Attributes

[email protected] (Sartak)
Newsgroups perl.moose
Message-ID <[email protected]>
On Wed, Dec 17, 2008 at 3:07 AM, Christopher Brown
<[email protected]> wrote:
> package MooseX::Attribute::DateTime;
>
>    has 'datetime' => (
>        is      => 'rw'     ,
>        isa     => 'DateTime' ,
>        coerce  => 1 ,
>        metaclass   => 'MooseX::Getopt::Meta::Attribute',
>        cmd_aliases   => 'time'
>    );

I hope that the Getopt options here wouldn't actually be in the
generic MooseX::Attribute::DateTime!

> package myApp;
>    use Moose;
>    with 'MooseX::Attribute::DateTime';
>
>    has 'begin' => ( prototype => 'datetime', cmd_aliases => 'begin' );
>    has 'end'   => ( prototype => 'datetime', cmd_aliases => 'end' );
>    ...

I don't think overriding "has" is the right solution. How's this? I
rather like it:

package myApp;
use Moose;
use MooseX::Attribute::DateTime;

has_date begin => (cmd_aliases => 'begin');
has_date end => (cmd_aliases => 'end');

"has_date" could be configurable, you just pass the keyword you want
to "use MooseX::Attribute::DateTime". Alternatively, you could go even
further and allow extra parameters to be specified in the "use" line,
such as:

use MooseX::Attribute::DateTime (
    'has_date',
    needs_date => {
        required => 1,
    },
);

needs_date begin => (cmd_aliases => 'begin');
has_date end => (cmd_aliases => 'end');

Data::OptList (already used in several places in Moose as you know)
would make this trivial.

Shawn
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.