File Coverage

blib/lib/Class/MakeMethods/Template/Inheritable.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 6 66.6
pod 2 3 66.6
total 16 21 76.1


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Class::MakeMethods::Template::Inheritable - Overridable data
4              
5             =head1 SYNOPSIS
6              
7             package MyClass;
8              
9             use Class::MakeMethods( 'Template::Inheritable:scalar' => 'foo' );
10             # We now have an accessor method for an "inheritable" scalar value
11            
12             MyClass->foo( 'Foozle' ); # Set a class-wide value
13             print MyClass->foo(); # Retrieve class-wide value
14            
15             my $obj = MyClass->new(...);
16             print $obj->foo(); # All instances "inherit" that value...
17            
18             $obj->foo( 'Foible' ); # until you set a value for an instance.
19             print $obj->foo(); # This now finds object-specific value.
20             ...
21            
22             package MySubClass;
23             @ISA = 'MyClass';
24            
25             print MySubClass->foo(); # Intially same as superclass,
26             MySubClass->foo('Foobar'); # but overridable per subclass,
27             print $subclass_obj->foo(); # and shared by its instances
28             $subclass_obj->foo('Fosil');# until you override them...
29             ...
30              
31             =head1 DESCRIPTION
32              
33             The MakeMethods subclass provides accessor methods that search an inheritance tree to find a value. This allows you to set a shared or default value for a given class, and optionally override it in a subclass.
34              
35             =cut
36              
37             ########################################################################
38              
39             package Class::MakeMethods::Template::Inheritable;
40              
41 1     1   10184 use Class::MakeMethods::Template::Generic '-isasubclass';
  1         4  
  1         18  
42              
43             $VERSION = 1.008;
44 1     1   10 use strict;
  1         2  
  1         44  
45             require 5.0;
46 1     1   7 use Carp;
  1         3  
  1         371  
47              
48             sub generic {
49             {
50 1     1 0 12 '-import' => {
51             'Template::Generic:generic' => '*'
52             },
53             'modifier' => {
54             '-all' => [ q{
55             _INIT_VALUE_CLASS_
56             *
57             } ],
58             },
59             'code_expr' => {
60             '_VALUE_CLASS_' => '$_value_class',
61             '_INIT_VALUE_CLASS_' => q{
62             my _VALUE_CLASS_;
63             my @_INC_search = ( _SELF_, _SELF_CLASS_ );
64             while ( scalar @_INC_search ) {
65             _VALUE_CLASS_ = shift @_INC_search;
66             last if ( exists _ATTR_{data}->{_VALUE_CLASS_} );
67             no strict 'refs';
68             unshift @_INC_search, @{"_VALUE_CLASS_\::ISA"} if ! ref _VALUE_CLASS_;
69             }
70             },
71             '_VALUE_' => '_ATTR_{data}->{_VALUE_CLASS_}',
72             '_GET_VALUE_' => q{ _ATTR_{data}->{_VALUE_CLASS_} },
73             '_SET_VALUE_{}' => q{ do { my $data = *; defined($data) ? ( _VALUE_CLASS_ = _SELF_ and _ATTR_{data}->{_SELF_} = $data ) : ( delete _ATTR_{data}->{_SELF_} and undef ) } },
74             },
75             }
76             }
77              
78             ########################################################################
79              
80             =head2 Standard Methods
81              
82             The following methods from Generic should be supported:
83              
84             scalar
85             string
86             string_index (?)
87             number
88             boolean (?)
89             bits (?)
90             array (?)
91             hash (?)
92             tiedhash (?)
93             hash_of_arrays (?)
94             object (?)
95             instance (?)
96             array_of_objects (?)
97             code (?)
98             code_or_scalar (?)
99              
100             See L for the interfaces and behaviors of these method types.
101              
102             The items marked with a * above are specifically defined in this package, whereas the others are formed automatically by the interaction of this package's generic settings with the code templates provided by the Generic superclass.
103              
104             The items marked with a ? above have not been tested sufficiently; please inform the author if they do not function as you would expect.
105              
106             =cut
107              
108             sub array {
109             {
110 0     0 1   '-import' => {
111             'Template::Generic:array' => '*',
112             },
113             'modifier' => {
114             '-all' => q{ _INIT_VALUE_CLASS_; _ENSURE_REF_VALUE_; * },
115             },
116             'code_expr' => {
117             '_ENSURE_REF_VALUE_' => q{ _VALUE_ ||= []; },
118             '_REF_VALUE_' => '(\@{_ATTR_{data}->{_VALUE_CLASS_}})',
119             },
120             }
121             }
122              
123             sub hash {
124             {
125 0     0 1   '-import' => {
126             'Template::Generic:hash' => '*',
127             },
128             'modifier' => {
129             '-all' => q{ _INIT_VALUE_CLASS_; _ENSURE_REF_VALUE_; * },
130             },
131             'code_expr' => {
132             '_ENSURE_REF_VALUE_' => q{ _VALUE_ ||= {}; },
133             '_REF_VALUE_' => '(\%{_ATTR_{data}->{_VALUE_CLASS_}})',
134             },
135             }
136             }
137              
138             ########################################################################
139              
140             =head1 SEE ALSO
141              
142             See L for general information about this distribution.
143              
144             See L for more about this family of subclasses.
145              
146             See L for information about the various accessor interfaces subclassed herein.
147              
148             If you just need scalar accessors, see L for a very elegant and efficient implementation.
149              
150             =cut
151              
152             ########################################################################
153              
154             1;