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

Revision 447 (checked in by miyagawa, 14 years ago)

Added plugin_id method to mean object ID, not class ID. Fixes #95

  • 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 # subclasses may overload to avoid cache sharing
56 sub plugin_id {
57     my $self = shift;
58     $self->class_id;
59 }
60
61 sub assets_dir {
62     my $self = shift;
63
64     my $assets_dir = File::Spec->catfile(
65                             $self->conf->{assets_path}
66                          || ($FindBin::Bin, "assets/plugins", $self->class_id)
67                      );
68
69 }
70
71 sub log {
72     my $self = shift;
73     Plagger->context->log(@_, caller => ref($self));
74 }
75
76 1;
Note: See TracBrowser for help on using the browser.