Re: roles verify attribute contents when applied?

[email protected] (Mark Hedges)
Newsgroups perl.moose
Message-ID <[email protected]>
Ah.  I see the confusion.

I don't know if I'm going about this in the right way.  I
want to represent a machine.  Machines may be different
hardware types.  It gets a role for the hardware type that
supplies methods that might be able to powercycle it.  I am
applying the hardware-specific role using apply_all_roles to
the instance rather than consuming a role in the class.
(Since it gets a different role based on which type it is,
and each one of these roles supplies the same named method.)
I just wanted to double-check when I apply the role to the
object that it is the correct type.

But I really wouldn't apply the object to the wrong type,
since the base class has a method to figure all that out and
apply the right one.  So this probably isn't necessary, it
would just be a nice cross-check.

Mark

On Thu, 12 Jan 2012, Edward Allen wrote:

> Ok.  This doesn't really make sense.  Here is the problem:  A role is
> applied to a class, not an object (technically you can apply it to an
> object with Moose::Util::apply_all_roles, but this seems like a Bad
> Idea).  A class doesn't have attributes (well there's a module for
> that, but let's ignore that for the time being).  A class has methods.
> So you can test if a method exists, but not if that method returns a
> particular value.
>
> -Edward
>
> On Thu, Jan 12, 2012 at 2:57 PM, Mark Hedges <[email protected]> wrote:
> >
> > Hi.  I'm trying to figure out how a role, when applied to an
> > object, can verify that the object already has a particular
> > attribute and that its conents equal some string.  Am I
> > missing something in the cookbook?  It is sort of backwards
> > from the usual way that an object requires the role can do
> > something or has an attribute.
> >
> > I want to cross check when the role gets applied that the
> > role has everything in the object that the methods it
> > supplies needs to work.
> >
> > I suppose I can just verify this when methods run and then
> > throw an error to say the role got applied to the wrong
> > object, but I'd rather throw that from the place that I
> > apply the role.
> >
> > Mark
> >
>
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.