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

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

Unified SmartFeed? and Rule API by ad-hoc hook addition. Fixes #28 and #31.

  • Property svn:keywords set to Id Revision
Line 
1 package Plagger::Rules;
2 use strict;
3 use Plagger::Operator;
4
5 sub new {
6     my($class, $op, @rules) = @_;
7     Plagger::Operator->is_valid_op(uc($op))
8         or Plagger->context->error("operator $op not supported");
9
10     bless {
11         op => uc($op),
12         rules => [ map Plagger::Rule->new($_), @rules ],
13     }, $class;
14 }
15
16 sub dispatch {
17     my($self, $plugin, $hook, $args) = @_;
18
19     return 1 unless $plugin->dispatch_rule_on($hook);
20
21     my @bool;
22     for my $rule (@{ $self->{rules} }) {
23         push @bool, ($rule->dispatch($args) ? 1 : 0);
24     }
25
26     # can't find rules for this phase: execute it
27     return 1 unless @bool;
28
29     Plagger::Operator->call($self->{op}, @bool);
30 }
31
32 sub id {
33     my $self = shift;
34     join '|', map $_->id, @{$self->{rules}};
35 }
36
37 sub as_title {
38     my $self = shift;
39     join " $self->{op} ", map $_->as_title, @{$self->{rules}};
40 }
41
42 1;
Note: See TracBrowser for help on using the browser.