perltidy all the AttributeHelpers code
[gitmo/Moose.git] / lib / Moose / AttributeHelpers / Trait / String.pm
CommitLineData
e3c07b19 1
2package Moose::AttributeHelpers::Trait::String;
3use Moose::Role;
4
37b7c240 5our $VERSION = '0.84';
e3c07b19 6$VERSION = eval $VERSION;
7our $AUTHORITY = 'cpan:STEVAN';
8
9use Moose::AttributeHelpers::MethodProvider::String;
10
11with 'Moose::AttributeHelpers::Trait::Base';
12
13has 'method_provider' => (
14 is => 'ro',
15 isa => 'ClassName',
16 predicate => 'has_method_provider',
17 default => 'Moose::AttributeHelpers::MethodProvider::String',
18);
19
20sub helper_type { 'Str' }
21
25d86888 22before 'process_options_for_handles' => sub {
046c8b5e 23 my ( $self, $options, $name ) = @_;
e3c07b19 24
25 # Set some default attribute options here unless already defined
046c8b5e 26 if ( ( my $type = $self->helper_type ) && !exists $options->{isa} ) {
e3c07b19 27 $options->{isa} = $type;
28 }
29
30 $options->{is} = 'rw' unless exists $options->{is};
31 $options->{default} = '' unless exists $options->{default};
32};
33
25d86888 34after 'check_handles_values' => sub {
046c8b5e 35 my $self = shift;
40ef30a5 36 my $handles = $self->handles;
e3c07b19 37
046c8b5e 38 unless ( scalar keys %$handles ) {
e3c07b19 39 my $method_constructors = $self->method_constructors;
40 my $attr_name = $self->name;
41
046c8b5e 42 foreach my $method ( keys %$method_constructors ) {
43 $handles->{$method} = ( $method . '_' . $attr_name );
e3c07b19 44 }
45 }
46};
47
48no Moose::Role;
49
50# register the alias ...
51package # hide me from search.cpan.org
52 Moose::Meta::Attribute::Custom::Trait::String;
53sub register_implementation { 'Moose::AttributeHelpers::Trait::String' }
54
551;
56
57__END__
58
59=pod
60
61=head1 NAME
62
63Moose::AttributeHelpers::String
64
65=head1 SYNOPSIS
66
67 package MyHomePage;
68 use Moose;
69 use Moose::AttributeHelpers;
70
71 has 'text' => (
72 metaclass => 'String',
73 is => 'rw',
74 isa => 'Str',
75 default => sub { '' },
5f3663b2 76 handles => {
77 add_text => 'append',
78 replace_text => 'replace',
e3c07b19 79 }
80 );
81
82 my $page = MyHomePage->new();
83 $page->add_text("foo"); # same as $page->text($page->text . "foo");
84
85=head1 DESCRIPTION
86
87This module provides a simple string attribute, to which mutating string
88operations can be applied more easily (no need to make an lvalue attribute
89metaclass or use temporary variables). Additional methods are provided for
90completion.
91
92If your attribute definition does not include any of I<is>, I<isa>,
5f3663b2 93I<default> or I<handles> but does use the C<String> metaclass,
e3c07b19 94then this module applies defaults as in the L</SYNOPSIS>
95above. This allows for a very basic counter definition:
96
97 has 'foo' => (metaclass => 'String');
98 $obj->append_foo;
99
100=head1 METHODS
101
102=over 4
103
104=item B<meta>
105
106=item B<method_provider>
107
108=item B<has_method_provider>
109
110=item B<helper_type>
111
25d86888 112=item B<process_options_for_handles>
e3c07b19 113
114Run before its superclass method.
115
25d86888 116=item B<check_handles_values>
e3c07b19 117
118Run after its superclass method.
119
120=back
121
122=head1 PROVIDED METHODS
123
124It is important to note that all those methods do in place
125modification of the value stored in the attribute.
126
127=over 4
128
129=item I<inc>
130
131Increments the value stored in this slot using the magical string autoincrement
a6dd8c94 132operator. Note that Perl doesn't provide analogous behavior in C<-->, so
e3c07b19 133C<dec> is not available.
134
135=item I<append> C<$string>
136
137Append a string, like C<.=>.
138
139=item I<prepend> C<$string>
140
141Prepend a string.
142
143=item I<replace> C<$pattern> C<$replacement>
144
145Performs a regexp substitution (L<perlop/s>). There is no way to provide the
146C<g> flag, but code references will be accepted for the replacement, causing
147the regex to be modified with a single C<e>. C</smxi> can be applied using the
148C<qr> operator.
149
150=item I<match> C<$pattern>
151
152Like I<replace> but without the replacement. Provided mostly for completeness.
153
154=item C<chop>
155
156L<perlfunc/chop>
157
158=item C<chomp>
159
160L<perlfunc/chomp>
161
162=item C<clear>
163
164Sets the string to the empty string (not the value passed to C<default>).
165
166=back
167
168=head1 BUGS
169
170All complex software has bugs lurking in it, and this module is no
171exception. If you find a bug please either email me, or add the bug
172to cpan-RT.
173
174=head1 AUTHOR
175
176Stevan Little E<lt>stevan@iinteractive.comE<gt>
177
178=head1 COPYRIGHT AND LICENSE
179
180Copyright 2007-2009 by Infinity Interactive, Inc.
181
182L<http://www.iinteractive.com>
183
184This library is free software; you can redistribute it and/or modify
185it under the same terms as Perl itself.
186
187=cut