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