Crop/Object/Sorted.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 98 CC 5 SCAR 18.6
CC
5
Cov
0%
Move
20.0
_last_sortn
6.0
_Is_key_defined
2.0
CRAP 6.4
1 package Crop::Object::Sorted;
2 use base qw/ Crop::Object /;
3  
4 =begin nd
5 Class: Crop::Object::Sorted
6 Base class for objects have 'sortn' attribute for global sorting.
7
8 The 'sortn' attribute stores even integer values for effective 'update' operation.
9
10 Initial sortn=2.
11 =cut
12  
13 use v5.14;
14 use warnings;
15  
16 use Crop::Error;
17  
18 use Crop::Debug;
19  
20 =begin nd
21 Constant: DISCRETE
22 The 'sortn' step.
23 =cut
24 use constant {
25 DISCRETE => 2,
26 };
27  
28 =begin nd
29 Variable: our %Attributes
30 Class attributes:
31  
32 sortn - sorting number
33 =cut
34 our %Attributes = (
35 sortn => {mode => 'read/write'},
36 );
37  
38 =begin nd
39 Method: _Is_key_defined ( )
40 The sortn attribute must be defined before the Save-operation.
41  
42 Returns:
43 true - if sortn is defined, and Save can be performed
44 false - otherwise
45 =cut
46 sub _Is_key_defined {
47 my $self = shift;
48  
49 defined $self->{sortn};
50 }
51  
52 =begin nd
53 Method: _last_sortn ( )
54 Get last 'sortn' attribute.
55
56 Returns:
57 integer - even num; 0 if class is empty
58 =cut
59 sub _last_sortn {
60 my $either = shift;
61
62 my $last = $either->Get(
63 SORT => 'sortn DESC',
64 LIMIT => 1,
65 );
66
67 $last ? $last->{sortn} : 0;
68 }
69  
70 =begin nd
71 Method: Move ($position)
72 Move exemplar to the new position.
73
74 Parameters:
75 position - new position=sortn/2
76
77 Returns:
78 $self - if moved ok
79 undef - otherwise
80 =cut
81 sub Move {
82 my ($self, $position) = @_;
83 my $class = ref $self;
84
85 my $self_sortn = $self->{sortn};
86 my $target_sortn = $position * DISCRETE;
87  
88 my $target = $class->Get(sortn => $target_sortn);
89 $target_sortn = $class->_last_sortn unless $target;
90
91 if ($self_sortn < $target_sortn) {
92 # shift down difference
93 $class->Global('UPDATE', sortn => \'sortn - 1', [
94 sortn => {GT => $self_sortn},
95 sortn => {LE => $target_sortn},
96 ]) or return warn 'OBJECT: Can not Move up Sorted';
97
98 # move self to target
99 $self->sortn($target_sortn);
100 $self->Save or return warn 'OBJECT: Can not Save in Move Sorted';
101
102 # last shift down difference
103 $class->Global('UPDATE', sortn => \'sortn - 1', [
104 sortn => {GT => $self_sortn},
105 sortn => {LT => $target_sortn},
106 ]) or return warn 'OBJECT: Can not Move Sorted';
107 } elsif ($target_sortn < $self_sortn) {
108 # shift up difference
109 $class->Global('UPDATE', sortn => \'sortn + 1', [
110 sortn => {GE => $target_sortn},
111 sortn => {LT => $self_sortn},
112 ]) or return warn 'OBJECT: Can not Move down Sorted';
113
114 # move self to target
115 $self->sortn($target_sortn);
116 $self->Save or return warn 'OBJECT: Can not Save in Move Sorted';
117
118 # last shift up difference
119 $class->Global('UPDATE', sortn => \'sortn + 1', [
120 sortn => {GT => $target_sortn},
121 sortn => {LT => $self_sortn},
122 ]) or return warn 'OBJECT: Can not Move down Sorted';
123 }
124
125 $self;
126 }
127  
128 =begin nd
129 Method: _next_sortn ( )
130 Get the number of next sortn attribute.
131
132 Returns:
133 Even integer.
134 =cut
135 sub _next_sortn {
136 my $self = shift;
137  
138 $self->_last_sortn + 2;
139 }
140  
141 =begin nd
142 Method: _Prepare_key ( )
143 Set real value for the 'sortn' attribute.
144
145 Returns:
146 true
147 =cut
148 sub _Prepare_key {
149 my $self = shift;
150
151 $self->{sortn} = $self->_next_sortn;
152
153 1;
154 }
155  
156 =begin nd
157 Method: Shift_down($n)
158 Shift to the left all the items greater or equal $n. Interface.
159
160 Execute immediately.
161  
162 =cut
163 sub Shift_down {
164 my ($class, $n) = @_;
165
166 $class->_shift_down($n) for 1 .. DISCRETE;
167 }
168  
169 =begin nd
170 Method: _shift_down ($n)
171 Shift to the left all the items greater or equal $n. Inner-class worker.
172
173 Execute immediately.
174
175 Parameters:
176 $n - sortn anchor
177 =cut
178 sub _shift_down {
179 my ($self, $n) = @_;
180
181 $self->Global('UPDATE', sortn => \'sortn - 1', {sortn => {GE => $n}});
182 }
183  
184 =begin nd
185 Method: _shift_up ($n)
186 Shift to the right all the items greater or equal $n.
187
188 Execute immediately.
189  
190 Parameters:
191 $n - sortn anchor
192  
193 =cut
194 sub _shift_up {
195 my ($self, $n) = @_;
196
197 $self->Global('UPDATE', sortn => \'sortn + 1', {sortn => {GE => $n}});
198 }
199  
200 1;