Whoops, I realized this is a lot simpler than I was making
it out to be.

HostSet has an attribute of the roles to apply to the hosts.

The HostSet roles do 'after BUILD => ...' and add that.

Ta-da.

Thanks.

Mark


On Tue, 20 Dec 2011, Mark Hedges wrote:

>
> Hi.  I tried to follow the example in
> Moose::Cookbook::Meta::Recipe5 but I got this error: 'Can't
> locate object method "table" via package
> "Moose::Meta::Class::__ANON__::SERIAL::1'.  I filed a bug.
>
> What I am trying to do is create a container object for
> another class of objects, apply selected roles to the
> container on construction, which tell it what other roles to
> apply to the contained objects.
>
> I.e.
>
>  my $hostset = My::HostSet
>      ->with_traits(qw( Cluster Hardware ))
>      ->new( hostnames => [qw( foo bar baz )]);
>
>  package My::HostSet;
>  with 'MooseX::Traits';
>  has '+_trait_namespace' => ( default => 'My::Role::HostSet' );
>  use My::Host;
>  use Moose::Util qw( apply_all_roles );
>
>  has hostnames (
>      # ...
>  );
>  has hosts (
>      # ...
>      lazy_build => 1,
>  );
>  sub _build_hosts {
>      my @hosts = map My::Host->new( hostname => $_ ), @{$hostnames};
>
>      my @contained_object_roles = foo();  # ...
>      # here I want to find out from My::Role::HostSet::Cluster
>      # and My::Role::HostSet::Hardware what other roles it
>      # should apply to the contained objects.
>
>      apply_all_roles( $_, @contained_object_roles ) for @hosts;
>      return \@hosts;
>  }
>
> I don't see a good way to do this.  Having same name attribute in
> each HostSet role class conflicts, obviously.  I thought maybe
> adding meta attributes to the HostSet role classes would be
> the way to go, but the example doesn't work.
>
> I don't know what I'm doing... thanks.  :-)
>
> Mark
>

Reply via email to