Skip Menu |
 

This queue is for tickets about the Moose CPAN distribution.

Report information
The Basics
Id: 79673
Status: rejected
Priority: 0/
Queue: Moose

People
Owner: Nobody in particular
Requestors: DAMI [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: supply attribute name to builder methods
Download (untitled) / with headers
text/plain 595b
Sometimes several builder methods have a common behaviour (for example fetching a default value from some config); so the same builder method could serve for several attributes, it just needs to know which attribute is being built. Therefore I suggest that builder methods receive the attribute name as argument. By the way, the same could apply to 'default'. Example: has 'aaa' => (is => 'ro', builder => '_from_config'); has 'bbb' => (is => 'ro', builder => '_from_config'); sub _from_config { my ($self, $attribute_name) = @_; my $config = ...; return $config->{$attribute_name}; }
Download (untitled) / with headers
text/plain 175b
I vote: sub _from_config { my ($self, $meta_attribute) = @_; my $attribute_name = $meta_attribute->name; my $config = ...; return $config->{$attribute_name}; }
Subject: Re: [rt.cpan.org #79673] supply attribute name to builder methods
Date: Mon, 12 Nov 2012 10:27:28 -0600
To: Toby Inkster via RT <bug-Moose [...] rt.cpan.org>
From: Jesse Luehrs <doy [...] tozt.net>
Download (untitled) / with headers
text/plain 484b
On Mon, Nov 12, 2012 at 11:24:17AM -0500, Toby Inkster via RT wrote: Show quoted text
> Queue: Moose > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=79673 > > > I vote: > > sub _from_config { > my ($self, $meta_attribute) = @_; > my $attribute_name = $meta_attribute->name; > my $config = ...; > return $config->{$attribute_name}; > }
No, metaobjects shouldn't be provided by the API unless explicitly asked for (via find_meta or the 'meta' method or whatever). -doy
Meh. Actually, for inlining purposes, the attribute name is probably easier.
Download (untitled) / with headers
text/plain 1.2k
Meh. On further thought, consider this: { package Person; has formal_name => ( is => 'ro', isa => 'Str', ); has informal_name => ( is => 'ro', isa => 'Str', lazy => 1, builder => 'formal_name', ); } Currently this works as a technique to default one attribute from another. But the change suggested in this issue would break it. If the builder method was passed a second parameter, then formal_name would be called as a setter. So the suggested change would break any code relying on the technique above. Perhaps nobody is relying on this technique. Perhaps mission control at NASA is relying on it. Who knows? An alternative would be for Moose to set a package variable $Moose::CURRENTLY_BUILDING_ATTRIBUTE to the name of the attribute being built. If the builder cares about the name of the attribute being built, it can check that package variable. It's not an especially elegant solution though. Perhaps the best way is... has 'aaa' => (is => 'ro', default => _from_config('aaa')); has 'bbb' => (is => 'ro', default => _from_config('bbb')); sub _from_config { my $attribute_name = shift; return sub { my ($self) = @_; my $config = ...; return $config->{$attribute_name}; }; }
I don't think we really want to do this. I'll close the ticket in a bit unless anyone objects.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.