File Coverage

blib/lib/DBIx/DBHResolver/Strategy/Range.pm
Criterion Covered Total %
statement 35 40 87.5
branch 7 10 70.0
condition 1 3 33.3
subroutine 6 7 85.7
pod 2 2 100.0
total 51 62 82.2


line stmt bran cond sub pod time code
1             package DBIx::DBHResolver::Strategy::Range;
2              
3 7     7   37 use strict;
  7         14  
  7         254  
4 7     7   37 use warnings;
  7         14  
  7         175  
5 7     7   56 use parent qw(DBIx::DBHResolver::Strategy);
  7         13  
  7         35  
6 7     7   379 use Carp;
  7         14  
  7         523  
7 7     7   42 use Data::Util qw(is_array_ref neat);
  7         19  
  7         3643  
8              
9             our $VERSION = '0.01';
10             our %OPS = (
11             '>' => sub { ( $_[0] > $_[1] ) ? 1 : 0 },
12             '>=' => sub { ( $_[0] >= $_[1] ) ? 1 : 0 },
13             '<' => sub { ( $_[0] < $_[1] ) ? 1 : 0 },
14             '<=' => sub { ( $_[0] <= $_[1] ) ? 1 : 0 },
15             '==' => sub { ( $_[0] == $_[1] ) ? 1 : 0 },
16             );
17              
18             sub connect_info {
19 0     0 1 0 my ( $class, $resolver, $node_or_cluster, $args ) = @_;
20              
21 0         0 my ($resolved_node, @keys) =
22             $class->resolve( $resolver, $node_or_cluster, $args );
23              
24 0         0 return $resolver->connect_info( $resolved_node, \@keys );
25             }
26              
27             sub resolve {
28 350     350 1 665 my ( $class, $resolver, $node_or_cluster, $args ) = @_;
29              
30 350         1876 my @keys = $class->keys_from_args($args);
31 350         902 my $key = shift @keys;
32            
33 350 50 33     2234 unless ( exists $args->{strategy_config}
34             && is_array_ref( $args->{strategy_config} ) )
35             {
36 0         0 croak 'strategy_config is not exists or is not array ref';
37             }
38              
39 350         398 my $resolved_node;
40 350         413 my @range_stmts = @{ $args->{strategy_config} };
  350         1418  
41 350         1177 while ( my ( $range_node, $range_stmt ) = splice( @range_stmts, 0, 2 ) ) {
42 1215         3035 my %op_values = @$range_stmt;
43 1215         1701 my $is_matched_stmt = 1;
44 1215         2354 for my $op ( keys %op_values ) {
45 1727 50       3845 croak sprintf('No supported operator (%s)', neat($op)) unless ( exists $OPS{$op});
46 1727 100       3428 unless ( $OPS{$op}->( $key, $op_values{$op} ) ) {
47 865         1080 $is_matched_stmt = 0;
48 865         1394 last;
49             }
50             }
51 1215 100       5323 if ($is_matched_stmt) {
52 350         410 $resolved_node = $range_node;
53 350         832 last;
54             }
55             }
56              
57 350 50       778 unless ( defined $resolved_node ) {
58 0         0 croak sprintf( 'No matched range (key: %f)', $key );
59             }
60              
61 350         1488 return ($resolved_node, @keys);
62             }
63              
64             1;
65              
66             __END__