File Coverage

blib/lib/Net/DRI/Data/Changes.pm
Criterion Covered Total %
statement 48 48 100.0
branch 26 28 92.8
condition 14 21 66.6
subroutine 13 13 100.0
pod 0 10 0.0
total 101 120 84.1


line stmt bran cond sub pod time code
1             ## Domain Registry Interface, Handle bundle of changes
2             ##
3             ## Copyright (c) 2005,2008,2011,2013 Patrick Mevzek . All rights reserved.
4             ##
5             ## This file is part of Net::DRI
6             ##
7             ## Net::DRI is free software; you can redistribute it and/or modify
8             ## it under the terms of the GNU General Public License as published by
9             ## the Free Software Foundation; either version 2 of the License, or
10             ## (at your option) any later version.
11             ##
12             ## See the LICENSE file that comes with this distribution for more details.
13             #########################################################################################
14              
15             package Net::DRI::Data::Changes;
16              
17 69     69   928 use strict;
  69         90  
  69         1659  
18 69     69   217 use warnings;
  69         97  
  69         33801  
19              
20             =pod
21              
22             =head1 NAME
23              
24             Net::DRI::Data::Changes - Bundle of changes in Net::DRI
25              
26             =head1 DESCRIPTION
27              
28             Please see the README file for details.
29              
30             =head1 SUPPORT
31              
32             For now, support questions should be sent to:
33              
34             Enetdri@dotandco.comE
35              
36             Please also see the SUPPORT file in the distribution.
37              
38             =head1 SEE ALSO
39              
40             Ehttp://www.dotandco.com/services/software/Net-DRI/E
41              
42             =head1 AUTHOR
43              
44             Patrick Mevzek, Enetdri@dotandco.comE
45              
46             =head1 COPYRIGHT
47              
48             Copyright (c) 2005,2008,2011,2013 Patrick Mevzek .
49             All rights reserved.
50              
51             This program is free software; you can redistribute it and/or modify
52             it under the terms of the GNU General Public License as published by
53             the Free Software Foundation; either version 2 of the License, or
54             (at your option) any later version.
55              
56             See the LICENSE file that comes with this distribution for more details.
57              
58             =cut
59              
60             ##############################################################################################################################
61              
62             sub new
63             {
64 10     10 0 22 my ($class,$type,$op,$el)=@_;
65              
66 10         15 my $self={}; ## { 'type' => [ toadd, todel, toset ] } type=host,ip,status,contact,etc...
67 10         13 bless($self,$class);
68              
69 10 50 66     45 if (defined($type) && defined($op) && defined($el))
      66        
70             {
71 6         12 $self->{$type}=[];
72 6 100       26 $self->{$type}->[0]=$el if ($op=~m/^(?:0|add)$/);
73 6 100       20 $self->{$type}->[1]=$el if ($op=~m/^(?:1|del)$/);
74 6 100       20 $self->{$type}->[2]=$el if ($op=~m/^(?:2|set)$/);
75             }
76              
77 10         21 return $self;
78             }
79              
80 1     1 0 3 sub new_add { return shift->new(shift,'add',shift); }
81 1     1 0 3 sub new_del { return shift->new(shift,'del',shift); }
82 1     1 0 4 sub new_set { return shift->new(shift,'set',shift); }
83              
84             sub types
85             {
86 34     34 0 2413 my ($self,$type)=@_;
87 34 100       60 if (! defined $type)
88             {
89 18         51 my @r=sort { $a cmp $b } keys %$self;
  6         9  
90 18         61 return @r;
91             }
92 16         15 my @r;
93 16 100 66     68 return @r unless (exists($self->{$type}) && defined($self->{$type}));
94 13 100       52 push @r,'add' if (defined($self->{$type}->[0]));
95 13 100       24 push @r,'del' if (defined($self->{$type}->[1]));
96 13 100       16 push @r,'set' if (defined($self->{$type}->[2]));
97 13         49 return @r;
98             }
99              
100             sub _el
101             {
102 38     38   39 my ($self,$pos,$type,$new)=@_;
103 38 100       58 unless (defined($new))
104             {
105 30 100 66     89 return unless (exists($self->{$type}) && defined($self->{$type}));
106 24         59 return $self->{$type}->[$pos];
107             }
108 8 100       21 $self->{$type}=[] unless (exists($self->{$type}));
109 8         9 $self->{$type}->[$pos]=$new;
110              
111 8         12 return $self;
112             }
113              
114 16     16 0 27 sub add { return shift->_el(0,shift,shift); }
115 16     16 0 21 sub del { return shift->_el(1,shift,shift); }
116 6     6 0 10 sub set { return shift->_el(2,shift,shift); }
117              
118             sub all_defined
119             {
120 20     20 0 24 my ($self,$type)=@_;
121 20 100 66     124 return () unless (defined($type) && $type && exists($self->{$type}) && defined($self->{$type}));
      66        
      66        
122 10         7 return (grep { defined } @{$self->{$type}});
  18         55  
  10         17  
123             }
124              
125             sub is_empty
126             {
127 3     3 0 4 my $self=shift;
128 3         4 my @o=map { $self->all_defined($_) } $self->types();
  3         5  
129 3 50       17 return @o? 0 : 1;
130             }
131              
132             ##############################################################################################################################
133             1;