package MooseX::AttributeHelpers::MethodProvider::String;
use MooseX::AttributeHelpers::MethodProvider;

our $VERSION   = '0.01';
our $AUTHORITY = 'cpan:STEVAN';

add_method_provider String => (
    type => 'Str',
    provides => {
        append => sub {
            my ($attr, $reader, $writer) = @_;
            return sub { $writer->( $_[0],  $reader->($_[0]) . $_[1] ) };
        },

        prepend => sub {
            my ($attr, $reader, $writer) = @_;
            return sub { $writer->( $_[0],  $_[1] . $reader->($_[0]) ) };
        },

        replace => sub {
            my ($attr, $reader, $writer) = @_;
            return sub {
                my ( $self, $regex, $replacement ) = @_;
                my $v = $reader->($_[0]);

                if ( (ref($replacement)||'') eq 'CODE' ) {
                    $v =~ s/$regex/$replacement->()/e;
                } else {
                    $v =~ s/$regex/$replacement/;
                }

                $writer->( $_[0], $v);
            };
        },

        match => sub {
            my ($attr, $reader, $writer) = @_;
            return sub { $reader->($_[0]) =~ $_[1] };
        },

        chop => sub {
            my ($attr, $reader, $writer) = @_;
            return sub {
                my $v = $reader->($_[0]);
                CORE::chop($v);
                $writer->( $_[0], $v);
            };
        },

        chomp => sub {
            my ($attr, $reader, $writer) = @_;
            return sub {
                my $v = $reader->($_[0]);
                chomp($v);
                $writer->( $_[0], $v);
            };
        },

        inc => sub {
            my ($attr, $reader, $writer) = @_;
            return sub {
                my $v = $reader->($_[0]);
                $v++;
                $writer->( $_[0], $v);
            };
        },

        clear => sub {
            my ($attr, $reader, $writer ) = @_;
            return sub { $writer->( $_[0], '' ) }
        },
    },
);

1;

__END__

=pod

=head1 NAME

MooseX::AttributeHelpers::MethodProvider::String

=head1 DESCRIPTION

This is a role which provides the method generators for
L<MooseX::AttributeHelpers::String>.

=head1 PROVIDED METHODS

It should be noted that all methods modify attribute values in place.

=over 4

=item I<inc>

Increments the value stored in this slot using the magical string autoincrement
operator. Note that Perl doesn't provide analogeous behavior in C<-->, so
C<dec> is not available.

=item I<append> C<$string>

Append a string, like C<.=>.

=item I<prepend> C<$string>

Prepend a string.

=item I<replace> C<$pattern> C<$replacement>

Performs a regexp substitution (L<perlop/s>). There is no way to provide the
C<g> flag, but code references will be accepted for the replacement, causing
the regex to be modified with a single C<e>. C</smxi> can be applied using the
C<qr> operator.

=item I<match> C<$pattern>

Like I<replace> but without the replacement. Provided mostly for completeness.

=item C<chop>

L<perlfunc/chop>

=item C<chomp>

L<perlfunc/chomp>

=item C<clear>

Sets the string to the empty string (not the value passed to C<default>).

=back

=head1 BUGS

All complex software has bugs lurking in it, and this module is no
exception. If you find a bug please either email me, or add the bug
to cpan-RT.

=head1 AUTHOR

Stevan Little E<lt>stevan@iinteractive.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright 2007-2008 by Infinity Interactive, Inc.

L<http://www.iinteractive.com>

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut
