root/trunk/plagger/lib/Plagger/Plugin.pm

Revision 415 (checked in by miyagawa, 14 years ago)
  • Filter::Regexp to use Filter::Base now
  • $plugin has log method now to support subclass to get proper caller
  • Property svn:keywords set to Id Revision
Line 
1 package Plagger::Plugin;
2 use strict;
3 use base qw( Class::Accessor::Fast );
4
5 __PACKAGE__->mk_accessors( qw(conf rule rule_hook cache) );
6
7 use Plagger::Rule;
8 use Plagger::Rules;
9
10 use FindBin;
11 use File::Spec;
12
13 sub new {
14     my($class, $opt) = @_;
15     my $self = bless {
16         conf => $opt->{config} || {},
17         rule => $opt->{rule},
18         rule_op => $opt->{rule_op} || 'AND',
19         rule_hook => '',
20         meta => {},
21     }, $class;
22     $self->init();
23     $self;
24 }
25
26 sub init {
27     my $self = shift;
28     if (my $rule = $self->{rule}) {
29         $rule = [ $rule ] if ref($rule) eq 'HASH';
30         my $op = $self->{rule_op};
31         $self->{rule} = Plagger::Rules->new($op, @$rule);
32     } else {
33         $self->{rule} = Plagger::Rule->new({ module => 'Always' });
34     }
35 }
36
37 sub conf { $_[0]->{conf} }
38 sub rule { $_[0]->{rule} }
39
40 sub dispatch_rule_on {
41     my($self, $hook) = @_;
42     $self->rule_hook && $self->rule_hook eq $hook;
43 }
44
45 sub class_id {
46     my $self = shift;
47
48     my $pkg = ref($self) || $self;
49        $pkg =~ s/Plagger::Plugin:://;
50     my @pkg = split /::/, $pkg;
51
52     return join '-', map lc, @pkg;
53 }
54
55 sub assets_dir {
56     my $self = shift;
57
58     my $assets_dir = File::Spec->catfile(
59                             $self->conf->{assets_path}
60                          || ($FindBin::Bin, "assets/plugins", $self->class_id)
61                      );
62
63 }
64
65 sub log {
66     my $self = shift;
67     Plagger->context->log(@_, caller => ref($self));
68 }
69
70 1;
Note: See TracBrowser for help on using the browser.