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