Composite now implemented.
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / MethodProvider / String.pm
CommitLineData
190b1c02 1package MooseX::AttributeHelpers::MethodProvider::String;
786dbc3d 2use MooseX::AttributeHelpers::MethodProvider;
190b1c02 3
4our $VERSION = '0.01';
5our $AUTHORITY = 'cpan:STEVAN';
6
786dbc3d 7add_method_provider String => (
8 type => 'Str',
9 provides => {
10 append => sub {
11 my ($attr, $reader, $writer) = @_;
12 return sub { $writer->( $_[0], $reader->($_[0]) . $_[1] ) };
13 },
14
15 prepend => sub {
16 my ($attr, $reader, $writer) = @_;
17 return sub { $writer->( $_[0], $_[1] . $reader->($_[0]) ) };
18 },
19
20 replace => sub {
21 my ($attr, $reader, $writer) = @_;
22 return sub {
23 my ( $self, $regex, $replacement ) = @_;
24 my $v = $reader->($_[0]);
25
26 if ( (ref($replacement)||'') eq 'CODE' ) {
27 $v =~ s/$regex/$replacement->()/e;
28 } else {
29 $v =~ s/$regex/$replacement/;
30 }
31
32 $writer->( $_[0], $v);
33 };
34 },
35
36 match => sub {
37 my ($attr, $reader, $writer) = @_;
38 return sub { $reader->($_[0]) =~ $_[1] };
39 },
40
41 chop => sub {
42 my ($attr, $reader, $writer) = @_;
43 return sub {
44 my $v = $reader->($_[0]);
45 CORE::chop($v);
46 $writer->( $_[0], $v);
47 };
48 },
49
50 chomp => sub {
51 my ($attr, $reader, $writer) = @_;
52 return sub {
53 my $v = $reader->($_[0]);
54 chomp($v);
55 $writer->( $_[0], $v);
56 };
57 },
58
59 inc => sub {
60 my ($attr, $reader, $writer) = @_;
61 return sub {
62 my $v = $reader->($_[0]);
63 $v++;
64 $writer->( $_[0], $v);
65 };
66 },
67
68 clear => sub {
69 my ($attr, $reader, $writer ) = @_;
70 return sub { $writer->( $_[0], '' ) }
71 },
72 },
73);
190b1c02 74
751;
76
77__END__
78
79=pod
80
81=head1 NAME
82
83MooseX::AttributeHelpers::MethodProvider::String
786dbc3d 84
190b1c02 85=head1 DESCRIPTION
86
786dbc3d 87This is a role which provides the method generators for
190b1c02 88L<MooseX::AttributeHelpers::String>.
89
720fa35b 90=head1 PROVIDED METHODS
91
92It should be noted that all methods modify attribute values in place.
190b1c02 93
94=over 4
95
720fa35b 96=item I<inc>
190b1c02 97
720fa35b 98Increments the value stored in this slot using the magical string autoincrement
99operator. Note that Perl doesn't provide analogeous behavior in C<-->, so
100C<dec> is not available.
190b1c02 101
720fa35b 102=item I<append> C<$string>
190b1c02 103
720fa35b 104Append a string, like C<.=>.
105
106=item I<prepend> C<$string>
107
108Prepend a string.
109
110=item I<replace> C<$pattern> C<$replacement>
111
112Performs a regexp substitution (L<perlop/s>). There is no way to provide the
113C<g> flag, but code references will be accepted for the replacement, causing
114the regex to be modified with a single C<e>. C</smxi> can be applied using the
115C<qr> operator.
190b1c02 116
720fa35b 117=item I<match> C<$pattern>
190b1c02 118
720fa35b 119Like I<replace> but without the replacement. Provided mostly for completeness.
190b1c02 120
720fa35b 121=item C<chop>
190b1c02 122
720fa35b 123L<perlfunc/chop>
190b1c02 124
720fa35b 125=item C<chomp>
190b1c02 126
720fa35b 127L<perlfunc/chomp>
190b1c02 128
720fa35b 129=item C<clear>
190b1c02 130
720fa35b 131Sets the string to the empty string (not the value passed to C<default>).
190b1c02 132
133=back
134
135=head1 BUGS
136
786dbc3d 137All complex software has bugs lurking in it, and this module is no
190b1c02 138exception. If you find a bug please either email me, or add the bug
139to cpan-RT.
140
141=head1 AUTHOR
142
143Stevan Little E<lt>stevan@iinteractive.comE<gt>
144
145=head1 COPYRIGHT AND LICENSE
146
99c62fb8 147Copyright 2007-2008 by Infinity Interactive, Inc.
190b1c02 148
149L<http://www.iinteractive.com>
150
151This library is free software; you can redistribute it and/or modify
152it under the same terms as Perl itself.
153
154=cut