Commit | Line | Data |
c466e58f |
1 | package Moose::Meta::Attribute::Native::Trait::String; |
e3c07b19 |
2 | use Moose::Role; |
3 | |
b6cca0d5 |
4 | our $VERSION = '1.14'; |
e3c07b19 |
5 | $VERSION = eval $VERSION; |
6 | our $AUTHORITY = 'cpan:STEVAN'; |
7 | |
e7724627 |
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; |
e3c07b19 |
18 | |
c466e58f |
19 | with 'Moose::Meta::Attribute::Native::Trait'; |
e3c07b19 |
20 | |
2edb73d9 |
21 | sub _default_default { q{} } |
22 | sub _default_is { 'rw' } |
2e069f5a |
23 | sub _helper_type { 'Str' } |
e3c07b19 |
24 | |
e7724627 |
25 | sub _native_type { 'String' } |
26 | |
e3c07b19 |
27 | no Moose::Role; |
28 | |
e3c07b19 |
29 | 1; |
30 | |
31 | __END__ |
32 | |
33 | =pod |
34 | |
35 | =head1 NAME |
36 | |
2420461c |
37 | Moose::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 | |
60 | This module provides a simple string attribute, to which mutating string |
61 | operations can be applied more easily (no need to make an lvalue attribute |
62 | metaclass or use temporary variables). Additional methods are provided for |
63 | completion. |
64 | |
65 | If your attribute definition does not include any of I<is>, I<isa>, |
5f3663b2 |
66 | I<default> or I<handles> but does use the C<String> metaclass, |
e3c07b19 |
67 | then this module applies defaults as in the L</SYNOPSIS> |
2420461c |
68 | above. 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 |
75 | These methods are implemented in |
2420461c |
76 | L<Moose::Meta::Attribute::Native::MethodProvider::String>. It is important to |
79b647c6 |
77 | note that all those methods do in place modification of the value stored in |
78 | the attribute. |
e3c07b19 |
79 | |
80 | =over 4 |
81 | |
157e0475 |
82 | =item B<inc> |
e3c07b19 |
83 | |
84 | Increments the value stored in this slot using the magical string autoincrement |
a6dd8c94 |
85 | operator. Note that Perl doesn't provide analogous behavior in C<-->, so |
e3c07b19 |
86 | C<dec> is not available. |
87 | |
157e0475 |
88 | =item B<append($string)> |
e3c07b19 |
89 | |
90 | Append a string, like C<.=>. |
91 | |
157e0475 |
92 | =item B<prepend($string)> |
e3c07b19 |
93 | |
94 | Prepend a string. |
95 | |
157e0475 |
96 | =item B<replace($pattern, $replacement)> |
e3c07b19 |
97 | |
98 | Performs a regexp substitution (L<perlop/s>). There is no way to provide the |
99 | C<g> flag, but code references will be accepted for the replacement, causing |
100 | the regex to be modified with a single C<e>. C</smxi> can be applied using the |
101 | C<qr> operator. |
102 | |
157e0475 |
103 | =item B<match($pattern)> |
e3c07b19 |
104 | |
157e0475 |
105 | Like C<replace> but without the replacement. Provided mostly for completeness. |
e3c07b19 |
106 | |
157e0475 |
107 | =item B<chop> |
e3c07b19 |
108 | |
109 | L<perlfunc/chop> |
110 | |
157e0475 |
111 | =item B<chomp> |
e3c07b19 |
112 | |
113 | L<perlfunc/chomp> |
114 | |
157e0475 |
115 | =item B<clear> |
e3c07b19 |
116 | |
117 | Sets the string to the empty string (not the value passed to C<default>). |
118 | |
eb95da0e |
119 | =item B<length> |
120 | |
121 | L<perlfunc/length> |
122 | |
123 | =item B<substr> |
124 | |
125 | L<perlfunc/substr>. We go to some lengths to match the different functionality |
126 | based 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 |
144 | See L<Moose/BUGS> for details on reporting bugs. |
e3c07b19 |
145 | |
146 | =head1 AUTHOR |
147 | |
148 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
149 | |
150 | =head1 COPYRIGHT AND LICENSE |
151 | |
152 | Copyright 2007-2009 by Infinity Interactive, Inc. |
153 | |
154 | L<http://www.iinteractive.com> |
155 | |
156 | This library is free software; you can redistribute it and/or modify |
157 | it under the same terms as Perl itself. |
158 | |
159 | =cut |