root/lang/perl/plagger/lib/Plagger/Rule/Candidates.pm @ 7640

Revision 856, 2.2 kB (checked in by yugui, 7 years ago)

lang/perl/plager/lib/Rule: created
lang/perl/plager/lib/Rule/Candidates.pm: initial import
lang/perl/plagger/lib/Rule: moved into lang/perl/plagger/lib/Plagger
lang/perl/plagger/lib/Plagger/Rule: moved from lang/perl/plagger/lib/Rule
lang/perl/plagger/lib/Plagger/Rule/Candidates.pm: moved from ../Rule
lang/perl/plagger/lib/Plagger/Rule/Candidates.pm: wrote POD

Line 
1package Plagger::Rule::Candidates;
2use strict;
3use base qw( Plagger::Rule );
4use DB_File;
5
6use UNIVERSAL::require;
7use Readonly;
8use Encode;
9use Data::Dumper;
10
11Readonly my %storage => (
12    asis => 0,
13    bdb => 'DB_File',
14);
15
16sub 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
36sub 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
461;
47
48__END__
49
50=head1 NAME
51
52Plagger::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
75This rule mathces a feed when the C<property> of the feed is one of the C<candidates>,
76where the C<property> of the feed is the evaluated value of C<property> option.
77
78You can specify the C<candidates> through one of the followings:
79
80=over 4
81
82=item bdb
83
84Berkeley DB (Btree)
85
86=item asis
87
88embeded in config.yaml
89
90=back
91
92
93=head1 AUTHOR
94
95Yuki Sonoda (a.k.a. Yugui)
96
97=head1 SEE ALSO
98
99L<Plagger>, L<Plagger::Rule>
100
101=cut
Note: See TracBrowser for help on using the browser.