Application/Part.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 159 CC 10 SCAR 27.4
CC
10
Cov
0%
register_rights
30.0
import
20.0
init
6.0
CRAP 15.4
1 package QBit::Application::Part;
2 $QBit::Application::Part::VERSION = '0.017';
3 use qbit;
4  
5 use base qw(QBit::Class);
6  
7 __PACKAGE__->mk_accessors(qw(app));
8  
9 sub init {
10 my ($self) = @_;
11  
12 $self->SUPER::init();
13  
14 throw gettext('Required opt "app" must be QBit::Application descendant')
15 unless $self->{'app'} && $self->{'app'}->isa('QBit::Application');
16  
17 weaken($self->{'app'});
18 }
19  
20 sub model_accessors {
21 my ($class, %accessors) = @_;
22  
23 my $pkg_stash = package_stash($class);
24  
25 no strict 'refs';
26 while (my ($acc_name, $acc_class) = each(%accessors)) {
27 my $accessor_from_app = $pkg_stash->{'__MODEL_ACCESSORS__'}{$acc_name} || $acc_name;
28  
29 *{"${class}::${acc_name}"} = sub {
30 $_[0]->{'__ACCESSORS__'}{$acc_name} //= do {
31 my $model = $_[0]->app->$accessor_from_app;
32  
33 throw gettext(
34 'Class "%s" expects application accessor "%s" used through model accessor "%s" to be "%s" descendant but got %s',
35 $class,
36 $accessor_from_app,
37 $acc_name,
38 $acc_class,
39 ref($model)
40 )
41 unless ref($model) && $model->isa($acc_class);
42  
43 weaken($model);
44  
45 return $model;
46 };
47 };
48 }
49 }
50  
51 sub register_rights {
52 my ($class, $data) = @_;
53  
54 my $pkg_stash = package_stash($class);
55  
56 $pkg_stash->{'__RIGHT_GROUPS__'} = {} unless exists($pkg_stash->{'__RIGHT_GROUPS__'});
57 $pkg_stash->{'__RIGHTS__'} = {} unless exists($pkg_stash->{'__RIGHTS__'});
58  
59 foreach my $group (@$data) {
60 $group->{'name'} = '__UNDEFINED__' unless exists($group->{'name'});
61 $pkg_stash->{'__RIGHT_GROUPS__'}{$group->{'name'}} = $group->{'description'} if exists($group->{'description'});
62  
63 while (my ($right, $right_name) = each(%{$group->{'rights'}})) {
64 $pkg_stash->{'__RIGHTS__'}{$right} = {name => $right_name, group => $group->{'name'}};
65 }
66 }
67 }
68  
69 sub import {
70 my ($package, %opts) = @_;
71  
72 my $pkg_stash = package_stash($package);
73  
74 $pkg_stash->{'__MODEL_ACCESSORS__'} = $opts{'models'} || {};
75  
76 my $app_pkg = $opts{'app_pkg'};
77 unless ($app_pkg) {
78 my $i = 1;
79 while ($app_pkg = caller($i++)) {
80 last if $app_pkg->isa('QBit::Application');
81 }
82 }
83  
84 throw gettext('Use only in QBit::Application descendant')
85 unless $app_pkg && $app_pkg->isa('QBit::Application');
86  
87 my $app_pkg_stash = package_stash($app_pkg);
88  
89 my $rights = {};
90 package_merge_isa_data(
91 $package, $rights,
92 sub {
93 my ($ipackage, $res) = @_;
94  
95 my $ipkg_stash = package_stash($ipackage);
96 $res->{'__RIGHTS__'} = {%{$res->{'__RIGHTS__'} || {}}, %{$ipkg_stash->{'__RIGHTS__'} || {}}};
97 $res->{'__RIGHT_GROUPS__'} =
98 {%{$res->{'__RIGHT_GROUPS__'} || {}}, %{$ipkg_stash->{'__RIGHT_GROUPS__'} || {}}};
99 },
100 __PACKAGE__
101 );
102  
103 $app_pkg_stash->{'__RIGHT_GROUPS__'} =
104 {%{$app_pkg_stash->{'__RIGHT_GROUPS__'} || {}}, %{$rights->{'__RIGHT_GROUPS__'} || {}}};
105  
106 $app_pkg_stash->{'__RIGHTS__'} = {%{$app_pkg_stash->{'__RIGHTS__'} || {}}, %{$rights->{'__RIGHTS__'} || {}}};
107  
108 {
109 no strict 'refs';
110 foreach my $method (qw(check_rights cur_user)) {
111 *{"${package}::${method}"} = sub {shift->app->$method(@_)};
112 }
113 }
114 }
115  
116 TRUE;