Crop/Object/Simple.pm untested

filter: Statementstmt 0.0% 0 / 36 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 75 CC 3 SCAR 14.2
CC
3
Cov
0%
Get
12.0
_Create
2.0
Genkey
2.0
CRAP 4.1
1 package Crop::Object::Simple;
2 use base qw/ Crop::Object /;
3  
4 =begin nd
5 Class: Crop::Object::Simple
6 Base class for objects have 'id' key only.
7 =cut
8  
9 use v5.14;
10 use warnings;
11  
12 use Crop::Error;
13 use Crop::Debug;
14  
15 =begin nd
16 Variable: our %Attributes
17 Class attributes:
18  
19 id - single unique identificator
20 =cut
21 our %Attributes = (
22 id => {key => 'ordinal'},
23 # id => {mode => 'read', key => 'ordinal'},
24 );
25  
26 =begin nd
27 Method: _Create ( )
28 Create new object.
29
30 Generate, then set the 'id' attribute.
31
32 Returns:
33 $self - if created
34 undef - in case of error
35 =cut
36 sub _Create ( ) {
37 my $self = shift;
38
39 $self->{id} = $self->WH->create_auto_id($self);
40
41 $self;
42 }
43  
44 =begin nd
45 Method: Genkey ( )
46 Generate primary key.
47
48 Regeneration is prohibited.
49  
50 Use this method to get the exemplar ID before it stored.
51  
52 Returns:
53 id - ok
54 undef - error
55 =cut
56 sub Genkey {
57 my $self = shift;
58 my $class = ref $self;
59
60 not defined $self->{id} or return warn "OBJECT|CRIT: Re-generation of existing key on '$class' exemplar";
61
62 $self->WH->get_id($self);
63 }
64  
65 =begin nd
66 Method: Get (@filter)
67 Get an exemplar from warehouse that satisfy to clause described by @filter.
68
69 Only one element there is in the @filter means 'id'.
70  
71 Parameters:
72 @filter - select clase
73  
74 Returns:
75 an exemplar - if ok
76 false - if an error has acquired
77 =cut
78 sub Get {
79 my $either = shift;
80  
81 my $self;
82
83 if (@_ == 1 and ref $_[0] ne 'HASH') {
84 return unless $_[0] =~ /^\d+$/;
85 $self = $either->SUPER::Get(id => $_[0]);
86 } else {
87 $self = $either->SUPER::Get(@_);
88 }
89  
90 $self;
91 }
92  
93 =begin nd
94 Method: id ( )
95 Get exemplar ID.
96  
97 The warehouse generates id if the exemplar does not exist.
98  
99 Returns:
100 id - an integer
101 =cut
102 sub id {
103 my $self = shift;
104  
105 return $self->{id} // $self->Genkey;
106 }
107  
108 =begin nd
109 Method: _Is_key_defined ( )
110 If defined id?
111
112 Returns:
113 true - defined
114 false - not defined
115 =cut
116 sub _Is_key_defined { defined +shift->{id} }
117  
118 =begin nd
119 Method: Max ($attr)
120 Get maximum of $attr value in all the class.
121  
122 If no $attr provided then 'id' is assumed.
123  
124 Method of class either method of exemplar.
125  
126 Attributes:
127 $attr - attribute name
128  
129 Returns:
130 Maximum value of $attr across all the class exemplars.
131 =cut
132 sub Max {
133 my ($either, $attr) = @_;
134  
135 $attr //= 'id';
136  
137 $either::SUPER->Max($attr);
138 }
139  
140 =begin nd
141 Method: _Prepare_key ( )
142 Do nothing.
143  
144 Redefines <Crop::Object::_Prepare_key>.
145  
146 Always true. Database will generate Id for a Simple object.
147 =cut
148 sub _Prepare_key { 1 }
149  
150 1;