Refactored native trait accessors so they are done entirely in roles.
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor / Native / String / replace.pm
CommitLineData
e7724627 1package Moose::Meta::Method::Accessor::Native::String::replace;
2
3use strict;
4use warnings;
5
10bd99ec 6our $VERSION = '1.14';
e7724627 7$VERSION = eval $VERSION;
8our $AUTHORITY = 'cpan:STEVAN';
9
8b9641b8 10use Moose::Role;
11
12with 'Moose::Meta::Method::Accessor::Native::Writer' => {
13 -excludes => [
14 qw(
15 _minimum_arguments
16 _maximum_arguments
17 _inline_check_arguments
18 _inline_optimized_set_new_value
19 )
20 ]
21};
e7724627 22
23sub _minimum_arguments { 1 }
8b9641b8 24
e7724627 25sub _maximum_arguments { 2 }
26
27sub _inline_check_arguments {
28 my $self = shift;
29
30 return $self->_inline_throw_error(
31 q{'The first argument passed to replace must be a string or regexp reference'}
32 )
33 . q{ unless ! ref $_[0] || ref $_[0] eq 'Regexp';} . "\n"
34 . $self->_inline_throw_error(
35 q{'The second argument passed to replace must be a string or code reference'}
36 ) . q{ unless ! ref $_[1] || ref $_[1] eq 'CODE';};
37}
38
39sub _potential_value {
40 my ( $self, $slot_access ) = @_;
41
42 return "( do { my \$val = $slot_access; ref \$_[1] ? \$val =~ s/\$_[0]/\$_[1]->()/e : \$val =~ s/\$_[0]/\$_[1]/; \$val } )";
43}
44
e32b7489 45sub _inline_optimized_set_new_value {
46 my ( $self, $inv, $new, $slot_access ) = @_;
e7724627 47
48 return "if ( ref \$_[1] ) { $slot_access =~ s/\$_[0]/\$_[1]->()/e; } else { $slot_access =~ s/\$_[0]/\$_[1]/; }";
49}
50
8b9641b8 51no Moose::Role;
52
e7724627 531;