blib/lib/QBit/Class.pm untested

filter: Statementstmt 0.0% 0 / 42 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 79 CC 2 SCAR 9.6
CC
2
Cov
0%
_rw_accessor
6.0
new
2.0
init
2.0
CRAP 2.6
1 =head1 Name
2  
3 QBit::Class - base class for QBit framework.
4  
5 =head1 Description
6  
7 All classes must inherit L<QBit::Class>.
8  
9 =cut
10  
11 package QBit::Class;
12 $QBit::Class::VERSION = '0.3';
13 use qbit;
14  
15 =head1 Methods
16  
17 =head2 new
18  
19 B<Arguments:>
20  
21 =over
22  
23 =item
24  
25 B<%fields> - fields to store in object.
26  
27 =back
28  
29 B<Return value:> blessed object.
30  
31 =cut
32  
33 sub new {
34 my ($class, %fields) = @_;
35  
36 $class = ref($class) || $class;
37  
38 my $self = \%fields;
39 bless($self, $class);
40  
41 $self->init();
42  
43 return $self;
44 }
45  
46 =head2 init
47  
48 B<No arguments.>
49  
50 Method called from L</new> before return object.
51  
52 =cut
53  
54 sub init { }
55  
56 =head2 mk_accessors
57  
58 B<Arguments:>
59  
60 =over
61  
62 =item
63  
64 B<@fields> - array of strings, names of accessors.
65  
66 =back
67  
68 It generate read/write accessors.
69  
70 __PACKAGE__->mk_accessors(qw(fieldname fieldname2));
71 ....
72 $self->fieldname(); # return value
73 $self->fieldname('newval'); # set value
74  
75 =cut
76  
77 sub mk_accessors {
78 my ($self, @fields) = @_;
79  
80 my $class = ref($self) || $self;
81  
82 no strict 'refs';
83 foreach my $field (@fields) {
84 *{"${class}::$field"} = $self->_rw_accessor($field);
85 }
86 }
87  
88 =head2 mk_ro_accessors
89  
90 B<Arguments:>
91  
92 =over
93  
94 =item
95  
96 B<@fields> - array of strings, names of accessors.
97  
98 =back
99  
100 It generate only read accessors.
101  
102 __PACKAGE__->mk_accessors(qw(fieldname fieldname2));
103 ....
104 $self->fieldname(); # return value
105  
106 =cut
107  
108 sub mk_ro_accessors {
109 my ($self, @fields) = @_;
110  
111 my $class = ref($self) || $self;
112  
113 no strict 'refs';
114 foreach my $field (@fields) {
115 *{"${class}::$field"} = $self->_ro_accessor($field);
116 }
117 }
118  
119 =head2 abstract_methods
120  
121 B<Arguments:>
122  
123 =over
124  
125 =item
126  
127 B<@metods> - array of strings, names of abstract methods.
128  
129 =back
130  
131 __PACKAGE__->abstract_methods(qw(method1 method2));
132 ....
133 $self->method1(); # trow exception with text "Abstract method: method1" if descendant has not override it.
134  
135 =cut
136  
137 sub abstract_methods {
138 my ($package, @metods) = @_;
139  
140 {
141 no strict 'refs';
142 *{"${package}::$_"} = eval('sub {$package->__abstract__(\'' . $_ . '\')}') foreach @metods;
143 }
144 }
145  
146 sub _rw_accessor {
147 my ($class, $field) = @_;
148  
149 return sub {@_ > 1 ? $_[0]->{$field} = $_[1] : $_[0]->{$field}};
150 }
151  
152 sub _ro_accessor {
153 my ($self, $field) = @_;
154  
155 return sub {$_[0]->{$field}};
156 }
157  
158 sub __abstract__ {
159 my ($class, $method) = @_;
160  
161 throw gettext("Abstract method: %s", $method);
162 }
163  
164 TRUE;