package MooseX::Recipe::Builder;

use Moose;

has q{class} => (is => q{ro}, required => 1, isa => q{Str});
has q{with_name} => (is => q{ro}, isa => q{Str});
has q{roles} => (is => q{ro}, required => 1, isa => q{ArrayRef});
has q{anon_meta} => (is => 'ro', required => 1, lazy => 1, default => sub { shift->_build_anon_meta });
has q{class_name} => (is => 'ro', required => 1, lazy => 1, default => sub { shift->anon_meta->name });

sub _build_anon_meta {
  my ($self) = @_;
 
  my $class = $self->class();
  my @roles = @{ $self->roles() };
  
  my $anon_class;
  if(my $class_name = $self->with_name()){
    $anon_class = Moose::Meta::Class->create($class_name);
  }else{
    $anon_class = Moose::Meta::Class->create_anon_class();
  }
  $anon_class->superclasses($class);

  Class::MOP::load_class($_) for @roles;
  if (scalar @roles == 1) {
      $roles[0]->meta->apply($anon_class);
  }
  else {
      Moose::Meta::Role->combine(
          map { $_->meta } @roles
      )->apply($anon_class);
  }

  return $anon_class;
}

=head1 NAME

MooseX::Recipe::Builder - Cook Moose classes with roles into anonymous classes

=head1 VERSION

Version 0.01

=cut

our $VERSION = '0.01';

=head1 SYNOPSIS

 use MooseX::Recipe::Builder;
 my $builder = MooseX::Recipe::Builder->new({
   class => q{Pie},
   roles => [qw(Apple Pear)],
 });

=head1 ATTRIBUTES

=head2 class

This attribute contains the name of the class that is being cooked.

=head2 roles

This attribute contains an ArrayRef of role names that the class is being
cooked with.

=head2 class_name

This attribute returns the name of the cooked class.

=head2 anon_meta

This attribute returns the meta-model of the cooked class.

=head2 with_name

This attribute contains the name of the class that the class should be
cooked as.

=head2 meta

This contains the builders meta-model.

=head1 AUTHOR

Scott McWhirter, C<< <kungfuftr at cpan.org> >>

=head1 BUGS

Please report any bugs or feature requests to
C<bug-moosex-recipe at rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MooseX-Recipe>.
I will be notified, and then you'll automatically be notified of progress on
your bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc MooseX::Recipe

You can also look for information at:

=over 4

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/MooseX-Recipe>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/MooseX-Recipe>

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooseX-Recipe>

=item * Search CPAN

L<http://search.cpan.org/dist/MooseX-Recipe>

=back

=head1 ACKNOWLEDGEMENTS

=head1 COPYRIGHT & LICENSE

Copyright 2007 Scott McWhirter, all rights reserved.

This program is released under the following license: BSD

=cut

1; # End of MooseX::Recipe
