line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::Meta::Type; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Class::Meta::Type - Data type validation and accessor building. |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package MyApp::TypeDef; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use strict; |
12
|
|
|
|
|
|
|
use Class::Meta::Type; |
13
|
|
|
|
|
|
|
use IO::Socket; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $type = Class::Meta::Type->add( |
16
|
|
|
|
|
|
|
key => 'io_socket', |
17
|
|
|
|
|
|
|
desc => 'IO::Socket object', |
18
|
|
|
|
|
|
|
name => 'IO::Socket Object' |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 DESCRIPTION |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
This class stores the various data types us |
24
|
|
|
|
|
|
|
ed by C. It manages |
25
|
|
|
|
|
|
|
all aspects of data type validation and method creation. New data types can be |
26
|
|
|
|
|
|
|
added to Class::Meta::Type by means of the C constructor. This is |
27
|
|
|
|
|
|
|
useful for creating custom types for your Class::Meta-built classes. |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
BThis class manages the most advanced features of C. |
30
|
|
|
|
|
|
|
Before deciding to create your own accessor closures as described in L, |
31
|
|
|
|
|
|
|
you should have a thorough working knowledge of how Class::Meta works, and |
32
|
|
|
|
|
|
|
have studied the L method carefully. Simple data type definitions such |
33
|
|
|
|
|
|
|
as that shown in the L, on the other hand, are encouraged. |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=cut |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
############################################################################## |
38
|
|
|
|
|
|
|
# Dependencies # |
39
|
|
|
|
|
|
|
############################################################################## |
40
|
21
|
|
|
21
|
|
182
|
use strict; |
|
21
|
|
|
|
|
39
|
|
|
21
|
|
|
|
|
48178
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
############################################################################## |
43
|
|
|
|
|
|
|
# Package Globals # |
44
|
|
|
|
|
|
|
############################################################################## |
45
|
|
|
|
|
|
|
our $VERSION = '0.66'; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
############################################################################## |
48
|
|
|
|
|
|
|
# Private Package Globals # |
49
|
|
|
|
|
|
|
############################################################################## |
50
|
|
|
|
|
|
|
my %def_builders = ( |
51
|
|
|
|
|
|
|
'default' => 'Class::Meta::AccessorBuilder', |
52
|
|
|
|
|
|
|
'affordance' => 'Class::Meta::AccessorBuilder::Affordance', |
53
|
|
|
|
|
|
|
'semi-affordance' => 'Class::Meta::AccessorBuilder::SemiAffordance', |
54
|
|
|
|
|
|
|
); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# This code ref builds object/reference value checkers. |
57
|
|
|
|
|
|
|
my $class_validation_generator = sub { |
58
|
|
|
|
|
|
|
my ($pkg, $type) = @_; |
59
|
|
|
|
|
|
|
return [ |
60
|
|
|
|
|
|
|
sub { |
61
|
|
|
|
|
|
|
return unless defined $_[0]; |
62
|
|
|
|
|
|
|
UNIVERSAL::isa($_[0], $pkg) |
63
|
|
|
|
|
|
|
or $_[2]->class->handle_error( |
64
|
|
|
|
|
|
|
"Value '$_[0]' is not a valid $type" |
65
|
|
|
|
|
|
|
); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
]; |
68
|
|
|
|
|
|
|
}; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
############################################################################## |
71
|
|
|
|
|
|
|
# Data type definition storage. |
72
|
|
|
|
|
|
|
############################################################################## |
73
|
|
|
|
|
|
|
{ |
74
|
|
|
|
|
|
|
my %types = (); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
############################################################################## |
77
|
|
|
|
|
|
|
# Constructors # |
78
|
|
|
|
|
|
|
############################################################################## |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 CONSTRUCTORS |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head2 new |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
my $type = Class::Meta::Type->new($key); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Returns the data type definition for an existing data type. The definition |
87
|
|
|
|
|
|
|
will be looked up by the C<$key> argument. Use C to specify new types. |
88
|
|
|
|
|
|
|
If no data type exists for a given key, but C<< Class::Meta->for_key >> |
89
|
|
|
|
|
|
|
returns a Class::Meta::Class object for that key, then C will |
90
|
|
|
|
|
|
|
implicitly call C to create add a new type corresponding to that |
91
|
|
|
|
|
|
|
class. This makes it easy to use any Class::Meta class as a data type. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Other data types can be added by means of the C constructor, or by |
94
|
|
|
|
|
|
|
simply C |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=over 4 |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=item L |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=over 4 |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=item scalar |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=item scalarref |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=item array |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=item hash |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=item code |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=back |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=item L |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=over 4 |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=item string |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=back |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=item L |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=over 4 |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=item boolean |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=back |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=item L |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=over 4 |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=item whole |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=item integer |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=item decimal |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=item real |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=item float |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=back |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=back |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Read the documentation for the individual modules for details on their data |
149
|
|
|
|
|
|
|
types. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=cut |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub new { |
154
|
445
|
|
|
445
|
1
|
5031
|
my $class = shift; |
155
|
445
|
100
|
|
|
|
1055
|
Class::Meta->handle_error('Type argument required') unless $_[0]; |
156
|
444
|
|
|
|
|
839
|
my $key = lc shift; |
157
|
444
|
100
|
|
|
|
1399
|
unless (exists $types{$key}) { |
158
|
|
|
|
|
|
|
# See if there's a Class::Meta class defined for this key. |
159
|
52
|
100
|
|
|
|
336
|
my $cmc = Class::Meta->for_key($key) |
160
|
|
|
|
|
|
|
or Class::Meta->handle_error("Type '$key' does not exist"); |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
# Create a new type for this class. |
163
|
2
|
|
|
|
|
9
|
return $class->add( |
164
|
|
|
|
|
|
|
key => $key, |
165
|
|
|
|
|
|
|
name => $cmc->package, |
166
|
|
|
|
|
|
|
check => $cmc->package |
167
|
|
|
|
|
|
|
); |
168
|
|
|
|
|
|
|
} |
169
|
392
|
|
|
|
|
3088
|
return bless $types{$key}, $class; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
############################################################################## |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head2 add |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
my $type = Class::Meta::Type->add( |
177
|
|
|
|
|
|
|
key => 'io_socket', |
178
|
|
|
|
|
|
|
name => 'IO::Socket Object', |
179
|
|
|
|
|
|
|
desc => 'IO::Socket object' |
180
|
|
|
|
|
|
|
); |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Creates a new data type definition and stores it for future use. Use this |
183
|
|
|
|
|
|
|
constructor to add new data types to meet the needs of your class. The named |
184
|
|
|
|
|
|
|
parameter arguments are: |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=over 4 |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=item key |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
Required. The key with which the data type can be looked up in the future via |
191
|
|
|
|
|
|
|
a call to C. Note that the key will be used case-insensitively, so |
192
|
|
|
|
|
|
|
"foo", "Foo", and "FOO" are equivalent, and the key must be unique. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=item name |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Required. The name of the data type. This should be formatted for display |
197
|
|
|
|
|
|
|
purposes, and indeed, Class::Meta will often use it in its own exceptions. |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=item check |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
Optional. Specifies how to validate the value of an attribute of this type. |
202
|
|
|
|
|
|
|
The check parameter can be specified in any of the following ways: |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=over 4 |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=item * |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
As a code reference. When Class::Meta executes this code reference, it will |
209
|
|
|
|
|
|
|
pass in the value to check, the object for which the attribute will be set, |
210
|
|
|
|
|
|
|
and the Class::Meta::Attribute object describing the attribute. If the |
211
|
|
|
|
|
|
|
attribute is a class attribute, then the second argument will not be an |
212
|
|
|
|
|
|
|
object, but a hash reference with two keys: |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=over 8 |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=item $name |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
The existing value for the attribute is stored under the attribute name. |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=item __pkg |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
The name of the package to which the attribute is being assigned. |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=back |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
If the new value is not the proper value for your custom data type, the code |
227
|
|
|
|
|
|
|
reference should throw an exception. Here's an example; it's the code |
228
|
|
|
|
|
|
|
reference used by "string" data type, which you can add to Class::Meta::Type |
229
|
|
|
|
|
|
|
simply by using Class::Meta::Types::String: |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
check => sub { |
232
|
|
|
|
|
|
|
my $value = shift; |
233
|
|
|
|
|
|
|
return unless defined $value && ref $value; |
234
|
|
|
|
|
|
|
require Carp; |
235
|
|
|
|
|
|
|
our @CARP_NOT = qw(Class::Meta::Attribute); |
236
|
|
|
|
|
|
|
Carp::croak("Value '$value' is not a valid string"); |
237
|
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
Here's another example. This code reference might be used to make sure that a |
240
|
|
|
|
|
|
|
new value is always greater than the existing value. |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
check => sub { |
243
|
|
|
|
|
|
|
my ($new_val, $obj, $attr) = @_; |
244
|
|
|
|
|
|
|
# Just return if the new value is greater than the old value. |
245
|
|
|
|
|
|
|
return if defined $new_val && $new_val > $_[1]->{$_[2]->get_name}; |
246
|
|
|
|
|
|
|
require Carp; |
247
|
|
|
|
|
|
|
our @CARP_NOT = qw(Class::Meta::Attribute); |
248
|
|
|
|
|
|
|
Carp::croak("Value '$new_val' is not greater than '$old_val'"); |
249
|
|
|
|
|
|
|
} |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=item * |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
As an array reference. All items in this array reference must be code |
254
|
|
|
|
|
|
|
references that perform checks on a value, as specified above. |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=item * |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
As a string. In this case, Class::Meta::Type assumes that your data type |
259
|
|
|
|
|
|
|
identifies a particular object type. Thus it will use the string to construct |
260
|
|
|
|
|
|
|
a validation code reference for you. For example, if you wanted to create a |
261
|
|
|
|
|
|
|
data type for IO::Socket objects, pass the string 'IO::Socket' to the check |
262
|
|
|
|
|
|
|
parameter and Class::Meta::Type will use the code reference returned by |
263
|
|
|
|
|
|
|
C to generate the validation checks. If you'd |
264
|
|
|
|
|
|
|
like to specify an alternative class validation code generator, pass one to |
265
|
|
|
|
|
|
|
the C class method. Or pass in a code reference |
266
|
|
|
|
|
|
|
or array reference of code reference as just described to use your own |
267
|
|
|
|
|
|
|
validator once. |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=back |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
Note that if the C parameter is not specified, there will never be any |
272
|
|
|
|
|
|
|
validation of your custom data type. And yes, there may be times when you want |
273
|
|
|
|
|
|
|
this -- The default "scalar" and "boolean" data types, for example, have no |
274
|
|
|
|
|
|
|
checks. |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=item builder |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
Optional. This parameter specifies the accessor builder for attributes of this |
279
|
|
|
|
|
|
|
type. The C parameter can be any of the following values: |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=over 4 |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
=item "default" |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
The string 'default' uses Class::Meta::Type's default accessor building code, |
286
|
|
|
|
|
|
|
provided by Class::Meta::AccessorBuilder. This is the default value, of |
287
|
|
|
|
|
|
|
course. |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
=item "affordance" |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
The string 'default' uses Class::Meta::Type's affordance accessor building |
292
|
|
|
|
|
|
|
code, provided by Class::Meta::AccessorBuilder::Affordance. Affordance |
293
|
|
|
|
|
|
|
accessors provide two accessors for an attribute, a C accessor and a |
294
|
|
|
|
|
|
|
C mutator. See |
295
|
|
|
|
|
|
|
L |
296
|
|
|
|
|
|
|
for more information. |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
=item "semi-affordance" |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
The string 'default' uses Class::Meta::Type's semi-affordance accessor |
301
|
|
|
|
|
|
|
building code, provided by Class::Meta::AccessorBuilder::SemiAffordance. |
302
|
|
|
|
|
|
|
Semi-affordance accessors differ from affordance accessors in that they do not |
303
|
|
|
|
|
|
|
prepend C to the accessor. So for an attribute "foo", the accessor would |
304
|
|
|
|
|
|
|
be named C and the mutator named C. See |
305
|
|
|
|
|
|
|
L |
306
|
|
|
|
|
|
|
for more information. |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
=item A Package Name |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
Pass in the name of a package that contains the functions C, |
311
|
|
|
|
|
|
|
C, and C. These functions will be used to |
312
|
|
|
|
|
|
|
create the necessary accessors for an attribute. See L
|
313
|
|
|
|
|
|
|
Building|"Custom Accessor Building"> for details on creating your own accessor |
314
|
|
|
|
|
|
|
builders. |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
=back |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
=back |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
=cut |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
sub add { |
323
|
149
|
|
|
149
|
1
|
46652
|
my $pkg = shift; |
324
|
|
|
|
|
|
|
# Make sure we can process the parameters. |
325
|
149
|
50
|
|
|
|
703
|
Class::Meta->handle_error( |
326
|
|
|
|
|
|
|
'Odd number of parameters in call to new() when named ' |
327
|
|
|
|
|
|
|
. 'parameters were expected' |
328
|
|
|
|
|
|
|
) if @_ % 2; |
329
|
|
|
|
|
|
|
|
330
|
149
|
|
|
|
|
836
|
my %params = @_; |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
# Check required paremeters. |
333
|
149
|
|
|
|
|
335
|
foreach (qw(key name)) { |
334
|
296
|
100
|
|
|
|
1137
|
Class::Meta->handle_error("Parameter '$_' is required") |
335
|
|
|
|
|
|
|
unless $params{$_}; |
336
|
|
|
|
|
|
|
} |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
# Check the key parameter. |
339
|
145
|
|
|
|
|
658
|
$params{key} = lc $params{key}; |
340
|
145
|
100
|
|
|
|
450
|
Class::Meta->handle_error("Type '$params{key}' already defined") |
341
|
|
|
|
|
|
|
if exists $types{$params{key}}; |
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
# Set up the check croak. |
344
|
|
|
|
|
|
|
my $chk_die = sub { |
345
|
3
|
|
|
3
|
|
24
|
Class::Meta->handle_error( |
346
|
|
|
|
|
|
|
"Paremter 'check' in call to add() must be a code reference, " |
347
|
|
|
|
|
|
|
. "an array of code references, or a scalar naming an object " |
348
|
|
|
|
|
|
|
. "type" |
349
|
|
|
|
|
|
|
); |
350
|
143
|
|
|
|
|
548
|
}; |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
# Check the check parameter. |
353
|
143
|
100
|
|
|
|
548
|
if ($params{check}) { |
354
|
122
|
|
|
|
|
438
|
my $ref = ref $params{check}; |
355
|
122
|
100
|
|
|
|
468
|
if (not $ref) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
356
|
|
|
|
|
|
|
# It names the object to be checked. So generate a validator. |
357
|
40
|
|
|
|
|
134
|
$params{check} = |
358
|
|
|
|
|
|
|
$class_validation_generator->(@params{qw(check name)}); |
359
|
40
|
50
|
|
|
|
176
|
$params{check} = [$params{check}] |
360
|
|
|
|
|
|
|
if ref $params{check} eq 'CODE'; |
361
|
|
|
|
|
|
|
} elsif ($ref eq 'CODE') { |
362
|
18
|
|
|
|
|
68
|
$params{check} = [$params{check}] |
363
|
|
|
|
|
|
|
} elsif ($ref eq 'ARRAY') { |
364
|
|
|
|
|
|
|
# Make sure that they're all code references. |
365
|
62
|
|
|
|
|
79
|
foreach my $chk (@{$params{check}}) { |
|
62
|
|
|
|
|
151
|
|
366
|
63
|
100
|
|
|
|
282
|
$chk_die->() unless ref $chk eq 'CODE'; |
367
|
|
|
|
|
|
|
} |
368
|
|
|
|
|
|
|
} else { |
369
|
|
|
|
|
|
|
# It's bogus. |
370
|
2
|
|
|
|
|
7
|
$chk_die->(); |
371
|
|
|
|
|
|
|
} |
372
|
|
|
|
|
|
|
} |
373
|
|
|
|
|
|
|
|
374
|
|
|
|
|
|
|
# Check the builder parameter. |
375
|
140
|
|
66
|
|
|
685
|
$params{builder} ||= $pkg->default_builder; |
376
|
|
|
|
|
|
|
|
377
|
140
|
|
66
|
|
|
666
|
my $builder = $def_builders{$params{builder}} || $params{builder}; |
378
|
|
|
|
|
|
|
# Make sure it's loaded. |
379
|
140
|
50
|
|
|
|
8876
|
eval "require $builder" or die $@; |
380
|
|
|
|
|
|
|
|
381
|
140
|
|
66
|
|
|
1185
|
$params{builder} = UNIVERSAL::can($builder, 'build') |
382
|
|
|
|
|
|
|
|| Class::Meta->handle_error("No such function " |
383
|
|
|
|
|
|
|
. "'${builder}::build()'"); |
384
|
|
|
|
|
|
|
|
385
|
139
|
|
66
|
|
|
1058
|
$params{attr_get} = UNIVERSAL::can($builder, 'build_attr_get') |
386
|
|
|
|
|
|
|
|| Class::Meta->handle_error("No such function " |
387
|
|
|
|
|
|
|
. "'${builder}::build_attr_get()'"); |
388
|
|
|
|
|
|
|
|
389
|
138
|
|
66
|
|
|
1055
|
$params{attr_set} = UNIVERSAL::can($builder, 'build_attr_set') |
390
|
|
|
|
|
|
|
|| Class::Meta->handle_error("No such function " |
391
|
|
|
|
|
|
|
. "'${builder}::build_attr_set()'"); |
392
|
|
|
|
|
|
|
|
393
|
|
|
|
|
|
|
# Okay, add the new type to the cache and construct it. |
394
|
137
|
|
|
|
|
400
|
$types{$params{key}} = \%params; |
395
|
|
|
|
|
|
|
|
396
|
|
|
|
|
|
|
# Grab any aliases. |
397
|
137
|
100
|
|
|
|
444
|
if (my $alias = delete $params{alias}) { |
398
|
54
|
100
|
|
|
|
197
|
if (ref $alias) { |
399
|
8
|
|
|
|
|
79
|
$types{$_} = \%params for @$alias; |
400
|
|
|
|
|
|
|
} else { |
401
|
46
|
|
|
|
|
145
|
$types{$alias} = \%params; |
402
|
|
|
|
|
|
|
} |
403
|
|
|
|
|
|
|
} |
404
|
137
|
|
|
|
|
526
|
return $pkg->new($params{key}); |
405
|
|
|
|
|
|
|
} |
406
|
|
|
|
|
|
|
} |
407
|
|
|
|
|
|
|
|
408
|
|
|
|
|
|
|
############################################################################## |
409
|
|
|
|
|
|
|
|
410
|
|
|
|
|
|
|
=head1 CLASS METHODS |
411
|
|
|
|
|
|
|
|
412
|
|
|
|
|
|
|
=head2 default_builder |
413
|
|
|
|
|
|
|
|
414
|
|
|
|
|
|
|
my $default_builder = Class::Meta::Type->default_builder; |
415
|
|
|
|
|
|
|
Class::Meta::Type->default_builder($default_builder); |
416
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
Get or set the default builder class attribute. The value can be any one of |
418
|
|
|
|
|
|
|
the values specified for the C parameter to add(). The value set in |
419
|
|
|
|
|
|
|
this attribute will be used for the C parameter to to add() when none |
420
|
|
|
|
|
|
|
is explicitly passed. Defaults to "default". |
421
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
=cut |
423
|
|
|
|
|
|
|
|
424
|
|
|
|
|
|
|
my $default_builder = 'default'; |
425
|
|
|
|
|
|
|
sub default_builder { |
426
|
10
|
|
|
10
|
1
|
22
|
my $pkg = shift; |
427
|
10
|
100
|
|
|
|
62
|
return $default_builder unless @_; |
428
|
1
|
|
|
|
|
3
|
$default_builder = shift; |
429
|
1
|
|
|
|
|
5
|
return $pkg; |
430
|
|
|
|
|
|
|
} |
431
|
|
|
|
|
|
|
|
432
|
|
|
|
|
|
|
############################################################################## |
433
|
|
|
|
|
|
|
|
434
|
|
|
|
|
|
|
=head2 class_validation_generator |
435
|
|
|
|
|
|
|
|
436
|
|
|
|
|
|
|
my $gen = Class::Meta::Type->class_validation_generator; |
437
|
|
|
|
|
|
|
Class::Meta::Type->class_validation_generator( sub { |
438
|
|
|
|
|
|
|
my ($pkg, $name) = @_; |
439
|
|
|
|
|
|
|
return sub { |
440
|
|
|
|
|
|
|
die "'$pkg' is not a valid $name" |
441
|
|
|
|
|
|
|
unless UNIVERSAL::isa($pkg, $name); |
442
|
|
|
|
|
|
|
}; |
443
|
|
|
|
|
|
|
}); |
444
|
|
|
|
|
|
|
|
445
|
|
|
|
|
|
|
Gets or sets a code reference that will be used to generate the validation |
446
|
|
|
|
|
|
|
checks for class data types. That is to say, it will be used when a string is |
447
|
|
|
|
|
|
|
passed to the C parameter to to generate the validation |
448
|
|
|
|
|
|
|
checking code for data types that are objects. By default, it will generate a |
449
|
|
|
|
|
|
|
validation checker like this: |
450
|
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
sub { |
452
|
|
|
|
|
|
|
my $value = shift; |
453
|
|
|
|
|
|
|
return if UNIVERSAL::isa($value, 'IO::Socket') |
454
|
|
|
|
|
|
|
require Carp; |
455
|
|
|
|
|
|
|
our @CARP_NOT = qw(Class::Meta::Attribute); |
456
|
|
|
|
|
|
|
Carp::croak("Value '$value' is not a IO::Socket object"); |
457
|
|
|
|
|
|
|
}; |
458
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
But if you'd like to specify an alternate validation check generator--perhaps |
460
|
|
|
|
|
|
|
you'd like to throw exception objects rather than use Carp--just pass a code |
461
|
|
|
|
|
|
|
reference to this class method. The code reference should expect two |
462
|
|
|
|
|
|
|
arguments: the data type value to be validated, and the string passed via the |
463
|
|
|
|
|
|
|
C parameter to C. It should return a code reference or array of |
464
|
|
|
|
|
|
|
code references that validate the value. For example, you might want to do |
465
|
|
|
|
|
|
|
something like this to throw exception objects: |
466
|
|
|
|
|
|
|
|
467
|
|
|
|
|
|
|
use Exception::Class('MyException'); |
468
|
|
|
|
|
|
|
|
469
|
|
|
|
|
|
|
Class::Meta::Type->class_validation_generator( sub { |
470
|
|
|
|
|
|
|
my ($pkg, $type) = @_; |
471
|
|
|
|
|
|
|
return [ sub { |
472
|
|
|
|
|
|
|
my ($value, $object, $attr) = @_; |
473
|
|
|
|
|
|
|
MyException->throw("Value '$value' is not a valid $type") |
474
|
|
|
|
|
|
|
unless UNIVERSAL::isa($value, $pkg); |
475
|
|
|
|
|
|
|
} ]; |
476
|
|
|
|
|
|
|
}); |
477
|
|
|
|
|
|
|
|
478
|
|
|
|
|
|
|
But if the default object data type validator is good enough for you, don't |
479
|
|
|
|
|
|
|
worry about it. |
480
|
|
|
|
|
|
|
|
481
|
|
|
|
|
|
|
=cut |
482
|
|
|
|
|
|
|
|
483
|
|
|
|
|
|
|
sub class_validation_generator { |
484
|
1
|
|
|
1
|
1
|
1790
|
my $class = shift; |
485
|
1
|
50
|
|
|
|
5
|
return $class_validation_generator unless @_; |
486
|
1
|
|
|
|
|
6
|
$class_validation_generator = shift; |
487
|
|
|
|
|
|
|
} |
488
|
|
|
|
|
|
|
|
489
|
|
|
|
|
|
|
############################################################################## |
490
|
|
|
|
|
|
|
# Instance methods. |
491
|
|
|
|
|
|
|
############################################################################## |
492
|
|
|
|
|
|
|
|
493
|
|
|
|
|
|
|
=head1 INTERFACE |
494
|
|
|
|
|
|
|
|
495
|
|
|
|
|
|
|
=head2 Instance Methods |
496
|
|
|
|
|
|
|
|
497
|
|
|
|
|
|
|
=head3 key |
498
|
|
|
|
|
|
|
|
499
|
|
|
|
|
|
|
my $key = $type->key; |
500
|
|
|
|
|
|
|
|
501
|
|
|
|
|
|
|
Returns the key name for the type. |
502
|
|
|
|
|
|
|
|
503
|
|
|
|
|
|
|
=head3 name |
504
|
|
|
|
|
|
|
|
505
|
|
|
|
|
|
|
my $name = $type->name; |
506
|
|
|
|
|
|
|
|
507
|
|
|
|
|
|
|
Returns the type name. |
508
|
|
|
|
|
|
|
|
509
|
|
|
|
|
|
|
=head3 check |
510
|
|
|
|
|
|
|
|
511
|
|
|
|
|
|
|
my $checks = $type->check; |
512
|
|
|
|
|
|
|
my @checks = $type->check; |
513
|
|
|
|
|
|
|
|
514
|
|
|
|
|
|
|
Returns an array reference or list of the data type validation code references |
515
|
|
|
|
|
|
|
for the data type. |
516
|
|
|
|
|
|
|
|
517
|
|
|
|
|
|
|
=cut |
518
|
|
|
|
|
|
|
|
519
|
117
|
|
|
117
|
1
|
567
|
sub key { $_[0]->{key} } |
520
|
38
|
|
|
38
|
1
|
256
|
sub name { $_[0]->{name} } |
521
|
|
|
|
|
|
|
sub check { |
522
|
184
|
100
|
|
184
|
1
|
1004
|
return unless $_[0]->{check}; |
523
|
160
|
100
|
|
|
|
580
|
wantarray ? @{$_[0]->{check}} : $_[0]->{check} |
|
98
|
|
|
|
|
502
|
|
524
|
|
|
|
|
|
|
} |
525
|
|
|
|
|
|
|
|
526
|
|
|
|
|
|
|
############################################################################## |
527
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
=head3 build |
529
|
|
|
|
|
|
|
|
530
|
|
|
|
|
|
|
This is a protected method, designed to be called only by the |
531
|
|
|
|
|
|
|
Class::Meta::Attribute class or a subclass of Class::Meta::Attribute. It |
532
|
|
|
|
|
|
|
creates accessors for the class that the Class::Meta::Attribute object is a |
533
|
|
|
|
|
|
|
part of by calling out to the C method of the accessor builder class. |
534
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
Although you should never call this method directly, subclasses of |
536
|
|
|
|
|
|
|
Class::Meta::Type may need to override its behavior. |
537
|
|
|
|
|
|
|
|
538
|
|
|
|
|
|
|
=cut |
539
|
|
|
|
|
|
|
|
540
|
|
|
|
|
|
|
sub build { |
541
|
|
|
|
|
|
|
# Check to make sure that only Class::Meta or a subclass is building |
542
|
|
|
|
|
|
|
# attribute accessors. |
543
|
114
|
|
|
114
|
1
|
2664
|
my $caller = caller; |
544
|
114
|
100
|
|
|
|
621
|
Class::Meta->handle_error("Package '$caller' cannot call " |
545
|
|
|
|
|
|
|
. __PACKAGE__ . "->build") |
546
|
|
|
|
|
|
|
unless UNIVERSAL::isa($caller, 'Class::Meta::Attribute'); |
547
|
|
|
|
|
|
|
|
548
|
113
|
|
|
|
|
184
|
my $self = shift; |
549
|
113
|
|
|
|
|
222
|
my $code = $self->{builder}; |
550
|
113
|
|
|
|
|
342
|
$code->(@_, $self->check); |
551
|
113
|
|
|
|
|
386
|
return $self; |
552
|
|
|
|
|
|
|
} |
553
|
|
|
|
|
|
|
|
554
|
|
|
|
|
|
|
############################################################################## |
555
|
|
|
|
|
|
|
|
556
|
|
|
|
|
|
|
=head3 make_attr_set |
557
|
|
|
|
|
|
|
|
558
|
|
|
|
|
|
|
This is a protected method, designed to be called only by the |
559
|
|
|
|
|
|
|
Class::Meta::Attribute class or a subclass of Class::Meta::Attribute. It |
560
|
|
|
|
|
|
|
returns a reference to the attribute set accessor (mutator) created by the |
561
|
|
|
|
|
|
|
call to C, and usable as an indirect attribute accessor by the |
562
|
|
|
|
|
|
|
Class::Meta::Attribute C method. |
563
|
|
|
|
|
|
|
|
564
|
|
|
|
|
|
|
Although you should never call this method directly, subclasses of |
565
|
|
|
|
|
|
|
Class::Meta::Type may need to override its behavior. |
566
|
|
|
|
|
|
|
|
567
|
|
|
|
|
|
|
=cut |
568
|
|
|
|
|
|
|
|
569
|
|
|
|
|
|
|
sub make_attr_set { |
570
|
113
|
|
|
113
|
1
|
49960
|
my $self = shift; |
571
|
113
|
|
|
|
|
334
|
my $code = $self->{attr_set}; |
572
|
113
|
|
|
|
|
561
|
$code->(@_); |
573
|
|
|
|
|
|
|
} |
574
|
|
|
|
|
|
|
|
575
|
|
|
|
|
|
|
############################################################################## |
576
|
|
|
|
|
|
|
|
577
|
|
|
|
|
|
|
=head3 make_attr_get |
578
|
|
|
|
|
|
|
|
579
|
|
|
|
|
|
|
This is a protected method, designed to be called only by the |
580
|
|
|
|
|
|
|
Class::Meta::Attribute class or a subclass of Class::Meta::Attribute. It |
581
|
|
|
|
|
|
|
returns a reference to the attribute get accessor created by the call to |
582
|
|
|
|
|
|
|
C, and usable as an indirect attribute accessor by the |
583
|
|
|
|
|
|
|
Class::Meta::Attribute C method. |
584
|
|
|
|
|
|
|
|
585
|
|
|
|
|
|
|
Although you should never call this method directly, subclasses of |
586
|
|
|
|
|
|
|
Class::Meta::Type may need to override its behavior. |
587
|
|
|
|
|
|
|
|
588
|
|
|
|
|
|
|
=cut |
589
|
|
|
|
|
|
|
|
590
|
|
|
|
|
|
|
sub make_attr_get { |
591
|
117
|
|
|
117
|
1
|
231
|
my $self = shift; |
592
|
117
|
|
|
|
|
217
|
my $code = $self->{attr_get}; |
593
|
117
|
|
|
|
|
553
|
$code->(@_); |
594
|
|
|
|
|
|
|
} |
595
|
|
|
|
|
|
|
|
596
|
|
|
|
|
|
|
1; |
597
|
|
|
|
|
|
|
__END__ |