Nitesi/Provider/Role.pm untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 127 CC 16 SCAR 44.0
CC
16
Cov
0%
_build_attribute_map
132.0
_lookup_attributes_from_moo
42.0
_base_role
2.0
CRAP 81.5
1 package Nitesi::Provider::Role;
2  
3 use strict;
4 use warnings;
5  
6 use Moo::Role;
7  
8 =head1 NAME
9  
10 Nitesi::Provider::Role - Provider role for Nitesi Shop Machine
11  
12 =head1 DESCRIPTION
13  
14 Provides methods for dynamically composing objects from a number
15 of roles.
16  
17 =head1 ATTRIBUTES
18  
19 =head2 api_info
20  
21 Returns hash reference with API information.
22  
23 =cut
24  
25 has api_info => (
26 is => 'rw',
27 );
28  
29 =head2 field_map
30  
31 Maps field names.
32  
33 =cut
34  
35 has field_map => (
36 is => 'rw',
37 );
38  
39 =head2 attribute_map
40  
41 Maps attribute to role providing the attribute (read-only).
42  
43 =cut
44  
45 has attribute_map => (
46 is => 'ro',
47 lazy => 1,
48 builder => '_build_attribute_map',
49 );
50  
51 =head2 base_role
52  
53 Base role for the current object (read-only).
54  
55 =cut
56  
57 has base_role => (
58 is => 'ro',
59 lazy => 1,
60 builder => '_base_role',
61 );
62  
63 sub _base_role {
64 my $self = shift;
65 my @classes = grep {$_ ne 'WITH' && $_ ne 'AND'} split(/__/, ref($self));
66  
67 return $classes[0];
68 }
69  
70 sub _build_attribute_map {
71 my $self = shift;
72 my (@classes, $name, $value, @attributes, %map, @rt_atts, %rt_map,
73 $virtual, $foreign, $inherit, $att_settings);
74  
75 @classes = grep {$_ ne 'WITH' && $_ ne 'AND'} split(/__/, ref($self));
76  
77 # determine attributes of this and parent classes
78 for my $role (@classes) {
79 if (exists $self->api_info->{$role}->{attributes}) {
80 $att_settings = $self->api_info->{$role}->{attributes};
81 }
82 else {
83 $att_settings = {};
84 }
85  
86 if (exists $self->api_info->{$role}->{virtual}) {
87 $virtual = $self->api_info->{$role}->{virtual};
88 }
89 else {
90 $virtual = {};
91 }
92  
93 if (exists $self->api_info->{$role}->{foreign}) {
94 $foreign = $self->api_info->{$role}->{foreign};
95 }
96 else {
97 $foreign = {};
98 }
99  
100 if (exists $self->api_info->{$role}->{inherit}) {
101 $inherit = $self->api_info->{$role}->{inherit};
102 }
103 elsif (exists $self->api_info->{$role}->{base}) {
104 $inherit = $self->api_info->{$role}->{base};
105 }
106  
107 $self->_lookup_attributes_from_moo($role, $role, \%map, $att_settings, $foreign, $virtual);
108  
109 if ($inherit && $classes[0] eq $role) {
110 $self->_lookup_attributes_from_moo($inherit, $role, \%map, $att_settings, $foreign, $virtual);
111 }
112  
113 @rt_atts = @{$Role::Tiny::INFO{$role}->{attributes} || []};
114 %rt_map = @rt_atts;
115  
116 while (($name, $value) = each %rt_map) {
117 next if $name =~ /^api_/;
118  
119 $map{$name} = {role => $role};
120  
121 if (exists $virtual->{$name}) {
122 $map{$name}->{virtual} = $virtual->{$name};
123 }
124 }
125 }
126  
127 if ($self->{field_map}) {
128 my ($orig, $mapped);
129  
130 while (($orig, $mapped) = each %{$self->{field_map}}) {
131 if (exists $map{$orig}) {
132 $map{$orig}->{map} = $mapped;
133 }
134 else {
135 die "Wrong entry in field_map: $orig.\n";
136 }
137 }
138 }
139  
140 return \%map;
141 }
142  
143 sub _lookup_attributes_from_moo {
144 my ($self, $lookup, $role, $mapref, $att_settings, $foreign, $virtual) = @_;
145 my ($name, $value);
146  
147 while (($name, $value) = each %{$Moo::MAKERS{$lookup}->{constructor}->{attribute_specs}}) {
148 next if $name =~ /^api_/;
149  
150 $mapref->{$name} = {role => $role};
151  
152 if (exists $att_settings->{$name}) {
153 if (defined $att_settings->{$name}) {
154 $mapref->{$name}->{map} = $att_settings->{$name};
155 }
156 else {
157 delete $mapref->{$name};
158 }
159 }
160  
161 if (exists $virtual->{$name}) {
162 $mapref->{$name}->{virtual} = $virtual->{$name};
163 }
164  
165 if (exists $foreign->{$name}) {
166 $mapref->{$name}->{foreign} = $foreign->{$name};
167 }
168 }
169 }
170  
171 =head1 AUTHOR
172  
173 Stefan Hornburg (Racke), <racke@linuxia.de>
174  
175 =head1 LICENSE AND COPYRIGHT
176  
177 Copyright 2011-2013 Stefan Hornburg (Racke) <racke@linuxia.de>.
178  
179 This program is free software; you can redistribute it and/or modify it
180 under the terms of either: the GNU General Public License as published
181 by the Free Software Foundation; or the Artistic License.
182  
183 See http://dev.perl.org/licenses/ for more information.
184  
185 =cut
186  
187 1;