2007年09月11日

なんだかんだしてたら返事が遅れて超亀レスで申し訳ないですが

sexさんのsubtech - sexさんのブログ - Class::Component::Plugin の init()

init() っていうメソッドがたぶんabstract methodとしてあるっぽいんだけど、その中でなにも渡ってこないのが悔しい><

との事ですが、どうせcallとかrun_hookで呼ばれる時にはMethod,Hookなメソッドにはcontextが渡されるので、要らないかな?というのと
今となってはDisableDynamicPluginを使うと、pluginのinitを呼ばれるタイミングでは、大元のpackageのインスタンスが出来てないので$contextをinitに渡せないという理由があったりします。

やるとすればComponentとしてnewした時にpluginを初期化する処理とか実装したほうがいいのかなぁ?と
例えば以下のようなComponentを用意して

package Class::Component::Component::PluginSetContext;
use strict;
use warnings;
use Scalar::Util ();

sub new {
    my $class = shift;
    my $self = $class->NEXT( new => @_ );

    for my $plugin (@{ $self->class_component_plugins }) {
        $plugin->{context} = $self;
        Scalar::Util::weaken( $self->{ context } );
    }

    $self;
}

package Class::Component::Plugin;
sub context { shift->{context} }
1;

これをload_componentsすれば、元記事でやりたい事は一応できます。

使い方のサンプルとしては

package OIOI;

use strict;
use warnings;

use Class::Component;
__PACKAGE__->load_components(qw/ PluginSetContext /);

sub hello {
    my $self = shift;
    print $self->{hello} . "\n";
}

1;

package OIOI::Plugin::Hoge;

use strict;
use warnings;
use base 'Class::Component::Plugin';

sub hoge :Method {
    my($self, $c) = @_;
    $self->context->{hello} = 'hoge';
}

sub uge :Method {
    my($self, $c) = @_;
    $self->context->{hello} = 'uge';    
}

1;

use strict;
use warnings;

use OIOI;

my $oioi = OIOI->new({ load_plugins => [ qw / Hoge / ] });

$oioi->call( 'hoge' );
$oioi->hello; # print "hoge\n";

$oioi->call( 'uge' );
$oioi->hello; # print "uge\n";

こんな感じです。

まだまだ、手をつけなきゃいけないところもあるし

とか、他にもいろいろしたいよ!

という要望。

という事なので、もっと色々聞かせていただきたく。

CodeRepos行き?

Posted by Yappo at 2007年09月11日 22:21 | TrackBack | Perl
Comments
Post a comment









Remember personal info?






コメントを投稿する前に↓の場所にnospamと入力してください。