line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::Meta::Types::Numeric; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Class::Meta::Types::Numeric - Numeric data types |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package MyApp::Thingy; |
10
|
|
|
|
|
|
|
use strict; |
11
|
|
|
|
|
|
|
use Class::Meta; |
12
|
|
|
|
|
|
|
use Class::Meta::Types::Numeric; |
13
|
|
|
|
|
|
|
# OR... |
14
|
|
|
|
|
|
|
# use Class::Meta::Types::Numeric 'affordance'; |
15
|
|
|
|
|
|
|
# OR... |
16
|
|
|
|
|
|
|
# use Class::Meta::Types::Numeric 'semi-affordance'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
BEGIN { |
19
|
|
|
|
|
|
|
# Create a Class::Meta object for this class. |
20
|
|
|
|
|
|
|
my $cm = Class::Meta->new( key => 'thingy' ); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# Add an integer attribute. |
23
|
|
|
|
|
|
|
$cm->add_attribute( name => 'age', |
24
|
|
|
|
|
|
|
type => 'integer' ); |
25
|
|
|
|
|
|
|
$cm->build; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 DESCRIPTION |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
This module provides numeric data types for use with Class::Meta attributes. |
31
|
|
|
|
|
|
|
Simply load it, then pass the name of one of its types to the |
32
|
|
|
|
|
|
|
C method of a Class::Meta object to create attributes of the |
33
|
|
|
|
|
|
|
numeric data type. See L for more |
34
|
|
|
|
|
|
|
information on using and creating data types. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
The validation checks for Class::Meta::Types::Numeric are provided by the |
37
|
|
|
|
|
|
|
Data::Types module. Consult its documentation to find out what it considers to |
38
|
|
|
|
|
|
|
be a number and what's not. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
The data types created by Class::Meta::Types::Numeric are: |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=over |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=item whole |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
A whole number. That is, a positive integer. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=item integer |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=item int |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
An integer number. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=item decimal |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=item dec |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
A decimal number. |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=item real |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
A real number. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=item float |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
A floating point number. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=back |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=cut |
71
|
|
|
|
|
|
|
|
72
|
12
|
|
|
12
|
|
51968
|
use strict; |
|
12
|
|
|
|
|
27
|
|
|
12
|
|
|
|
|
453
|
|
73
|
12
|
|
|
12
|
|
66
|
use Class::Meta::Type; |
|
12
|
|
|
|
|
23
|
|
|
12
|
|
|
|
|
223
|
|
74
|
12
|
|
|
12
|
|
12448
|
use Data::Types (); |
|
12
|
|
|
|
|
32905
|
|
|
12
|
|
|
|
|
4501
|
|
75
|
|
|
|
|
|
|
our $VERSION = '0.66'; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# This code ref builds value checkers. |
78
|
|
|
|
|
|
|
my $mk_chk = sub { |
79
|
|
|
|
|
|
|
my ($code, $type) = @_; |
80
|
|
|
|
|
|
|
return [ |
81
|
|
|
|
|
|
|
sub { |
82
|
|
|
|
|
|
|
return unless defined $_[0]; |
83
|
|
|
|
|
|
|
$code->($_[0]) |
84
|
|
|
|
|
|
|
or $_[2]->class->handle_error("Value '$_[0]' is not a valid " |
85
|
|
|
|
|
|
|
. "$type"); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
]; |
88
|
|
|
|
|
|
|
}; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
############################################################################## |
91
|
|
|
|
|
|
|
sub import { |
92
|
12
|
|
|
12
|
|
117
|
my ($pkg, $builder) = @_; |
93
|
12
|
|
100
|
|
|
85
|
$builder ||= 'default'; |
94
|
12
|
50
|
|
|
|
26
|
return if eval { Class::Meta::Type->new('whole') }; |
|
12
|
|
|
|
|
176
|
|
95
|
|
|
|
|
|
|
|
96
|
12
|
|
|
|
|
67
|
Class::Meta::Type->add( |
97
|
|
|
|
|
|
|
key => "whole", |
98
|
|
|
|
|
|
|
name => "Whole Number", |
99
|
|
|
|
|
|
|
desc => "Whole number", |
100
|
|
|
|
|
|
|
builder => $builder, |
101
|
|
|
|
|
|
|
check => $mk_chk->(\&Data::Types::is_whole, 'whole number'), |
102
|
|
|
|
|
|
|
); |
103
|
|
|
|
|
|
|
|
104
|
12
|
|
|
|
|
59
|
Class::Meta::Type->add( |
105
|
|
|
|
|
|
|
key => "integer", |
106
|
|
|
|
|
|
|
name => "Integer", |
107
|
|
|
|
|
|
|
desc => "Integer", |
108
|
|
|
|
|
|
|
alias => 'int', |
109
|
|
|
|
|
|
|
builder => $builder, |
110
|
|
|
|
|
|
|
check => $mk_chk->(\&Data::Types::is_int, 'integer'), |
111
|
|
|
|
|
|
|
); |
112
|
|
|
|
|
|
|
|
113
|
12
|
|
|
|
|
55
|
Class::Meta::Type->add( |
114
|
|
|
|
|
|
|
key => "decimal", |
115
|
|
|
|
|
|
|
name => "Decimal Number", |
116
|
|
|
|
|
|
|
desc => "Decimal number", |
117
|
|
|
|
|
|
|
alias => 'dec', |
118
|
|
|
|
|
|
|
builder => $builder, |
119
|
|
|
|
|
|
|
check => $mk_chk->(\&Data::Types::is_decimal, 'decimal number'), |
120
|
|
|
|
|
|
|
); |
121
|
|
|
|
|
|
|
|
122
|
12
|
|
|
|
|
51
|
Class::Meta::Type->add( |
123
|
|
|
|
|
|
|
key => "real", |
124
|
|
|
|
|
|
|
name => "Real Number", |
125
|
|
|
|
|
|
|
desc => "Real number", |
126
|
|
|
|
|
|
|
builder => $builder, |
127
|
|
|
|
|
|
|
check => $mk_chk->(\&Data::Types::is_real, 'real number'), |
128
|
|
|
|
|
|
|
); |
129
|
|
|
|
|
|
|
|
130
|
12
|
|
|
|
|
57
|
Class::Meta::Type->add( |
131
|
|
|
|
|
|
|
key => "float", |
132
|
|
|
|
|
|
|
name => "Floating Point Number", |
133
|
|
|
|
|
|
|
desc => "Floating point number", |
134
|
|
|
|
|
|
|
builder => $builder, |
135
|
|
|
|
|
|
|
check => $mk_chk->(\&Data::Types::is_float, 'floating point number'), |
136
|
|
|
|
|
|
|
); |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
1; |
140
|
|
|
|
|
|
|
__END__ |