Implemented inlning for all string methods.
[gitmo/Moose.git] / lib / Moose / Meta / Attribute / Native / Trait / String.pm
CommitLineData
c466e58f 1package Moose::Meta::Attribute::Native::Trait::String;
e3c07b19 2use Moose::Role;
3
b6cca0d5 4our $VERSION = '1.14';
e3c07b19 5$VERSION = eval $VERSION;
6our $AUTHORITY = 'cpan:STEVAN';
7
e7724627 8use Moose::Meta::Method::Accessor::Native::String::append;
9use Moose::Meta::Method::Accessor::Native::String::chomp;
10use Moose::Meta::Method::Accessor::Native::String::chop;
11use Moose::Meta::Method::Accessor::Native::String::clear;
12use Moose::Meta::Method::Accessor::Native::String::inc;
13use Moose::Meta::Method::Accessor::Native::String::length;
14use Moose::Meta::Method::Accessor::Native::String::match;
15use Moose::Meta::Method::Accessor::Native::String::prepend;
16use Moose::Meta::Method::Accessor::Native::String::replace;
17use Moose::Meta::Method::Accessor::Native::String::substr;
e3c07b19 18
c466e58f 19with 'Moose::Meta::Attribute::Native::Trait';
e3c07b19 20
2edb73d9 21sub _default_default { q{} }
22sub _default_is { 'rw' }
2e069f5a 23sub _helper_type { 'Str' }
e3c07b19 24
e7724627 25sub _native_type { 'String' }
26
e3c07b19 27no Moose::Role;
28
e3c07b19 291;
30
31__END__
32
33=pod
34
35=head1 NAME
36
2420461c 37Moose::Meta::Attribute::Native::Trait::String - Helper trait for Str attributes
e3c07b19 38
39=head1 SYNOPSIS
40
41 package MyHomePage;
42 use Moose;
e3c07b19 43
44 has 'text' => (
9610c1d2 45 traits => ['String'],
e3c07b19 46 is => 'rw',
47 isa => 'Str',
2edb73d9 48 default => q{},
9610c1d2 49 handles => {
5f3663b2 50 add_text => 'append',
51 replace_text => 'replace',
9610c1d2 52 },
e3c07b19 53 );
54
55 my $page = MyHomePage->new();
56 $page->add_text("foo"); # same as $page->text($page->text . "foo");
57
58=head1 DESCRIPTION
59
60This module provides a simple string attribute, to which mutating string
61operations can be applied more easily (no need to make an lvalue attribute
62metaclass or use temporary variables). Additional methods are provided for
63completion.
64
65If your attribute definition does not include any of I<is>, I<isa>,
5f3663b2 66I<default> or I<handles> but does use the C<String> metaclass,
e3c07b19 67then this module applies defaults as in the L</SYNOPSIS>
2420461c 68above. This allows for a very basic string definition:
e3c07b19 69
2420461c 70 has 'foo' => (traits => ['String']);
e3c07b19 71 $obj->append_foo;
72
e3c07b19 73=head1 PROVIDED METHODS
74
79b647c6 75These methods are implemented in
2420461c 76L<Moose::Meta::Attribute::Native::MethodProvider::String>. It is important to
79b647c6 77note that all those methods do in place modification of the value stored in
78the attribute.
e3c07b19 79
80=over 4
81
157e0475 82=item B<inc>
e3c07b19 83
84Increments the value stored in this slot using the magical string autoincrement
a6dd8c94 85operator. Note that Perl doesn't provide analogous behavior in C<-->, so
e3c07b19 86C<dec> is not available.
87
157e0475 88=item B<append($string)>
e3c07b19 89
90Append a string, like C<.=>.
91
157e0475 92=item B<prepend($string)>
e3c07b19 93
94Prepend a string.
95
157e0475 96=item B<replace($pattern, $replacement)>
e3c07b19 97
98Performs a regexp substitution (L<perlop/s>). There is no way to provide the
99C<g> flag, but code references will be accepted for the replacement, causing
100the regex to be modified with a single C<e>. C</smxi> can be applied using the
101C<qr> operator.
102
157e0475 103=item B<match($pattern)>
e3c07b19 104
157e0475 105Like C<replace> but without the replacement. Provided mostly for completeness.
e3c07b19 106
157e0475 107=item B<chop>
e3c07b19 108
109L<perlfunc/chop>
110
157e0475 111=item B<chomp>
e3c07b19 112
113L<perlfunc/chomp>
114
157e0475 115=item B<clear>
e3c07b19 116
117Sets the string to the empty string (not the value passed to C<default>).
118
eb95da0e 119=item B<length>
120
121L<perlfunc/length>
122
123=item B<substr>
124
125L<perlfunc/substr>. We go to some lengths to match the different functionality
126based on C<substr>'s arity.
127
e3c07b19 128=back
129
79b647c6 130=head1 METHODS
131
132=over 4
133
134=item B<meta>
135
136=item B<method_provider>
137
138=item B<has_method_provider>
139
140=back
141
e3c07b19 142=head1 BUGS
143
d4048ef3 144See L<Moose/BUGS> for details on reporting bugs.
e3c07b19 145
146=head1 AUTHOR
147
148Stevan Little E<lt>stevan@iinteractive.comE<gt>
149
150=head1 COPYRIGHT AND LICENSE
151
152Copyright 2007-2009 by Infinity Interactive, Inc.
153
154L<http://www.iinteractive.com>
155
156This library is free software; you can redistribute it and/or modify
157it under the same terms as Perl itself.
158
159=cut