line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MooseX::Meta::Parameter::Moose; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
4057
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
use Moose::Util::TypeConstraints; |
6
|
|
|
|
|
|
|
use MooseX::Method::Exception; |
7
|
|
|
|
|
|
|
use Scalar::Util qw/blessed/; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
with qw/MooseX::Meta::Parameter/; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has isa => (is => 'bare', isa => 'Str | Object'); |
12
|
|
|
|
|
|
|
has does => (is => 'bare', isa => 'Str'); |
13
|
|
|
|
|
|
|
has required => (is => 'bare', isa => 'Bool'); |
14
|
|
|
|
|
|
|
has default => (is => 'bare', isa => 'Defined'); |
15
|
|
|
|
|
|
|
has coerce => (is => 'bare', isa => 'Bool'); |
16
|
|
|
|
|
|
|
has type_constraint => (is => 'bare', isa => 'Moose::Meta::TypeConstraint'); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:BERLE'; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub BUILD { |
23
|
|
|
|
|
|
|
my ($self) = @_; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
if (defined $self->{isa}) { |
26
|
|
|
|
|
|
|
if (blessed ($self->{isa})) { |
27
|
|
|
|
|
|
|
if ($self->{isa}->isa ('Moose::Meta::TypeConstraint')) { |
28
|
|
|
|
|
|
|
$self->{type_constraint} = $self->{isa}; |
29
|
|
|
|
|
|
|
} else { |
30
|
|
|
|
|
|
|
MooseX::Method::Exception->throw ("You cannot specify an object as type if it's not a type constraint"); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
} else { |
33
|
|
|
|
|
|
|
if ($self->{isa} =~ /\|/) { |
34
|
|
|
|
|
|
|
my @type_constraints = split /\s*\|\s*/,$self->{isa}; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
$self->{type_constraint} = Moose::Util::TypeConstraints::create_type_constraint_union (@type_constraints); |
37
|
|
|
|
|
|
|
} else { |
38
|
|
|
|
|
|
|
my $constraint = find_type_constraint ($self->{isa}); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
$constraint = subtype ('Object',where { $_->isa ($self->{isa}) }) |
41
|
|
|
|
|
|
|
unless defined $constraint; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
$self->{type_constraint} = $constraint; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
if ($self->{coerce}) { |
49
|
|
|
|
|
|
|
MooseX::Method::Exception->throw ("You cannot set coerce if type does not support this") |
50
|
|
|
|
|
|
|
unless defined $self->{type_constraint} && $self->{type_constraint}->has_coercion; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
return; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub validate { |
57
|
|
|
|
|
|
|
my ($self,$value) = @_; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
my $provided = ($#_ > 0 ? 1 : 0); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
if (! $provided && defined $self->{default}) { |
62
|
|
|
|
|
|
|
if (ref $self->{default} eq 'CODE') { |
63
|
|
|
|
|
|
|
$value = $self->{default}->(); |
64
|
|
|
|
|
|
|
} else { |
65
|
|
|
|
|
|
|
$value = $self->{default}; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
$provided = 1; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
if ($provided) { |
72
|
|
|
|
|
|
|
if (defined $self->{type_constraint}) { |
73
|
|
|
|
|
|
|
my $constraint = $self->{type_constraint}; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
unless ($constraint->check ($value)) { |
76
|
|
|
|
|
|
|
if ($self->{coerce}) { |
77
|
|
|
|
|
|
|
my $return = $constraint->coerce ($value); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
MooseX::Method::Exception->throw ("Argument isn't ($self->{isa})") |
80
|
|
|
|
|
|
|
unless $constraint->check ($return); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
$value = $return; |
83
|
|
|
|
|
|
|
} else { |
84
|
|
|
|
|
|
|
MooseX::Method::Exception->throw ("Argument isn't ($self->{isa})"); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
if (defined $self->{does}) { |
90
|
|
|
|
|
|
|
unless (blessed $value && $value->can ('does') && $value->does ($self->{does})) { |
91
|
|
|
|
|
|
|
MooseX::Method::Exception->throw ("Does not do ($self->{does})"); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
} elsif ($self->{required}) { |
95
|
|
|
|
|
|
|
MooseX::Method::Exception->throw ("Must be specified"); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
return $value; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
1; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
__END__ |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=pod |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 NAME |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
MooseX::Meta::Parameter::Moose - Moose style parameter metaclass |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 WARNING |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
This API is unstable, it may change at any time. This should not |
116
|
|
|
|
|
|
|
affect ordinary L<MooseX::Method> usage. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 SYNOPSIS |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
use MooseX::Meta::Parameter::Moose; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
my $parameter = MooseX::Meta::Parameter::Moose->new (isa => 'Int'); |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
my $result; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
eval { |
127
|
|
|
|
|
|
|
$result = $parameter->validate ("foo"); |
128
|
|
|
|
|
|
|
}; |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
print Dumper($parameter->export); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 METHODS |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=over 4 |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=item B<validate> |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Takes an argument, validates it, and returns the argument or possibly |
139
|
|
|
|
|
|
|
a coerced version of it. Exceptions are thrown on validation failure. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=back |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 BUGS |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Most software has bugs. This module probably isn't an exception. |
146
|
|
|
|
|
|
|
If you find a bug please either email me, or add the bug to cpan-RT. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 AUTHOR |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Anders Nor Berle E<lt>debolaz@gmail.comE<gt> |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Copyright 2007 by Anders Nor Berle. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
157
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=cut |
160
|
|
|
|
|
|
|
|