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