Codebase list libkavorka-perl / 9c7c300
allow traits to be applied to keywords via import Toby Inkster 10 years ago
2 changed file(s) with 37 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
5353 $IMPLEMENTATION{$_[0]};
5454 }
5555
56 sub compose_implementation
57 {
58 shift;
59 require Moo::Role;
60 Moo::Role->create_class_with_roles(@_);
61 }
62
63
5664 sub _exporter_fail
5765 {
5866 my $me = shift;
6573
6674 my $into = $globals->{into};
6775
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')
6985 or Carp::croak("No suitable implementation for keyword '$name'");
7086
7187 # Kavorka::Multi (for example) needs to know what Kavorka keywords are
247263 # use My::Sub::Method instead of Kavorka::Sub::Method
248264 use Kavorka method => { implementation => 'My::Sub::Method' };
249265
266 Or add traits to the default implementation:
267
268 use Kavorka method => { traits => ['My::Sub::Role::Foo'] };
269
250270 See L<Exporter::Tiny> for more tips.
251271
252272 =head2 Function Introspection API
7878 );
7979 };
8080
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
8197 done_testing;