MooseX-DeclareX

 view release on metacpan or  search on metacpan

lib/MooseX/DeclareX.pm  view on Meta::CPAN

package MooseX::DeclareX;

use 5.010;
use strict;
use warnings;
use utf8;

BEGIN {
	$MooseX::DeclareX::AUTHORITY = 'cpan:TOBYINK';
	$MooseX::DeclareX::VERSION   = '0.009';
}

use constant DEFAULT_KEYWORDS => [qw(class role exception)];
use constant DEFAULT_PLUGINS  => [qw(build guard std_constants)];

use Class::Load 0 qw(load_class);
use Data::OptList 0;
use MooseX::Declare 0;
use TryCatch 0;

sub import
{
	my ($class, %args) = @_;
	my $caller = caller(0);

	# Simplified interface for a couple of standard plugins
	foreach my $awesome (qw/ types imports /)
	{
		if (my $ness = delete $args{$awesome})
		{
			$args{plugins} ||= DEFAULT_PLUGINS;
			push @{ $args{plugins} }, $awesome, $ness;
		}
	}

	$_->setup_for($caller, %args, provided_by => $class) for __PACKAGE__->_keywords(\%args);
	
	strict->import;
	warnings->import;
	TryCatch->import({ into => $caller });
}

sub _keywords
{
	my ($class, $args) = @_;
	my @return;
	
	my $kinds = Data::OptList::mkopt( $args->{keywords} || DEFAULT_KEYWORDS );
	foreach my $pair (@$kinds)
	{
		my ($class, $opts) = @$pair;
		$opts //= {};
		
		load_class(
			my $module = join '::' => qw[MooseX DeclareX Keyword], $class
		);
		
		my $kw = $module->new( $opts->{init} ? $opts->{init} : (identifier => $class) );
		push @return, $kw;
		
		my $plugins = Data::OptList::mkopt(
			$opts->{plugins} || $args->{plugins} || DEFAULT_PLUGINS,
		);
		
		foreach my $pair2 (@$plugins)
		{
			no warnings;
			my ($class2, $opts2) = @$pair2;
			next if $class2 =~ qr(
				method | before | after | around | override | augment
				| with | is | clean | dirty | mutable | try | catch
			)x;
			use warnings;
			
			my $module2 = join '::' => (qw[MooseX DeclareX Plugin], $class2);
			$module2 =~ s/Plugin::concrete$/Plugin::abstract/;
			load_class $module2;
			
			$module2->plugin_setup($kw, $opts2);
		}
	}
	
	return @return;
}

"Would you like some tea with that sugar?"

__END__

=head1 NAME

MooseX::DeclareX - more sugar for MooseX::Declare

=head1 SYNOPSIS

  use 5.010;
  use MooseX::DeclareX
    keywords => [qw(class exception)],



( run in 1.803 second using v1.01-cache-2.11-cpan-5a3173703d6 )