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