File Coverage

blib/lib/Cassandra/Client/Config.pm
Criterion Covered Total %
statement 14 49 28.5
branch 0 30 0.0
condition 0 6 0.0
subroutine 5 6 83.3
pod 0 1 0.0
total 19 92 20.6


line stmt bran cond sub pod time code
1             package Cassandra::Client::Config;
2             our $AUTHORITY = 'cpan:TVDW';
3             $Cassandra::Client::Config::VERSION = '0.21';
4 13     13   227 use 5.010;
  13         53  
5 13     13   75 use strict;
  13         51  
  13         501  
6 13     13   65 use warnings;
  13         22  
  13         739  
7              
8 13     13   25322 use Ref::Util qw/is_plain_arrayref is_plain_coderef is_blessed_ref/;
  13         36515  
  13         1423  
9 13     13   11150 use Cassandra::Client::Policy::Auth::Password;
  13         45  
  13         8492  
10              
11             sub new {
12 0     0 0   my ($class, $config)= @_;
13              
14 0           my $self= bless {
15             anyevent => 0,
16             contact_points => undef,
17             port => 9042,
18             cql_version => undef,
19             keyspace => undef,
20             compression => undef,
21             default_consistency => undef,
22             default_idempotency => 0,
23             max_page_size => 5000,
24             max_connections => 2,
25             timer_granularity => 0.1,
26             request_timeout => 11,
27             warmup => 0,
28             max_concurrent_queries => 1000,
29             tls => 0,
30             protocol_version => 4,
31              
32             throttler => undef,
33             command_queue => undef,
34             retry_policy => undef,
35             load_balancing_policy => undef,
36             authentication => undef,
37              
38             stats_hook => undef,
39             }, $class;
40              
41 0 0         if (my $cp= $config->{contact_points}) {
42 0 0         if (is_plain_arrayref($cp)) {
43 0           @{$self->{contact_points}=[]}= @$cp;
  0            
44 0           } else { die "contact_points must be an arrayref"; }
45 0           } else { die "contact_points not specified"; }
46              
47             # Booleans
48 0           for (qw/anyevent warmup tls default_idempotency/) {
49 0 0         if (exists($config->{$_})) {
50 0           $self->{$_}= !!$config->{$_};
51             }
52             }
53              
54             # Numbers, ignore undef
55 0           for (qw/port timer_granularity request_timeout max_connections max_concurrent_queries/) {
56 0 0         if (defined($config->{$_})) {
57 0           $self->{$_}= 0+ $config->{$_};
58             }
59             }
60             # Numbers, undef actually means undef
61 0           for (qw/max_page_size/) {
62 0 0         if (exists($config->{$_})) {
63 0 0         $self->{$_}= defined($config->{$_}) ? (0+ $config->{$_}) : undef;
64             }
65             }
66              
67             # Strings
68 0           for (qw/cql_version keyspace compression default_consistency/) {
69 0 0         if (exists($config->{$_})) {
70 0 0         $self->{$_}= defined($config->{$_}) ? "$config->{$_}" : undef;
71             }
72             }
73              
74             # Coderefs
75 0           for (qw/stats_hook/) {
76 0 0         if (defined($config->{$_})) {
77 0 0         die "$_ must be a CODE reference" unless is_plain_coderef($config->{$_});
78 0           $self->{$_}= $config->{$_};
79             }
80             }
81              
82             # Policies
83 0           for (qw/throttler retry_policy command_queue load_balancing_policy authentication/) {
84 0 0         if (exists($config->{$_})) {
85             die "$_ must be a blessed reference implementing the correct API"
86 0 0         unless is_blessed_ref($config->{$_});
87 0           $self->{$_}= $config->{$_};
88             }
89             }
90              
91 0 0 0       if (exists($config->{username}) || exists($config->{password})) {
92             $self->{authentication}= Cassandra::Client::Policy::Auth::Password->new(
93             username => $config->{username},
94             password => $config->{password},
95 0           );
96             }
97              
98 0 0         if (exists $config->{protocol_version}) {
99 0 0 0       if ($config->{protocol_version} == 3 || $config->{protocol_version} == 4) {
100 0           $self->{protocol_version}= 0+ $config->{protocol_version};
101             } else {
102 0           die "Invalid protocol_version: must be one of [3, 4]";
103             }
104             }
105              
106 0           return $self;
107             }
108              
109             1;
110              
111             __END__