File Coverage

blib/lib/EntityModel/BaseClass.pm
Criterion Covered Total %
statement 24 56 42.8
branch 3 14 21.4
condition 0 8 0.0
subroutine 7 10 70.0
pod 4 4 100.0
total 38 92 41.3


line stmt bran cond sub pod time code
1             package EntityModel::BaseClass;
2             $EntityModel::BaseClass::VERSION = '0.016';
3 1     1   842 use strict;
  1         2  
  1         35  
4 1     1   7 use warnings;
  1         2  
  1         26  
5              
6 1     1   6 use Scalar::Util ();
  1         2  
  1         18  
7 1     1   6 use EntityModel::Log ':all';
  1         1  
  1         894  
8              
9             =head2 new
10              
11             Basic constructor. Will populate $self with any parameters passed.
12              
13             Returns the new instance.
14              
15             =cut
16              
17             sub new {
18 1     1 1 89 my $class = shift;
19 1         3 my %data;
20 1 50       5 if(ref($_[0]) eq 'HASH') {
21 0         0 %data = %{$_[0]};
  0         0  
22             } else {
23 1 50       7 if(@_ % 2) {
24 0   0     0 logStack("Bad element list for [%s] - %s", $class, join(',', map { $_ // 'undef' } @_));
  0         0  
25             }
26 1         3 %data = @_;
27             }
28 1         4 my $self = bless \%data, $class;
29 1         8 my @defaults = EntityModel::Class::has_defaults($class);
30 1 50       9 return $self unless @defaults;
31              
32 0         0 foreach my $attr (grep { !exists $data{$_} } @defaults) {
  0         0  
33 0         0 my $def = EntityModel::Class::_attrib_info($class, $attr);
34 0         0 my $v = $def->{default};
35 0 0 0     0 $v = $v->() if (ref($v) // '') eq 'CODE';
36             # Still aliased to $self
37 0         0 $data{$attr} = $v;
38             }
39 0         0 return $self;
40             }
41              
42             =head2 clone
43              
44             Shallow clone implementation.
45              
46             Returns a new instance with a copy of everything in the hashref.
47              
48             =cut
49              
50             sub clone {
51 0     0 1 0 my $self = shift;
52 0         0 return bless { %$self }, ref $self;
53             }
54              
55             =head2 dump
56              
57             Simple method to dump out this object and all attributes.
58              
59             =cut
60              
61             sub dump {
62 0     0 1 0 my $self = shift;
63             my $out = shift || sub {
64 0     0   0 my $k = shift;
65 0         0 my $depth = shift;
66 0         0 my $v = shift // '';
67 0         0 print((' ' x $depth) . "$k = $v\n");
68 0   0     0 };
69 0   0     0 my $depth = shift // 0;
70              
71 0         0 $out->(ref($self), $depth, $self);
72 0         0 foreach my $k (sort $self->ATTRIBS) {
73 0         0 my $v = $self->$k();
74 0 0       0 if(eval { $v->can('dump'); }) {
  0 0       0  
    0          
75 0         0 $out->($k, $depth + 1, ':');
76 0         0 $v->dump($out, $depth + 1);
77             } elsif(ref $v eq 'ARRAY') {
78 0         0 $out->($k, $depth + 1, '[' . join(',', @$v) . ']');
79             } elsif(ref $v eq 'HASH') {
80 0         0 $out->($k, $depth + 1, '{' . (map { $_ . ' => ' . $v->{$_} } sort keys %$v) . '}');
  0         0  
81             } else {
82 0         0 $out->($k, $depth + 1, $v);
83             }
84             }
85 0         0 $self;
86             }
87              
88             =head2 sap
89              
90             Generate a coderef that takes a weakened value of $self.
91              
92             Usage:
93              
94             push @handler, $obj->sap(sub {
95             my $self = shift;
96             $self->do_something;
97             });
98              
99             =cut
100              
101             sub sap {
102 1     1 1 4 my ($self, $sub) = @_;
103 1         5 Scalar::Util::weaken $self;
104             return sub {
105 4     4   14 $self->$sub(@_);
106 1         15 };
107             }
108              
109             1;