Changeset 225

Show
Ignore:
Timestamp:
03/01/06 22:57:12
Author:
miyagawa
Message:

Implemented Plagger::Cache framework, to be used in plugins. Refs #65

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plagger/lib/Plagger.pm

    r194 r225  
    66use Carp; 
    77use Data::Dumper; 
     8use File::Basename; 
    89use File::Find::Rule; 
    910use YAML; 
     
    1112 
    1213use base qw( Class::Accessor::Fast ); 
    13 __PACKAGE__->mk_accessors( qw(conf update subscription plugins_path) ); 
    14  
     14__PACKAGE__->mk_accessors( qw(conf update subscription plugins_path cache) ); 
     15 
     16use Plagger::Cache; 
     17use Plagger::CacheProxy; 
    1518use Plagger::Date; 
    1619use Plagger::Entry; 
     
    4245        $config = YAML::LoadFile($opt{config}); 
    4346        $self->{conf} = $config->{global}; 
    44         $self->{conf}->{log} ||= { level => 'debug' }; 
     47        $self->{conf}->{log}   ||= { level => 'debug' }; 
    4548    } else { 
    4649        croak "Plagger->bootstrap: $opt{config}: $!"; 
    4750    } 
    4851 
     52    $self->load_cache($opt{config}); 
     53 
    4954    local *Plagger::context = sub { $self }; 
    50  
    5155    $self->load_plugins(@{ $config->{plugins} || [] }); 
    5256    $self->run(); 
     57} 
     58 
     59sub load_cache { 
     60    my($self, $config) = @_; 
     61 
     62    # use config filename as a base directory for cache 
     63    my $base = ( basename($config) =~ /^(.*?)\.yaml$/ )[0]; 
     64    my $dir  = $base eq 'config' ? ".plagger" : ".plagger-$base"; 
     65 
     66    $self->{conf}->{cache} ||= { 
     67        base => File::Spec->catfile($ENV{HOME}, $dir), 
     68    }; 
     69 
     70    $self->cache( Plagger::Cache->new($self->{conf}->{cache}) ); 
    5371} 
    5472 
     
    106124 
    107125    my $plugin = $module->new($config); 
     126    $plugin->cache( Plagger::CacheProxy->new($plugin, $self->cache) ); 
    108127    $plugin->register($self); 
    109128} 
  • trunk/plagger/lib/Plagger/Plugin.pm

    r103 r225  
    11package Plagger::Plugin; 
    22use strict; 
     3use base qw( Class::Accessor::Fast ); 
     4 
     5__PACKAGE__->mk_accessors( qw(conf rule rule_hook cache) ); 
    36 
    47use Plagger::Rule; 
     
    3235sub rule { $_[0]->{rule} } 
    3336 
    34 sub rule_hook { 
    35     my $self = shift; 
    36     $self->{rule_hook} = shift if @_; 
    37     $self->{rule_hook}; 
    38 } 
    39  
    4037sub dispatch_rule_on { 
    4138    my($self, $hook) = @_; 
     
    4340} 
    4441 
     42sub class_id { 
     43    my $self = shift; 
     44 
     45    my $pkg = ref($self); 
     46       $pkg =~ s/Plagger::Plugin:://; 
     47    my @pkg = split /::/, $pkg; 
     48 
     49    return join '-', map lc, @pkg; 
     50} 
     51 
    45521;