|
Revision 11213, 1.2 kB
(checked in by mizzy, 5 years ago)
|
|
ロギング機能追加。Moose::Role かわゆす。
|
| Line | |
|---|
| 1 | package Punc::Slave::Module; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | use Moose; |
|---|
| 6 | use Module::Pluggable; |
|---|
| 7 | |
|---|
| 8 | extends 'Class::Data::Inheritable'; |
|---|
| 9 | |
|---|
| 10 | __PACKAGE__->mk_classdata('default_for'); |
|---|
| 11 | |
|---|
| 12 | sub import { |
|---|
| 13 | my ( $class, $args ) = @_; |
|---|
| 14 | my $pkg = caller(0); |
|---|
| 15 | no strict 'refs'; |
|---|
| 16 | unshift @{"$pkg\::ISA"}, $class; |
|---|
| 17 | $pkg->default_for($args); |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | sub new { |
|---|
| 21 | my $class = shift; |
|---|
| 22 | my $self = bless {}, $class; |
|---|
| 23 | $self->delegate; |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | sub delegate { |
|---|
| 27 | my $self = shift; |
|---|
| 28 | $self->search_path( new => ref $self ); |
|---|
| 29 | my @modules = ( $self->plugins, ref $self ); |
|---|
| 30 | my $module_to_delegate; |
|---|
| 31 | for my $module ( @modules ) { |
|---|
| 32 | next if $module =~ /Role$/; |
|---|
| 33 | $module->require or die $@; |
|---|
| 34 | my $default_for = $module->default_for; |
|---|
| 35 | next unless $default_for; |
|---|
| 36 | my ( $fact ) = keys %$default_for; |
|---|
| 37 | if ( grep { Punc->context->fact($fact) =~ /$_/i } @{ $default_for->{$fact} } ) { |
|---|
| 38 | $module_to_delegate = $module; |
|---|
| 39 | } |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | Punc->context->log( info => "Delegated to $module_to_delegate." ); |
|---|
| 43 | bless $self, $module_to_delegate; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | sub exec { |
|---|
| 47 | my ( $self, $method, $args ) = @_; |
|---|
| 48 | $self->$method($args); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | sub description { |
|---|
| 52 | my $class = shift; |
|---|
| 53 | return { result => `perldoc -t $class` || '' }; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | 1; |
|---|