package MooX::BuildClass;

use strictures 2;
use Package::Variant 1.003002    #
  importing => ['Moo'],
  subs      => [qw(extends has with before around after)];
use Module::Runtime 'module_notional_filename';

our $VERSION = '0.152070'; # VERSION

# ABSTRACT: build a Moo class at runtime

#
# This file is part of Throwable-SugarFactory
#
#
# Christian Walde has dedicated the work to the Commons by waiving all of his
# or her rights to the work worldwide under copyright law and all related or
# neighboring legal rights he or she had in the work, to the extent allowable by
# law.
#
# Works under CC0 do not require attribution. When citing the work, you should
# not imply endorsement by the author.
#


sub make_variant_package_name {
    my ( undef, $name ) = @_;

    my $path = module_notional_filename $name;
    die "Won't clobber already loaded: $path => $INC{$path}" if $INC{$path};

    return $name;
}


sub make_variant {
    my ( $class, undef, undef, @args ) = @_;
    while ( @args ) {
        my ( $func, $args ) = ( shift @args, shift @args );
        $args = [$args] if ref $args ne "ARRAY";
        __PACKAGE__->can( $func )->( @{$args} );
    }
    return;
}

1;

__END__

=pod

=head1 NAME

MooX::BuildClass - build a Moo class at runtime

=head1 VERSION

version 0.152070

=head1 SYNOPSIS

    use MooX::BuildClass;
        
    BuildClass "Cat::Food" => (
        
        install => [
            feed_lion => sub {
                my $self = shift;
                my $amount = shift || 1;
                
                $self->pounds( $self->pounds - $amount );
            },
        ],
        
        has => [ taste => ( is => 'ro', ) ],
        
        has => [
            brand => (
                is  => 'ro',
                isa => sub {
                    die "Only SWEET-TREATZ supported!" unless $_[0] eq 'SWEET-TREATZ';
                },
            )
        ],
        
        has => [
            pounds => (
                is  => 'rw',
                isa => sub { die "$_[0] is too much cat food!" unless $_[0] < 15 },
            )
        ],
        
        extends => "Food",
        
    );

    1;

=head1 DESCRIPTION

Provides a runtime interface to create Moo classes. Takes a class name and a
pair-list of parameters used to create the class. The pairs are always in the
form of ( function => arguments ), where arguments has to be a single scalar. It
can be either an array-ref, or if it is not one, it will be wrapped in one.
C<function> can be a string from this list:

extends  has  with  before  around  after  install

The obvious ones are proxies for the corresponding Moo class setup functions,
and install is used to set up methods.

=head1 METHODS

=head2 make_variant_package_name

Advises Package::Variant to use the user-provided name to create the new class
in. Dies if that class has already been defined.

=head2 make_variant

Takes the arguments and executes them as function calls on the target package
to declare the class.

=head1 AUTHOR

Christian Walde <walde.christian@gmail.com>

=head1 COPYRIGHT AND LICENSE


Christian Walde has dedicated the work to the Commons by waiving all of his
or her rights to the work worldwide under copyright law and all related or
neighboring legal rights he or she had in the work, to the extent allowable by
law.

Works under CC0 do not require attribution. When citing the work, you should
not imply endorsement by the author.

=cut
