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