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