Browse Source

more work on match

tags/0.0.0
Zane C. B-H 1 month ago
parent
commit
74894707d0
1 changed files with 56 additions and 2 deletions
  1. 56
    2
      Net-Connection-Match/lib/Net/Connection/Match.pm

+ 56
- 2
Net-Connection-Match/lib/Net/Connection/Match.pm View File

@@ -21,7 +21,7 @@ our $VERSION = '0.0.0';
21 21
 =head1 SYNOPSIS
22 22
 
23 23
     use Net::Connection::Match;
24
-
24
+    use Net::Connection;
25 25
 
26 26
 =head1 METHODS
27 27
 
@@ -59,6 +59,7 @@ sub new{
59 59
 				errorExtra=>{
60 60
 							 flags=>{
61 61
 									 1=>'failedCheckInit',
62
+									 2=>'notNCobj',
62 63
 									 }
63 64
 							 },
64 65
 				checks=>[],
@@ -121,7 +122,11 @@ sub new{
121 122
 		eval( $eval_string );
122 123
 
123 124
 		if (!defined( $check )){
124
-			die('Failed to init the check for '.$check_int.' as it returned undef... '.$@);
125
+			$self->{error}=1;
126
+			$self->{errorString}='Failed to init the check for '.$check_int.' as it returned undef... '.$@;
127
+			$self->warn;
128
+			$self->{perror}=1;
129
+			return $self;
125 130
 		}
126 131
 
127 132
 		$new_check{check}=$check;
@@ -141,7 +146,56 @@ Checks if a single Net::Connection object matches the stack.
141 146
 =cut
142 147
 
143 148
 sub matches{
149
+	my $self=$_[0];
150
+	my $conn=$_[1];
151
+
152
+	if( ! $self->errorblank ){
153
+		return undef;
154
+	}
155
+
156
+	if (
157
+		( ! defined( $conn ) ) ||
158
+		( ref( $conn ) ne 'Net::Connection' )
159
+		){
160
+		$self->{error}=2;
161
+		$self->{errorString}='Either the connection is undefined or is not a Net::Connection object';
162
+		$self->warn;
163
+		return undef;
164
+	}
165
+
166
+	# Stores the number of hits
167
+	my $hits;
168
+	my $required=0;
169
+	foreach my $check ( @{ $self->{checks} } ){
170
+		my $hit;
171
+		eval{
172
+			$hit=$check->{check}->($conn);
173
+		};
174
+
175
+		# If $hits is undef, then one of the checks errored and we skip processing the results.
176
+		# Should only be 0 or 1.
177
+		if ( defined( $hit ) ){
178
+			# invert if needed
179
+			if ( $check->{invert} ){
180
+				$hit = $hit ^ 1;
181
+			}
182
+
183
+			# increment the hits count if we hit
184
+			if ( $hit ){
185
+				$hits++;
186
+			}
187
+		}
188
+
189
+		$required++;
190
+	}
191
+
192
+	# if these are the same, then we have a match
193
+	if ( $required == $hits ){
194
+		return 1;
195
+	}
144 196
 
197
+	# If we get here, it is not a match
198
+	return 0;
145 199
 }
146 200
 
147 201
 =head1 AUTHOR

Loading…
Cancel
Save