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