Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / Parse / Method / Signatures / Param.pm
1 package Parse::Method::Signatures::Param;
2
3 use Moose;
4 use MooseX::Types::Structured qw/Tuple/;
5 use MooseX::Types::Moose qw/Bool Str ArrayRef HashRef/;
6
7 use namespace::clean -except => 'meta';
8
9 with 'MooseX::Traits';
10
11 has required => (
12     is       => 'ro',
13     isa      => Bool,
14     required => 1
15 );
16
17 has sigil => (
18     is       => 'ro',
19     isa      => Str,
20     required => 1,
21 );
22
23 has type_constraints => (
24     is         => 'ro',
25     isa        => 'Parse::Method::Signatures::TypeConstraint',
26     predicate  => 'has_type_constraints',
27     handles    => {
28         meta_type_constraint => 'tc'
29     },
30 );
31
32 has default_value => (
33     is        => 'ro',
34     isa       => Str,
35     predicate => 'has_default_value',
36 );
37
38 has constraints => (
39     is         => 'ro',
40     isa        => ArrayRef[Str],
41     predicate  => 'has_constraints',
42     auto_deref => 1,
43 );
44
45 has param_traits => (
46     is         => 'ro',
47     isa        => ArrayRef[Tuple[Str, Str]],
48     predicate  => 'has_traits',
49     auto_deref => 1
50 );
51
52 has '+_trait_namespace' => (
53     default => 'Parse::Method::Signatures::Param',
54 );
55
56 sub _stringify_type_constraints {
57     my ($self) = @_;
58     return $self->has_type_constraints
59         ? $self->type_constraints->to_string . q{ }
60         : q{};
61 }
62
63 sub _stringify_default_value {
64     my ($self) = @_;
65     return $self->has_default_value
66         ? q{ = } . $self->default_value
67         : q{};
68 }
69
70 sub _stringify_constraints {
71     my ($self) = @_;
72     return q{} unless $self->has_constraints;
73     return q{ where } . join(q{ where }, $self->constraints);
74 }
75
76 sub _stringify_traits {
77     my ($self) = @_;
78     return q{} unless $self->has_traits;
79     return q{ } . join q{ }, map { @{ $_ } } $self->param_traits;
80 }
81
82 sub to_string {
83     my ($self) = @_;
84     my $ret = q{};
85
86     $ret .= $self->_stringify_type_constraints;
87     $ret .= $self->_stringify_variable_name;
88     $ret .= $self->_stringify_required;
89     $ret .= $self->_stringify_default_value;
90     $ret .= $self->_stringify_constraints;
91     $ret .= $self->_stringify_traits;
92
93     return $ret;
94 }
95
96 __PACKAGE__->meta->make_immutable;
97
98 1;
99
100 =head1 NAME
101
102 Parse::Method::Signatures::Param - a parsed parameter from a signature
103
104 =head1 ATTRIBUTES
105
106 All attributes of this class are read-only.
107
108 =head2 required
109
110 Is this parameter required (true) or optional (false)?
111
112 =head2 sigil
113
114 The effective sigil ('$', '@' or '%') of this parameter.
115
116 =head2 type_constraints
117
118 =over
119
120 B<Type:> L<Parse::Method::Signatures::TypeConstraint>
121
122 B<Predicate:> has_type_constraints
123
124 =back
125
126 Representation of the type constraint for this parameter. Most commonly you
127 will just call L</meta_type_constraint> and not access this attribute directly.
128
129 =head2 default_value
130
131 =over
132
133 B<Type:> Str
134
135 B<Predicate:> has_default_value
136
137 =back
138
139 A string that should be eval'd or injected to get the default value for this
140 parameter. For example:
141
142  $name = 'bar'
143
144 Would give a default_value of "'bar'".
145
146 =head2 constraints
147
148 =over
149
150 B<Type:> ArrayRef[Str]
151
152 B<Predicate:> has_constraints
153
154 =back
155
156 C<where> constraints for this type. Each member of the array a the string
157 (including enclosing braces) of the where constraint block.
158
159 =head2 param_traits
160
161 =over
162
163 B<Type:> ArrayRef[ Tupple[Str,Str] ]
164
165 B<Predicate:> has_traits
166
167 =back
168
169 Traits that this parameter is declared to have. For instance
170
171  $foo does coerce
172
173 would have a trait of
174
175  ['does', 'coerce']
176
177 =head1 METHODS
178
179 =head2 to_string
180
181 =head2 meta_type_constraint
182
183 Get the L<Moose::Meta::TypeConstraint> for this parameter. Check first that the
184 type has a type constraint:
185
186  $tc = $param->meta_type_constraint if $param->has_type_constraints;
187
188 =head1 SEE ALSO
189
190 L<Parse::Method::Signatures>.
191
192 =head1 AUTHORS
193
194 Ash Berlin <ash@cpan.org>.
195
196 Florian Ragwitz <rafl@debian.org>.
197
198 =head1 LICENSE
199
200 Licensed under the same terms as Perl itself.
201