allow traits to be applied to keywords via import
Toby Inkster
10 years ago
53 | 53 |
$IMPLEMENTATION{$_[0]};
|
54 | 54 |
}
|
55 | 55 |
|
|
56 |
sub compose_implementation
|
|
57 |
{
|
|
58 |
shift;
|
|
59 |
require Moo::Role;
|
|
60 |
Moo::Role->create_class_with_roles(@_);
|
|
61 |
}
|
|
62 |
|
|
63 |
|
56 | 64 |
sub _exporter_fail
|
57 | 65 |
{
|
58 | 66 |
my $me = shift;
|
|
65 | 73 |
|
66 | 74 |
my $into = $globals->{into};
|
67 | 75 |
|
68 | |
Module::Runtime::use_package_optimistically($implementation)->can('parse')
|
|
76 |
Module::Runtime::use_package_optimistically($implementation);
|
|
77 |
|
|
78 |
{
|
|
79 |
my $traits = $globals->{traits} // $args->{traits};
|
|
80 |
$implementation = $me->compose_implementation($implementation, @$traits)
|
|
81 |
if $traits;
|
|
82 |
}
|
|
83 |
|
|
84 |
$implementation->can('parse')
|
69 | 85 |
or Carp::croak("No suitable implementation for keyword '$name'");
|
70 | 86 |
|
71 | 87 |
# Kavorka::Multi (for example) needs to know what Kavorka keywords are
|
|
247 | 263 |
# use My::Sub::Method instead of Kavorka::Sub::Method
|
248 | 264 |
use Kavorka method => { implementation => 'My::Sub::Method' };
|
249 | 265 |
|
|
266 |
Or add traits to the default implementation:
|
|
267 |
|
|
268 |
use Kavorka method => { traits => ['My::Sub::Role::Foo'] };
|
|
269 |
|
250 | 270 |
See L<Exporter::Tiny> for more tips.
|
251 | 271 |
|
252 | 272 |
=head2 Function Introspection API
|
78 | 78 |
);
|
79 | 79 |
};
|
80 | 80 |
|
|
81 |
use Kavorka funny => {
|
|
82 |
implementation => 'Kavorka::Sub::Fun',
|
|
83 |
traits => [ 'Kavorka::TraitFor::Sub::superbad' ],
|
|
84 |
};
|
|
85 |
|
|
86 |
funny foo3 () {
|
|
87 |
43
|
|
88 |
}
|
|
89 |
|
|
90 |
subtest "Passing traits to import" => sub
|
|
91 |
{
|
|
92 |
my $foo = Kavorka->info( 'main'->can('foo3') );
|
|
93 |
ok $foo->DOES('Kavorka::TraitFor::Sub::superbad');
|
|
94 |
is foo3(), 43;
|
|
95 |
};
|
|
96 |
|
81 | 97 |
done_testing;
|