| 1 | package Data::Model::Mixin; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Carp (); | ||
| 6 | $Carp::Internal{(__PACKAGE__)}++; | ||
| 7 | |||
| 8 | sub import { | ||
| 9 | my($class, %args) = @_; | ||
| 10 | Carp::croak "Usage: use Data::Model::Mixin modules => ['MixinModuleName', 'MixinModuleName2', .... ]" | ||
| 11 | unless $args{modules} && ref($args{modules}) eq 'ARRAY'; | ||
| 12 | |||
| 13 | my $caller = caller; | ||
| 14 | for my $module (@{ $args{modules} }) { | ||
| 15 | my $pkg = $module; | ||
| 16 | $pkg = __PACKAGE__ . "::$pkg" unless $pkg =~ s/^\+//; | ||
| 17 | |||
| 18 | eval "use $pkg"; ## no critic | ||
| 19 | if ($@) { | ||
| 20 | Carp::croak $@; | ||
| 21 | } | ||
| 22 | |||
| 23 | my $register_methods = $pkg->register_method; | ||
| 24 | while (my($method, $code) = each %{ $register_methods }) { | ||
| 25 | no strict 'refs'; | ||
| 26 | *{"$caller\::$method"} = $code; | ||
| 27 | } | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | |||
| 32 | 1; | ||
| 33 | |||
| 34 | =head1 NAME | ||
| 35 | |||
| 36 | Data::Model::Mixin - mixin manager for Data::Model | ||
| 37 | |||
| 38 | =head1 SYNOPSIS | ||
| 39 | |||
| 40 | use Data::Model::Mixin modules => ['mixin_module_names']; | ||
| 41 | |||
| 42 | =head1 AUTHOR | ||
| 43 | |||
| 44 | Kazuhiro Osawa E<lt>yappo <at> shibuya <döt> plE<gt> | ||
| 45 | |||
| 46 | =head1 LICENSE | ||
| 47 | |||
| 48 | This library is free software; you can redistribute it and/or modify | ||
| 49 | it under the same terms as Perl itself. | ||
| 50 | |||
| 51 | =cut |