File Coverage

blib/lib/Class/Data/Inheritable/Translucent.pm
Criterion Covered Total %
statement 44 44 100.0
branch 18 20 90.0
condition 9 12 75.0
subroutine 8 8 100.0
pod 2 2 100.0
total 81 86 94.1


line stmt bran cond sub pod time code
1             package Class::Data::Inheritable::Translucent;
2              
3 3     3   66378 use 5.008001;
  3         12  
  3         108  
4              
5 3     3   16 use strict;
  3         5  
  3         105  
6 3     3   15 use warnings;
  3         9  
  3         1079  
7              
8             =head1 NAME
9              
10             Class::Data::Inheritable::Translucent - Inheritable, overridable, translucent class data / object attributes
11              
12             =cut
13              
14             our $VERSION = '1.04';
15              
16             if (eval { require Sub::Name }) {
17             Sub::Name->import;
18             }
19              
20             =head1 SYNOPSIS
21              
22             package Foo;
23             use base 'Class::Data::Inheritable::Translucent';
24              
25             Foo->mk_translucent("bar");
26             Foo->bar("baz");
27              
28             $obj = Foo->new;
29              
30             print $obj->bar; # prints "baz"
31              
32             $obj->bar("whatever");
33              
34             print $obj->bar; # prints "whatever"
35             print Foo->bar; # prints "baz"
36              
37             $obj->bar(undef);
38              
39             print $obj->bar; # prints "baz"
40              
41             =head1 DESCRIPTION
42              
43             This module is based on Class::Data::Inheritable, and is largely the same,
44             except the class data accessors double as translucent object attributes.
45              
46             Object data, by default, is stored in $obj->{$attribute}. See the attrs()
47             method, explained below, on how to change that.
48              
49             =head1 METHODS
50              
51             =over
52              
53             =item B
54              
55             Creates inheritable class data / translucent instance attributes
56              
57             =cut
58              
59             sub mk_translucent {
60 6     6 1 62 my ($declaredclass, $attribute, $data) = @_;
61              
62             my $accessor = sub {
63 28 100   28   106 my $obj = ref($_[0]) ? $_[0] : undef;
64 28   66     77 my $wantclass = ref($_[0]) || $_[0];
65              
66 28 100 100     159 return $wantclass->mk_translucent($attribute)->(@_)
      100        
67             if @_>1 && !$obj && $wantclass ne $declaredclass;
68              
69 27 100       65 if ($obj) {
70 12         32 my $attrs = $obj->attrs;
71 12 100       56 $attrs->{$attribute} = $_[1] if @_ > 1;
72 12 100       44 return $attrs->{$attribute} if defined $attrs->{$attribute};
73             }
74             else {
75 15 100       40 $data = $_[1] if @_>1;
76             }
77 21         86 return $data;
78 6         35 };
79              
80 6         19 my $name = "${declaredclass}::$attribute";
81 6         10 my $subnamed = 0;
82 6 100       8 unless (defined &{$name}) {
  6         36  
83 5 50       22 subname($name, $accessor) if defined &subname;
84 5         9 $subnamed = 1;
85             {
86 3     3   17 no strict 'refs';
  3         6  
  3         290  
  5         8  
87 5         8 *{$name} = $accessor;
  5         34  
88             }
89             }
90 6         17 my $alias = "${declaredclass}::_${attribute}_accessor";
91 6 100       7 unless (defined &{$alias}) {
  6         51  
92 5 50 33     20 subname($alias, $accessor) if defined &subname and not $subnamed;
93             {
94 3     3   14 no strict 'refs';
  3         7  
  3         309  
  5         7  
95 5         8 *{$alias} = $accessor;
  5         37  
96             }
97             }
98             }
99              
100             =pod
101              
102             =item B
103              
104             This method is called by the generated accessors and, by default, simply
105             returns the object that called it, which should be a hash reference for storing
106             object attributes. If your objects are not hashrefs, or you wish to store your
107             object attributes in a different location, eg. $obj->{attrs}, you should
108             override this method. Class::Data::Inheritable::Translucent stores object
109             attributes in $obj->attrs()->{$attribute}.
110              
111             =cut
112              
113             sub attrs {
114 10     10 1 12 my $obj = shift;
115 10         15 return $obj;
116             }
117              
118             =pod
119              
120             =back
121              
122             =head1 AUTHOR
123              
124             Steve Hay > is now maintaining
125             Class::Data::Inheritable::Translucent as of version 1.00
126              
127             Originally by Ryan McGuigan
128              
129             Based on Class::Data::Inheritable, originally by Damian Conway
130              
131             =head1 ACKNOWLEDGEMENTS
132              
133             Thanks to Damian Conway for L
134              
135             =head1 COPYRIGHT & LICENSE
136              
137             Version 0.01 Copyright 2005 Ryan McGuigan, all rights reserved.
138             Changes in Version 1.00 onwards Copyright (C) 2009, 2011 Steve Hay
139              
140             mk_translucent is based on mk_classdata from Class::Data::Inheritable,
141             Copyright Damian Conway and Michael G Schwern, licensed under the terms of the
142             Perl Artistic License.
143              
144             This program is free software; It may be used, redistributed and/or modified
145             under the terms of the Perl Artistic License (see
146             L)
147              
148             =head1 BUGS
149              
150             Please report any bugs or feature requests on the CPAN Request Tracker at
151             F.
152              
153             =head1 SEE ALSO
154              
155             =over 2
156              
157             =item *
158              
159             L
160              
161             =item *
162              
163             L - Tom's OO Tutorial for Class Data in Perl - a pretty nice Class
164             Data tutorial for Perl
165              
166             =item *
167              
168             The source. It's quite short, and simple enough.
169              
170             =back
171              
172             =cut
173              
174             1; # End of Class::Data::Inheritable::Translucent