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