File Coverage

blib/lib/EO.pm
Criterion Covered Total %
statement 22 24 91.6
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 30 32 93.7


line stmt bran cond sub pod time code
1             package EO;
2              
3 18     18   173237 use strict;
  18         44  
  18         636  
4 18     18   105 use warnings;
  18         33  
  18         457  
5              
6 18     18   10045 use EO::Error;
  18         114  
  18         590  
7 18     18   17760 use Data::UUID;
  18         20415  
  18         1231  
8 18     18   10636 use EO::Attributes;
  18         113  
  18         596  
9 18     18   12770 use EO::NotAttributes;
  18         45  
  18         496  
10 18     18   16905 use Class::Accessor::Chained;
  18         72600  
  18         198  
11 18     18   10705 use Data::Structure::Util qw( get_blessed );
  0            
  0            
12             use Storable;
13              
14             use base qw( Class::Accessor::Chained );
15              
16             our $VERSION = 0.96;
17             our $AUTOLOAD;
18              
19             exception EO::Error::New;
20             exception EO::Error::Method;
21             exception EO::Error::Method::Private extends => 'EO::Error::Method';
22             exception EO::Error::Method::NotFound extends => 'EO::Error::Method';
23             exception EO::Error::Method::Abstract extends => 'EO::Error::Method';
24             exception EO::Error::InvalidParameters;
25              
26             sub new {
27             my $proto = shift;
28             my $class = ref($proto) || $proto;
29             my $self = bless $class->primitive(), $class;
30             my $text = "Couldn't create object of class '$class'";
31             throw EO::Error::New text => $text unless $self->init( @_ );
32             if (!$self->{ _eo_init_success }) {
33             require Carp;
34             my $class = ref($self);
35             Carp::cluck("init not passed up the call chain");
36             }
37             return $self;
38             }
39              
40             sub init {
41             my $self = shift;
42             $self->{ _eo_init_success } = 1;
43             }
44              
45             sub generate_oid {
46             Data::UUID->new()->create_str();
47             }
48              
49             sub primitive : Private {
50             return { _eo_init_success => 0 };
51             }
52              
53             sub set_oid : Private {
54             my $self = shift;
55             if (@_) {
56             $self->{ oid } = shift;
57             return $self;
58             }
59             }
60              
61             sub oid {
62             my $self = shift;
63             if (@_) {
64             throw EO::Error::InvalidParameters text => "Can't set read-only valid oid";
65             }
66             ## generate oids lazily
67             if (!$self->{ oid }) {
68             $self->set_oid( $self->generate_oid() );
69             }
70             return $self->{ oid };
71             }
72              
73             sub AUTOLOAD {
74             my $self = shift;
75             my $class = ref($self) ? ref($self) : $self;
76             my $meth = substr($AUTOLOAD, rindex($AUTOLOAD, ':') + 1);
77             my $text = "Can't locate object method \"$meth\" via package '$class'";
78             local($Error::Depth) = $Error::Depth + 1;
79             throw EO::Error::Method::NotFound text => $text;
80             }
81              
82             sub clone {
83             my $self = shift;
84             my $clone = Storable::dclone( $self );
85             my $objs = get_blessed( $clone );
86             ## ensure we regenerate the oids.
87             foreach my $object (@$objs) {
88             if ($object->isa('EO')) {
89             $object->set_oid( $object->generate_oid() );
90             }
91             }
92             $clone;
93             }
94              
95             sub as_string {
96             my $self = shift;
97             my $class = ref($self);
98             return "I am an '$class' object";
99             }
100              
101             sub DESTROY { 1; }
102              
103             1;
104              
105             __END__