Crop/Object/Attrs.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 106 CC 4 SCAR 12.0
CC
4
Cov
0%
new
6.0
first
6.0
have
6.0
CRAP 3.3
1 package Crop::Object::Attrs;
2  
3 =begin nd
4 Class: Crop::Object::Attrs
5 Container for <Crop::Object::Attr> of class attributes declaration.
6
7 OOP-class, but do not inherits to the <Crop::Object>.
8
9 Attributes:
10
11 attr - Collection of <Crop::Object::Attr>
12 class - class of declaration
13 extern - hash of Crop::Object::Extern::* describes link to an external object
14 source - raw source of attributes declaration
15 =cut
16  
17 use v5.14;
18 use warnings;
19 no warnings 'experimental::smartmatch';
20  
21 use Crop::Debug;
22 use Crop::Error;
23 use Crop::Object::Attr;
24 use Crop::Object::Collection;
25 use Crop::Object::Constants;
26 use Crop::Object::Extern;
27  
28 =begin nd
29 Constructor: new (%attr)
30 Construct Collection of attributes <Crop::Object::Attr>.
31
32 $attr{class} is required.
33
34 Parameters:
35 %attr - see 'Attributes' in class description
36  
37 Returns:
38 $self - if all right
39 undef - otherwise
40 =cut
41 sub new {
42 my ($class, %in) = @_;
43
44 exists $in{class} or return warn "Attrs constructor requires the 'class' argument.";
45
46 my $self = bless {
47 attr => Crop::Object::Collection->new('Crop::Object::Attr'),
48 class => $in{class},
49 extern => {},
50 source => {},
51 %in,
52 }, $class;
53  
54 while (my ($name, $desc) = each %{$self->{source}}) {
55 next if $name eq 'EXT';
56 my $attr = Crop::Object::Attr->new(name => $name, source => $desc);
57 $self->{attr}->Push($attr);
58 }
59  
60 $self;
61 }
62  
63 =begin nd
64 Method: extern ($name)
65 Get the extern declaration for $name attribute
66  
67 Parameters:
68 $name - attribute name
69
70 Returns:
71 The particular object Crop::Object::Extern::*
72 =cut
73 sub extern {
74 my ($self, $name) = @_;
75 my $class = ref $self;
76
77 exists $self->{extern}{$name} and return $self->{extern}{$name};
78
79 exists $self->{source}{EXT} or return warn "NODECL|CRIT: EXT not is in class declaration";
80 exists $self->{source}{EXT}{$name} or return warn "OBJECT|CRIT: Not EXT declaration presence for name $name";
81
82 my $ext = Crop::Object::Extern->new($self->{source}{EXT}{$name}) or return warn "OBJECT|CRIT: extern declaration is not valid for $name";
83 my $rc = $self->{extern}{$name} = $ext;
84
85 $rc;
86 }
87  
88 =begin nd
89 Method: first ($key, $val)
90 Get the first <Crop::Object::Attr> that has key named $key and corresponging $val as value
91
92 Parameters:
93 $key - name of the key
94 $val - value for $key
95
96 Returns:
97 attribute object - if found
98 undef - otherwise
99 =cut
100 sub first {
101 my ($self, $key, $val) = @_;
102
103 for ($self->{attr}->List) {
104 return $_ if $_->has($key, $val);
105 }
106
107 return;
108 }
109  
110 =begin nd
111 Method: have ($discipline, $name)
112 Get attribute by name.
113  
114 Parameters:
115 $discipline - predefined constant (see <Crop::Object::Constants>)
116 $name - attribute name
117  
118 Returns:
119 <Crop::Object::Attr> object - if found
120 undef - if not found
121 =cut
122 sub have {
123 my $self = shift;
124 my $name = pop;
125 my $discipline = shift || ANY;
126  
127 my $attribute = $self->{attr}->First(name => $name) or return;
128
129 $attribute->of_type($discipline) ? $attribute : undef;
130 }
131  
132 =begin nd
133 Method: List ( )
134 Get the attributes (<Crop::Object::Attr>) list.
135  
136 Will call <Crop::Object::Collection::List> so calling context matters. The initial
137 capital letter 'L' in the method name reminds it.
138  
139 Returns:
140 items arrayref - in scalar context
141 items array - in list context
142
143 See <Crop::Object::Collection::List>.
144 =cut
145 sub List { shift->{attr}->List }
146  
147 =begin nd
148 Method: source ( )
149 Get declaration source.
150  
151 Returns:
152 Hashref of raw source of attributes declaration in form of the class declaration.
153 =cut
154 sub source { shift->{source} }
155  
156 1;