blib/lib/Yote/YapiServer/BaseObj.pm untested

filter: Statementstmt 0.0% 0 / 45 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 27 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 93 CC 5 SCAR 25.5
CC
5
Cov
0%
to_client_hash
30.0
field_access
2.0
method_defs
2.0
CRAP 12.8
1 package Yote::YapiServer::BaseObj;
2  
3 use strict;
4 use warnings;
5 use base 'Yote::SQLObjectStore::SQLite::Obj', 'Yote::SQLObjectStore::MariaDB::Obj';
6  
7 #----------------------------------------------------------------------
8 # Class data accessors — look up %HASH in the calling class
9 # Named to avoid colliding with get_foo/set_foo instance accessors
10 #----------------------------------------------------------------------
11  
12 sub field_access {
13 my ($self) = @_;
14 my $class = ref($self) || $self;
15 no strict 'refs';
16 return \%{"${class}::FIELD_ACCESS"};
17 }
18  
19 sub method_defs {
20 my ($self) = @_;
21 my $class = ref($self) || $self;
22 no strict 'refs';
23 return \%{"${class}::METHODS"};
24 }
25  
26 #----------------------------------------------------------------------
27 # Client class name — strips Yote::YapiServer:: prefix
28 #----------------------------------------------------------------------
29  
30 sub _client_class_name {
31 my ($self) = @_;
32 my $class = ref($self) || $self;
33 $class =~ s/^Yote::YapiServer::App:://;
34 $class =~ s/^Yote::YapiServer:://;
35 return $class;
36 }
37  
38 #----------------------------------------------------------------------
39 # Default serialization — filters fields by %FIELD_ACCESS rules
40 #----------------------------------------------------------------------
41  
42 sub to_client_hash {
43 my ($self, $session, $viewer) = @_;
44 my %result;
45  
46 my $field_access = $self->field_access;
47 my $cols = $self->cols;
48 my $owner = eval { $self->get_owner };
49 my $is_owner = $viewer && $owner && $owner->id eq $viewer->id;
50 my $is_admin = $viewer && $viewer->get_is_admin;
51  
52 for my $field (keys %$field_access) {
53 my $rule = $field_access->{$field};
54  
55 next if $rule->{never};
56 next if $rule->{owner_only} && !$is_owner && !$is_admin;
57 next if $rule->{admin_only} && !$is_admin;
58  
59 # Use get() directly — AUTOLOAD-generated getters aren't seen by can()
60 next unless exists $cols->{$field};
61 $result{$field} = $self->get($field);
62 }
63  
64 return \%result;
65 }
66  
67 1;
68  
69 __END__