Re: Moose::Meta::Role::rename_attribute

[email protected] (Matt S Trout)
Newsgroups perl.moose
Message-ID <[email protected]>
On Thu, Sep 25, 2008 at 12:08:31AM -0700, Christopher Brown wrote:
> For instance, I can use *has "+..."* to change the properties of
> attributes.  I can use *traits* to change the properties.  And I can use
> roles to make reusable attributes.  But there does not seem to handle
> generic attributes.  And rename_attribute (and other functions) seems like
> one allow generics and seems like missing functionality.

Er. rename_attributes as you described it is completely irrelevant to this
problem. You'd've done better to start with the -actual- problem, which I'll
speak to below.
 
> I am pretty tired of writing  attribute specifications.   All my attributes
> are essentially the same.  They are dates, they are filenames, they are
> strings, they are names, they are directories.  My perl programs are
> littered with *has* *=>* definitions.  That are often repeated.

Moose does already support

has [ qw(...) ] => (...);

as an alias for

has $_ => (...) for qw(...);

> The good
> programmer in me says that I should put these reusable codes in modules and
> release them on CPAN. My hopes is to put these common attributes into a
> Moose Role and use them with some construct like:
> 
> with Attributes( date,  name );
> 
> The catch is that the attributes, though providing the same functionality
> based on the Type, do not always have the same name.  I often want an API to
> have attributes with different names.  "Date" might be "begin_date" or
> "name" might be "last_name" for instance.

So what you want is for the method aliasing stuff that already exists in
roles to be extended so you can alias attributes on application as well?

Now -that- seems like a useful feature, though I don't think it's actually
what you want. Stevan, Dave, thoughts?

> But I much rather say something like
> 
>     with Attributes( infile => "file", outfile=> "file" )

This smells like either the above, or parameterised roles. However ... 
 
> I suppose one solution would be to create a module that returns the
> definition of the attribute rather than the attribute itself
> 
>     use MooseX::Common::Attributes;
>     my $mca = MooseX::Common::Attributes->new;
>     has qw(infile, outfile) => $mcs->file;
> 
> But that gets uglier.

Well, only because you intentionally made it ugly.

Instead, I think what you really want it one of

(1) export attribute declarators, so -

use MooseX::Attributes::File qw(has_file);

has_file 'infile';
has_file 'outfile';

(2) an attribute metaclass that provides what you want as defaults

has 'infile' => (metaclass => 'File');
has 'outfile' => (metaclass => 'File');

-- 
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director                    http://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Want a managed development or deployment platform?
http://chainsawblues.vox.com/            http://www.shadowcat.co.uk/servers/
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.