| 1 | package Farly::Remove::Rule; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use 5.008008; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Farly::Object::Aggregate qw(NEXTVAL); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 1 | our $VERSION = '0.26'; | |||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 12 | 1 | my ( $class, $list ) = @_; | |||||||||
| 13 | |||||||||||
| 14 | 1 | ▶ | confess "configuration Farly::Object::List required" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | unless ( defined($list) ); | ||||||||||
| 16 | |||||||||||
| 17 | 1 | ▶ | confess "configuration Farly::Object::List required" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | unless ( $list->isa("Farly::Object::List") ); | ||||||||||
| 19 | |||||||||||
| 20 | 1 | my $self = { | |||||||||
| 21 | FW => $list, | ||||||||||
| 22 | RESULT => Farly::Object::List->new(), | ||||||||||
| 23 | }; | ||||||||||
| 24 | 1 | bless $self, $class; | |||||||||
| 25 | |||||||||||
| 26 | 1 | return $self; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 1 | ▶ | sub fw { return $_[0]->{FW} } | ||||||||
|
Subroutine
fw: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 30 | 5 | ▶ | sub result { return $_[0]->{RESULT} } | ||||||||
|
Subroutine
result: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 31 | |||||||||||
| 32 | 1 | ▶ | sub _removes { | ||||||||
|
Subroutine
_removes: called CC=3 SCAR=11.0
| |||||||||||
| 33 | 1 | my ( $self, $list ) = @_; | |||||||||
| 34 | |||||||||||
| 35 | 1 | my $remove = Farly::Object::List->new(); | |||||||||
| 36 | |||||||||||
| 37 | 1 | foreach my $rule ( $list->iter() ) { | |||||||||
| 38 | 21 | ▶ | if ( $rule->has_defined('REMOVE') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 2 | $remove->add($rule); | |||||||||
| 40 | } | ||||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 1 | return $remove; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 1 | ▶ | sub _keeps { | ||||||||
|
Subroutine
_keeps: called CC=3 SCAR=11.0
| |||||||||||
| 47 | 1 | my ( $self, $list ) = @_; | |||||||||
| 48 | |||||||||||
| 49 | 1 | my $keep = Farly::Object::List->new(); | |||||||||
| 50 | |||||||||||
| 51 | 1 | foreach my $rule ( $list->iter() ) { | |||||||||
| 52 | 21 | ▶ | if ( !$rule->has_defined('REMOVE') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 19 | $keep->add($rule); | |||||||||
| 54 | } | ||||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 1 | return $keep; | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | # convert an object into a reference object | ||||||||||
| 61 | 1 | ▶ | sub _create_ref { | ||||||||
|
Subroutine
_create_ref: called CC=1 SCAR=0.0
| |||||||||||
| 62 | 1 | my ( $self, $object ) = @_; | |||||||||
| 63 | |||||||||||
| 64 | 1 | my $ref = Farly::Object::Ref->new(); | |||||||||
| 65 | 1 | $ref->set( 'ENTRY', $object->get('ENTRY') ); | |||||||||
| 66 | 1 | $ref->set( 'ID', $object->get('ID') ); | |||||||||
| 67 | |||||||||||
| 68 | 1 | return $ref; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 1 | ▶ | sub _is_expanded { | ||||||||
|
Subroutine
_is_expanded: called CC=4 SCAR=14.8
| |||||||||||
| 72 | 1 | my ( $self, $list ) = @_; | |||||||||
| 73 | |||||||||||
| 74 | 1 | foreach my $object ( $list->iter() ) { | |||||||||
| 75 | 21 | foreach my $property ( $object->get_keys() ) { | |||||||||
| 76 | 197 | ▶ | if ( $object->get($property)->isa('Farly::Object') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 0 | confess "an expanded rule set is required"; | |||||||||
| 78 | } | ||||||||||
| 79 | } | ||||||||||
| 80 | } | ||||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 1 | ▶ | sub _is_unique { | ||||||||
|
Subroutine
_is_unique: called CC=3 SCAR=12.0
| |||||||||||
| 84 | 1 | my ( $self, $id, $list ) = @_; | |||||||||
| 85 | |||||||||||
| 86 | 1 | foreach my $object ( $list->iter() ) { | |||||||||
| 87 | 21 | ▶ | if ( !$object->matches($id) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 0 | die "list is not unique"; | |||||||||
| 89 | } | ||||||||||
| 90 | } | ||||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 2 | ▶ | sub _aggregate { | ||||||||
|
Subroutine
_aggregate: called CC=1 SCAR=0.0
| |||||||||||
| 94 | 2 | my ( $self, $list ) = @_; | |||||||||
| 95 | 2 | my $agg = Farly::Object::Aggregate->new($list); | |||||||||
| 96 | 2 | $agg->groupby( 'ENTRY', 'ID', 'LINE' ); | |||||||||
| 97 | 2 | return $agg; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 1 | ▶ | sub remove { | ||||||||
|
Subroutine
remove: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 101 | 1 | my ( $self, $list ) = @_; | |||||||||
| 102 | |||||||||||
| 103 | 1 | my $rule_id = $self->_create_ref( $list->[0] ); | |||||||||
| 104 | |||||||||||
| 105 | # validate list | ||||||||||
| 106 | 1 | $self->_is_unique( $rule_id, $list ); | |||||||||
| 107 | 1 | $self->_is_expanded($list); | |||||||||
| 108 | |||||||||||
| 109 | # get the config rules | ||||||||||
| 110 | 1 | my $cfg_rules = Farly::Object::List->new(); | |||||||||
| 111 | 1 | $self->fw->matches( $rule_id, $cfg_rules ); | |||||||||
| 112 | |||||||||||
| 113 | # remove_agg will have the list of entries to be removed | ||||||||||
| 114 | 1 | my $remove_agg = $self->_aggregate( $self->_removes($list) ); | |||||||||
| 115 | |||||||||||
| 116 | # keep_agg will have the set of entries which need to be kept | ||||||||||
| 117 | 1 | my $keep_agg = $self->_aggregate( $self->_keeps($list) ); | |||||||||
| 118 | |||||||||||
| 119 | 1 | my $it = $remove_agg->id_iterator(); | |||||||||
| 120 | |||||||||||
| 121 | 1 | ▶ | while ( my $id = NEXTVAL($it) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | |||||||||||
| 123 | # identity of the entries which are being kept | ||||||||||
| 124 | 2 | my $keep_list = $keep_agg->matches($id); | |||||||||
| 125 | |||||||||||
| 126 | # add the entries which are being kept | ||||||||||
| 127 | 2 | foreach my $keep_rule ( $keep_list->iter() ) { | |||||||||
| 128 | |||||||||||
| 129 | 2 | $self->result->add($keep_rule); | |||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | # add the config rule being removed | ||||||||||
| 133 | 2 | foreach my $object ( $cfg_rules->iter() ) { | |||||||||
| 134 | |||||||||||
| 135 | 32 | ▶ | if ( $object->matches($id) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | |||||||||||
| 137 | 2 | my $clone = $object->clone(); | |||||||||
| 138 | 2 | $clone->set( 'REMOVE', Farly::Value::String->new('RULE') ); | |||||||||
| 139 | 2 | $clone->delete_key('LINE'); | |||||||||
| 140 | |||||||||||
| 141 | 2 | $self->result->add($clone); | |||||||||
| 142 | } | ||||||||||
| 143 | } | ||||||||||
| 144 | } | ||||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | 1 | 1; | |||||||||
| 148 | __END__ | ||||||||||