StructTemplate.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 122 CC 12 SCAR 24.3
CC
12
Cov
0%
set_attributes
20.0
attributes
12.0
_define_constructor
12.0
CRAP 11.4
1 #
2 # Class::StructTemplate - Replacement class for Class::Struct
3 # $Id$
4 #
5 # Copyright (C) 2000 by Heiko Wundram.
6 # All rights reserved.
7 #
8 # This program is free software; you can redistribute and/or modify it under the same terms as Perl itself.
9 #
10 # $Log$
11 #
12  
13 package Class::StructTemplate;
14 $Class::StructTemplate::VERSION = '0.01';
15  
16 require Exporter;
17 @Class::StructTemplate::ISA = qw(Exporter);
18 @Class::StructTemplate::EXPORT = qw(attributes);
19  
20 use Carp;
21  
22 sub attributes
23 {
24 my $pkg = ref $_[0] ? ${ shift() } : caller();
25  
26 if( @_ < 1 )
27 {
28 confess "Need at least one attribute to assign to class!";
29 }
30  
31 my $attrib;
32  
33 ${"${pkg}::_ATTRIBUTES"} = [@_];
34 ${"${pkg}::_max_id"} = 1;
35  
36 _define_constructor($pkg) or confess("Couldn't create constructor for $pkg!");
37  
38 foreach $attrib (@${"${pkg}::_ATTRIBUTES"},"_id")
39 {
40 _define_accessor($pkg,$attrib) or confess("Couldn't create accessor for $pkg!");
41 }
42  
43 return 1;
44 }
45  
46 sub _define_constructor
47 {
48 if( @_ != 1 )
49 {
50 confess "_define_constructor is only called with one argument!";
51 }
52  
53 my ($pkg) = @_;
54  
55 my $accs = qq|
56 package $pkg;
57  
58 sub new
59 {
60 my (\$class,\%attribs) = \@_;
61 \$class = ref \$class ? ref \$class : \$class;
62 bless( my \$self = {}, \$class );
63  
64 \$self->set_attributes(\%attribs,"_id"=>\$self->_next_id());
65  
66 \$self->{"_created"} = 0;
67 \$self->{"_changed"} = 1;
68  
69 return \$self;
70 }|;
71  
72 eval $accs;
73  
74 croak $@ if $@;
75 return !$@;
76 }
77  
78 sub _define_accessor
79 {
80 if( @_ != 2 )
81 {
82 confess "_define_accessor is only called with two arguments!";
83 }
84  
85 my ($pkg,$attrib) = @_;
86  
87 my $accs = qq|
88 package $pkg;
89  
90 sub $attrib
91 {
92 my (\$class) = (shift);
93 ref \$class or confess("Can only set attribute $attrib on an instance of class $pkg!");
94  
95 if( \@_ == 0 )
96 {
97 return \$class->{"$attrib"};
98 }
99 elsif( \@_ == 1 )
100 {
101 \$class->{"$attrib"} = \$_[0];
102 \$class->{"_changed"} = 1;
103 return \$class->{"$attrib"};
104 }
105 else
106 {
107 confess("Can only retrieve or set class-data!");
108 return undef;
109 }
110 }|;
111  
112 eval $accs;
113  
114 croak $@ if $@;
115 return !$@;
116 }
117  
118 sub _next_id
119 {
120 if( @_ != 1 )
121 {
122 confess "_next_id only called with one argument!";
123 }
124  
125 my ($class) = @_;
126 ref $class or confess "Can only get the next id on an instance of class $class!";
127 my $pkg = ref $class;
128  
129 return ${"${pkg}::_max_id"}++;
130 }
131  
132 sub set_attributes
133 {
134 if( ( @_ - 1 ) % 2 != 0 )
135 {
136 confess "set_attributes can only be called with an even number of arguments!";
137 }
138  
139 my ($class,%attribs) = @_;
140 ref $class or confess "set_attributes can only be called on an instance of class $class!";
141 my $pkg = ref $class;
142 my $attrib;
143  
144 foreach $attrib (@${"${pkg}::_ATTRIBUTES"},"_id")
145 {
146 if( exists $attribs{$attrib} )
147 {
148 $class->$attrib($attribs{$attrib});
149 }
150 elsif( !$class->{"_allset"} )
151 {
152 $class->$attrib(undef);
153 }
154 }
155  
156 $class->{"_allset"} = 1;
157 $class->{"_changed"} = 1;
158  
159 return $class;
160 }
161  
162 sub get_attributes
163 {
164 if( @_ != 1 )
165 {
166 confess "get_attributes is never called with any arguments!";
167 }
168  
169 my ($class) = @_;
170 ref $class or confess "get_attributes can only be called on an instance of class $class!";
171 my $pkg = ref $class;
172 my %ret_val = ();
173 my $attrib;
174  
175 foreach $attrib (@${"${pkg}::_ATTRIBUTES"},"_id")
176 {
177 $ret_val{$attrib} = $class->$attrib();
178 }
179  
180 return %ret_val;
181 }
182  
183 1;