File Coverage

blib/lib/Net/DRI/Data/Changes.pm
Criterion Covered Total %
statement 48 48 100.0
branch 26 28 92.8
condition 15 21 71.4
subroutine 13 13 100.0
pod 0 10 0.0
total 102 120 85.0


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 60     60   1295 use strict;
  60         117  
  60         3284  
18 60     60   320 use warnings;
  60         96  
  60         38419  
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 33 my ($class,$type,$op,$el)=@_;
65              
66 10         16 my $self={}; ## { 'type' => [ toadd, todel, toset ] } type=host,ip,status,contact,etc...
67 10         64 bless($self,$class);
68              
69 10 50 66     66 if (defined($type) && defined($op) && defined($el))
      66        
70             {
71 6         13 $self->{$type}=[];
72 6 100       28 $self->{$type}->[0]=$el if ($op=~m/^(?:0|add)$/);
73 6 100       29 $self->{$type}->[1]=$el if ($op=~m/^(?:1|del)$/);
74 6 100       28 $self->{$type}->[2]=$el if ($op=~m/^(?:2|set)$/);
75             }
76              
77 10         25 return $self;
78             }
79              
80 1     1 0 5 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 6 sub new_set { return shift->new(shift,'set',shift); }
83              
84             sub types
85             {
86 34     34 0 1983 my ($self,$type)=@_;
87 34 100       71 if (! defined $type)
88             {
89 18         72 my @r=sort { $a cmp $b } keys %$self;
  6         35  
90 18         77 return @r;
91             }
92 16         16 my @r;
93 16 100 66     84 return @r unless (exists($self->{$type}) && defined($self->{$type}));
94 13 100       41 push @r,'add' if (defined($self->{$type}->[0]));
95 13 100       32 push @r,'del' if (defined($self->{$type}->[1]));
96 13 100       42 push @r,'set' if (defined($self->{$type}->[2]));
97 13         61 return @r;
98             }
99              
100             sub _el
101             {
102 38     38   46 my ($self,$pos,$type,$new)=@_;
103 38 100       63 unless (defined($new))
104             {
105 30 100 66     108 return unless (exists($self->{$type}) && defined($self->{$type}));
106 24         69 return $self->{$type}->[$pos];
107             }
108 8 100       42 $self->{$type}=[] unless (exists($self->{$type}));
109 8         21 $self->{$type}->[$pos]=$new;
110              
111 8         15 return $self;
112             }
113              
114 16     16 0 39 sub add { return shift->_el(0,shift,shift); }
115 16     16 0 33 sub del { return shift->_el(1,shift,shift); }
116 6     6 0 13 sub set { return shift->_el(2,shift,shift); }
117              
118             sub all_defined
119             {
120 20     20 0 28 my ($self,$type)=@_;
121 20 100 66     161 return () unless (defined($type) && $type && exists($self->{$type}) && defined($self->{$type}));
      100        
      66        
122 10         11 return (grep { defined } @{$self->{$type}});
  18         60  
  10         23  
123             }
124              
125             sub is_empty
126             {
127 3     3 0 3 my $self=shift;
128 3         8 my @o=map { $self->all_defined($_) } $self->types();
  3         8  
129 3 50       34 return @o? 0 : 1;
130             }
131              
132             ##############################################################################################################################
133             1;