Commit | Line | Data |
e3c07b19 |
1 | |
a40b446a |
2 | package Moose::Meta::Attribute::Trait::Native::String; |
e3c07b19 |
3 | use Moose::Role; |
4 | |
96539d20 |
5 | our $VERSION = '0.87'; |
e3c07b19 |
6 | $VERSION = eval $VERSION; |
7 | our $AUTHORITY = 'cpan:STEVAN'; |
8 | |
a40b446a |
9 | use Moose::Meta::Attribute::Trait::Native::MethodProvider::String; |
e3c07b19 |
10 | |
a40b446a |
11 | with 'Moose::Meta::Attribute::Trait::Native::Base'; |
e3c07b19 |
12 | |
13 | has '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 |
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 | package # hide me from search.cpan.org |
41 | Moose::Meta::Attribute::Custom::Trait::String; |
a40b446a |
42 | sub register_implementation { 'Moose::Meta::Attribute::Trait::Native::String' } |
e3c07b19 |
43 | |
44 | 1; |
45 | |
46 | __END__ |
47 | |
48 | =pod |
49 | |
50 | =head1 NAME |
51 | |
a40b446a |
52 | Moose::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 | |
76 | This module provides a simple string attribute, to which mutating string |
77 | operations can be applied more easily (no need to make an lvalue attribute |
78 | metaclass or use temporary variables). Additional methods are provided for |
79 | completion. |
80 | |
81 | If your attribute definition does not include any of I<is>, I<isa>, |
5f3663b2 |
82 | I<default> or I<handles> but does use the C<String> metaclass, |
e3c07b19 |
83 | then this module applies defaults as in the L</SYNOPSIS> |
84 | above. 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 | |
103 | It is important to note that all those methods do in place |
104 | modification of the value stored in the attribute. |
105 | |
106 | =over 4 |
107 | |
108 | =item I<inc> |
109 | |
110 | Increments the value stored in this slot using the magical string autoincrement |
a6dd8c94 |
111 | operator. Note that Perl doesn't provide analogous behavior in C<-->, so |
e3c07b19 |
112 | C<dec> is not available. |
113 | |
114 | =item I<append> C<$string> |
115 | |
116 | Append a string, like C<.=>. |
117 | |
118 | =item I<prepend> C<$string> |
119 | |
120 | Prepend a string. |
121 | |
122 | =item I<replace> C<$pattern> C<$replacement> |
123 | |
124 | Performs a regexp substitution (L<perlop/s>). There is no way to provide the |
125 | C<g> flag, but code references will be accepted for the replacement, causing |
126 | the regex to be modified with a single C<e>. C</smxi> can be applied using the |
127 | C<qr> operator. |
128 | |
129 | =item I<match> C<$pattern> |
130 | |
131 | Like I<replace> but without the replacement. Provided mostly for completeness. |
132 | |
133 | =item C<chop> |
134 | |
135 | L<perlfunc/chop> |
136 | |
137 | =item C<chomp> |
138 | |
139 | L<perlfunc/chomp> |
140 | |
141 | =item C<clear> |
142 | |
143 | Sets the string to the empty string (not the value passed to C<default>). |
144 | |
145 | =back |
146 | |
147 | =head1 BUGS |
148 | |
149 | All complex software has bugs lurking in it, and this module is no |
150 | exception. If you find a bug please either email me, or add the bug |
151 | to cpan-RT. |
152 | |
153 | =head1 AUTHOR |
154 | |
155 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
156 | |
157 | =head1 COPYRIGHT AND LICENSE |
158 | |
159 | Copyright 2007-2009 by Infinity Interactive, Inc. |
160 | |
161 | L<http://www.iinteractive.com> |
162 | |
163 | This library is free software; you can redistribute it and/or modify |
164 | it under the same terms as Perl itself. |
165 | |
166 | =cut |