Lots of doc improvements for native delegations.
[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
efa728b4 4our $VERSION = '1.15';
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
e3c07b19 25no Moose::Role;
26
e3c07b19 271;
28
29__END__
30
31=pod
32
33=head1 NAME
34
2420461c 35Moose::Meta::Attribute::Native::Trait::String - Helper trait for Str attributes
e3c07b19 36
37=head1 SYNOPSIS
38
39 package MyHomePage;
40 use Moose;
e3c07b19 41
42 has 'text' => (
e132fd56 43 traits => ['String'],
44 is => 'rw',
45 isa => 'Str',
46 default => q{},
47 handles => {
5f3663b2 48 add_text => 'append',
49 replace_text => 'replace',
9610c1d2 50 },
e3c07b19 51 );
52
53 my $page = MyHomePage->new();
e132fd56 54 $page->add_text("foo"); # same as $page->text($page->text . "foo");
e3c07b19 55
56=head1 DESCRIPTION
57
58This module provides a simple string attribute, to which mutating string
59operations can be applied more easily (no need to make an lvalue attribute
60metaclass or use temporary variables). Additional methods are provided for
61completion.
62
63If your attribute definition does not include any of I<is>, I<isa>,
5f3663b2 64I<default> or I<handles> but does use the C<String> metaclass,
e3c07b19 65then this module applies defaults as in the L</SYNOPSIS>
2420461c 66above. This allows for a very basic string definition:
e3c07b19 67
2420461c 68 has 'foo' => (traits => ['String']);
e3c07b19 69 $obj->append_foo;
70
e3c07b19 71=head1 PROVIDED METHODS
72
e3c07b19 73=over 4
74
e132fd56 75=item * B<inc>
e3c07b19 76
77Increments the value stored in this slot using the magical string autoincrement
a6dd8c94 78operator. Note that Perl doesn't provide analogous behavior in C<-->, so
e132fd56 79C<dec> is not available. This method returns the new value.
e3c07b19 80
e132fd56 81This method does not accept any arguments.
e3c07b19 82
e132fd56 83=item * B<append($string)>
e3c07b19 84
e132fd56 85Appends to the string, like C<.=>, and returns the new value.
e3c07b19 86
e132fd56 87This method requires a single argument.
e3c07b19 88
e132fd56 89=item * B<prepend($string)>
90
91Prepends to the string and returns the new value.
92
93This method requires a single argument.
94
95=item * B<replace($pattern, $replacement)>
e3c07b19 96
97Performs a regexp substitution (L<perlop/s>). There is no way to provide the
98C<g> flag, but code references will be accepted for the replacement, causing
99the regex to be modified with a single C<e>. C</smxi> can be applied using the
e132fd56 100C<qr> operator. This method returns the new value.
e3c07b19 101
e132fd56 102This method requires two arguments.
e3c07b19 103
e132fd56 104=item * B<match($pattern)>
e3c07b19 105
e132fd56 106Runs the regex against the string and returns the matching value(s).
e3c07b19 107
e132fd56 108This method requires a single argument.
e3c07b19 109
e132fd56 110=item * B<chop>
e3c07b19 111
e132fd56 112Just like L<perlfunc/chop>. This method returns the chopped character.
e3c07b19 113
e132fd56 114This method does not accept any arguments.
e3c07b19 115
e132fd56 116=item * B<chomp>
117
118Just like L<perlfunc/chomp>. This method returns the number of characters
119removed.
e3c07b19 120
e132fd56 121This method does not accept any arguments.
eb95da0e 122
e132fd56 123=item * B<clear>
eb95da0e 124
e132fd56 125Sets the string to the empty string (not the value passed to C<default>).
eb95da0e 126
e132fd56 127This method does not have a defined return value.
eb95da0e 128
e132fd56 129This method does not accept any arguments.
e3c07b19 130
e132fd56 131=item * B<length>
79b647c6 132
e132fd56 133Just like L<perlfunc/length>, returns the length of the string.
134
135=item * B<substr>
136
137This acts just like L<perlfunc/substr>. When called as a writer, it returns
138the substring that was replaced, just like the Perl builtin.
79b647c6 139
e132fd56 140This method requires at least one argument, and accepts no more than three.
79b647c6 141
79b647c6 142=back
143
e3c07b19 144=head1 BUGS
145
d4048ef3 146See L<Moose/BUGS> for details on reporting bugs.
e3c07b19 147
148=head1 AUTHOR
149
150Stevan Little E<lt>stevan@iinteractive.comE<gt>
151
152=head1 COPYRIGHT AND LICENSE
153
154Copyright 2007-2009 by Infinity Interactive, Inc.
155
156L<http://www.iinteractive.com>
157
158This library is free software; you can redistribute it and/or modify
159it under the same terms as Perl itself.
160
161=cut