| 1 | package Plagger::Rule::Candidates; |
|---|
| 2 | use strict; |
|---|
| 3 | use base qw( Plagger::Rule ); |
|---|
| 4 | use DB_File; |
|---|
| 5 | |
|---|
| 6 | use UNIVERSAL::require; |
|---|
| 7 | use Readonly; |
|---|
| 8 | use Encode; |
|---|
| 9 | use Data::Dumper; |
|---|
| 10 | |
|---|
| 11 | Readonly my %storage => ( |
|---|
| 12 | asis => 0, |
|---|
| 13 | bdb => 'DB_File', |
|---|
| 14 | ); |
|---|
| 15 | |
|---|
| 16 | sub init { |
|---|
| 17 | my $self = shift; |
|---|
| 18 | |
|---|
| 19 | my $friendly_storage_symbol = $self->{storage} || 'bdb'; |
|---|
| 20 | my $storage = $storage{$friendly_storage_symbol}; |
|---|
| 21 | Plagger->context->error("Unknown storage type $self->{storage}: $@") and die unless defined $storage; |
|---|
| 22 | if ($storage) { |
|---|
| 23 | $storage->require or Plagger->context->error("Error loading $storage: $@"); |
|---|
| 24 | tie my %dict, $storage, $self->{path}, O_RDWR|O_CREAT, 0600, $DB_BTREE |
|---|
| 25 | or Plagger->context->error("Can't open $storage $self->{path}: $!"); |
|---|
| 26 | $self->{candidates} = \%dict; |
|---|
| 27 | } else { |
|---|
| 28 | my %hash; |
|---|
| 29 | foreach my $cand (@{$self->{candidates}}) { |
|---|
| 30 | $hash{encode('utf-8', $cand)} = 1; |
|---|
| 31 | } |
|---|
| 32 | $self->{candidates} = \%hash; |
|---|
| 33 | } |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | sub dispatch { |
|---|
| 37 | my($self, $args) = @_; |
|---|
| 38 | my $value = eval $self->{property}; |
|---|
| 39 | if ($@) { |
|---|
| 40 | Plagger->context->log(error => "Expression error: $@ with '$self->{property}'"); |
|---|
| 41 | } |
|---|
| 42 | Plagger->context->log(debug => "Target value '$self->{property}' is " . Dumper($value)); |
|---|
| 43 | defined $self->{candidates}->{encode('utf-8', $value)}; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | 1; |
|---|
| 47 | |
|---|
| 48 | __END__ |
|---|
| 49 | |
|---|
| 50 | =head1 NAME |
|---|
| 51 | |
|---|
| 52 | Plagger::Rule::Candidates |
|---|
| 53 | |
|---|
| 54 | =head1 SYNOPSIS |
|---|
| 55 | |
|---|
| 56 | - module: Filter::Rule |
|---|
| 57 | rule: |
|---|
| 58 | - module: Candidates |
|---|
| 59 | storage: bdb |
|---|
| 60 | path: /path/to/berkeley/db/datafile |
|---|
| 61 | property: 'split /, ?/, $args->{entry}->{body} |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | - module: Filter::Rule |
|---|
| 65 | rule: |
|---|
| 66 | - module: Candidates |
|---|
| 67 | storage: asis |
|---|
| 68 | candidates: |
|---|
| 69 | - foo |
|---|
| 70 | - bar |
|---|
| 71 | property: 'split /, ?/, $args->{entry}->{body} |
|---|
| 72 | |
|---|
| 73 | =head1 DESCRIPTION |
|---|
| 74 | |
|---|
| 75 | This rule mathces a feed when the C<property> of the feed is one of the C<candidates>, |
|---|
| 76 | where the C<property> of the feed is the evaluated value of C<property> option. |
|---|
| 77 | |
|---|
| 78 | You can specify the C<candidates> through one of the followings: |
|---|
| 79 | |
|---|
| 80 | =over 4 |
|---|
| 81 | |
|---|
| 82 | =item bdb |
|---|
| 83 | |
|---|
| 84 | Berkeley DB (Btree) |
|---|
| 85 | |
|---|
| 86 | =item asis |
|---|
| 87 | |
|---|
| 88 | embeded in config.yaml |
|---|
| 89 | |
|---|
| 90 | =back |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | =head1 AUTHOR |
|---|
| 94 | |
|---|
| 95 | Yuki Sonoda (a.k.a. Yugui) |
|---|
| 96 | |
|---|
| 97 | =head1 SEE ALSO |
|---|
| 98 | |
|---|
| 99 | L<Plagger>, L<Plagger::Rule> |
|---|
| 100 | |
|---|
| 101 | =cut |
|---|