User Information
| Display Name: | Jesse Luehrs |
|---|
| Partial Email Address: | d...@tozt.net |
| Posts: |
|
| 1) Jesse Luehrs Re: Attributes tracking other attributes. |
|
|
| MooseX::AttributeTree seems to be exactly what you're looking for here. It also handles a lot of... |
|
|
|
|
|
|
|
On Thu, Feb 18, 2010 at 05:37:56PM +0100, Martin Kamerbeek wrote: > Hi, > > For a project I need (slave) attributes that default to the value of > another (master) attribute, and keep following their master's value > until they are set with a value of their own. > > At first I tried using lazy attributes with code ref defaults that would > fetch the value of the attribute that is to be tracked. That doesn't > work however, since the value is fetched only on the first get, and > after that stays static even if the master attribute's value changes. > > In the end I ended up writing a trait, based on the MooseX::Worm trait > that came by this list a couple of days ago. > > It can be found here: > > http://gist.github.com/307789 > > Although it seems to do the job, I have a few questions: > > 1) Is there a simpler way of doing this? I've searched the docs, CPAN > and google but I couldn't come up with an answer.
MooseX::AttributeTree seems to be exactly what you're looking for here. It also handles a lot of other edge cases that your basic implementation doesn't, such as inherited attributes. > 2) If not, is the implementation of the trait correct? What bothers me > most is extending the private _inline_get sub, which seems wrong > (although MooseX::Worm does that too).But your implementation looks fine, Moose extensions are intended to be able to override any part of the implementation of things. Privacy really just indicates which parts of the API are useful for people to use for introspection (i.e. there's no reason for anyone to actually call _inline_get themselves, which is why it's private). -doy
|
|
|
| 2) Jesse Luehrs Re: Maybe[Foo] type coercions |
|
|
| I'm not sure what problem you're having, since you didn't post the error message, but you probably... |
|
|
|
|
|
|
|
On Mon, Feb 15, 2010 at 04:25:54PM -0800, Karen Etheridge wrote: > > I'm having difficulty getting a type coercion to work that involves Maybes. > I've looked at the "deep coercion" section of Moose::Manual::Types, and I'm > not sure what I'm missing to get this to work? > > e.g. I'm running > > perl -MData::Dumper -MObject -MDateTime -I. -wle'my $o=Object->new(date=>"0000-00-00 00:00:00"); print Dumper($o)' > > against this module, Object.pm: > > package Object; > use Moose; > use Moose::Util::TypeConstraints; > use DateTime; > use DateTime::Format::MySQL; > > has date => ( is => 'rw', isa => 'MaybeDateTime', coerce => 1 );
I'm not sure what problem you're having, since you didn't post the error message, but you probably need the attribute definition to happen after the types are defined... pretty sure this would assume you have a class named 'MaybeDateTime'. > # this generates "Attempt to free unreferenced scalar" error!> #has date => ( is => 'rw', isa => 'Undef | DateTime', coerce => 1 );If you could turn this issue into an actual failing test, that would be really helpful. > subtype 'DateTime' => as 'Object' => where { $_->isa('DateTime') };Also, this is equivalent to "class_type 'DateTime'". > subtype 'MaybeDateTime'> => as 'Maybe[Object]'> => where { $_->isa('DateTime') or not defined $_ };> > coerce 'MaybeDateTime'> => from 'DateTime'> => via { return $_ };> > coerce 'MaybeDateTime'> => from 'Str'> => via {> print "### calling Str->MaybeDateTime coercion\n";> # check for retarded mysql 4.x null times> return if $_ eq '0000-00-00 00:00:00';> return DateTime::Format::MySQL->parse_datetime($_);> };> > coerce 'DateTime'> => from 'Str'> => via {> print "### coercing Str $_ into DateTime\n";> return DateTime::Format::MySQL->parse_datetime($_);> };> > 1;-doy
|
|
|
| 3) Jesse Luehrs Re: $anon_class->new_object: BUILDALL is not called |
|
|
| To clarify, if you want Moose features during object construction $meta->name->new, not... |
|
|
|
|
|
|
|
On Sun, Feb 14, 2010 at 06:40:46PM -0500, Hans Dieter Pearcey wrote: > Excerpts from Komarov Oleg's message of Sun Feb 14 18:17:07 -0500 2010: > > It turns out that such an object isa 'Moose::Object', but > > Moose::Object::new isn't involved in its creation. > > $class->new calls $class->meta->new_object, not the other way around. You have > confused the lower level interface for the higher level one.
To clarify, if you want Moose features during object construction (rather than just handling attributes and such), you should use $meta->name->new, not $meta->new_object. -doy
|
|
|
| 4) Jesse Luehrs Re: Non-Moose metaclasses |
|
|
| Actually, I've thought some more about this, and disregard this last paragraph. This here is the... |
|
|
|
|
|
|
|
On Sun, Feb 07, 2010 at 01:37:16PM -0600, Jesse Luehrs wrote: > So, after a discussion with mst in #moose, he pointed out that > initializing a non-Moose class with a Moose metaclass is a bug, and we > really shouldn't be doing it. This occurs quite a bit in code within > CMOP and Moose, which does things like: > > for my $class ($self->linearized_isa) { > my $meta = $self->initialize($class); > ... > } > > and similarly with $self->superclasses, etc. When called on a Moose > class, this will initialize any non-Moose ancestors with a metaclass of > Moose::Meta::Class, which isn't really correct. > > I looked into this, and it seems like the only way to accomplish this > safely is to not cache metaclasses which aren't explicitly initialized. > I've implemented this on the topic/nonmoose_gets_cmop_meta branches of > CMOP and Moose (the name is from an earlier attempt which used a > different strategy). It seems to work properly, and fixes the issues > that I've been able to find, but I was wondering if people think this is > a sane thing to want to do, or if this is going to cause other issues > down the road.
Actually, I've thought some more about this, and disregard this last paragraph. > Another possible solution to this problem would be to force all classes> which get incidentally initialized like this to have CMOP metaclasses,> but this tends to break in complicated inheritance situations - for> instance, Moose::init_meta won't reinitialize a class with a CMOP> metaclass to have a Moose metaclass, it just throws an error (probably> since upgrading the metaclass in a safe way that preserves things like> existing attributes and such would be a pain). Also, in some situations,> the temporary metaclass needs to be a Moose::Meta::Class -> _fix_metaclass_incompatibility for instance doesn't exist in CMOP, so in> a Moose - non-Moose - Moose inheritance setup, the non-Moose class needs> to get a Moose metaclass to run _fix_metaclass_incompatibility on to see> if it is going to need fixing (this isn't a big deal, since metaclass> compatibility means that it needs to have a Moose metaclass anyway, but> still something to watch out for).This here is the actual correct solution. The issues I brought up here are all really just bugs in our metaclass compat code, that really should be fixed. I'll see about having another shot at this sometime later this week. > So... any thoughts?> > -doy-doy
|
|
|
| 5) Jesse Luehrs Non-Moose metaclasses |
|
|
| So, after a discussion with mst in #moose, he pointed out that initializing a non-Moose class with... |
|
|
|
|
|
|
|
So, after a discussion with mst in #moose, he pointed out that initializing a non-Moose class with a Moose metaclass is a bug, and we really shouldn't be doing it. This occurs quite a bit in code within CMOP and Moose, which does things like:
for my $class ($self->linearized_isa) { my $meta = $self->initialize($class); ... }
and similarly with $self->superclasses, etc. When called on a Moose class, this will initialize any non-Moose ancestors with a metaclass of Moose::Meta::Class, which isn't really correct.
I looked into this, and it seems like the only way to accomplish this safely is to not cache metaclasses which aren't explicitly initialized. I've implemented this on the topic/nonmoose_gets_cmop_meta branches of CMOP and Moose (the name is from an earlier attempt which used a different strategy). It seems to work properly, and fixes the issues that I've been able to find, but I was wondering if people think this is a sane thing to want to do, or if this is going to cause other issues down the road.
Another possible solution to this problem would be to force all classes which get incidentally initialized like this to have CMOP metaclasses, but this tends to break in complicated inheritance situations - for instance, Moose::init_meta won't reinitialize a class with a CMOP metaclass to have a Moose metaclass, it just throws an error (probably since upgrading the metaclass in a safe way that preserves things like existing attributes and such would be a pain). Also, in some situations, the temporary metaclass needs to be a Moose::Meta::Class - _fix_metaclass_incompatibility for instance doesn't exist in CMOP, so in a Moose - non-Moose - Moose inheritance setup, the non-Moose class needs to get a Moose metaclass to run _fix_metaclass_incompatibility on to see if it is going to need fixing (this isn't a big deal, since metaclass compatibility means that it needs to have a Moose metaclass anyway, but still something to watch out for).
So... any thoughts?
-doy
|
|
|
|
 | |