use new method names from cmop
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor / Native / String / replace.pm
1 package Moose::Meta::Method::Accessor::Native::String::replace;
2
3 use strict;
4 use warnings;
5
6 use Moose::Util ();
7 use Params::Util ();
8
9 our $VERSION = '1.19';
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 use Moose::Role;
14
15 with 'Moose::Meta::Method::Accessor::Native::Writer' => {
16     -excludes => [
17         qw(
18             _minimum_arguments
19             _maximum_arguments
20             _inline_check_arguments
21             _inline_optimized_set_new_value
22             )
23     ]
24     };
25
26 sub _minimum_arguments { 1 }
27
28 sub _maximum_arguments { 2 }
29
30 sub _inline_check_arguments {
31     my $self = shift;
32
33     return (
34         'if (!Moose::Util::_STRINGLIKE0($_[0]) && !Params::Util::_REGEX($_[0])) {',
35             $self->_inline_throw_error(
36                 '"The first argument passed to replace must be a string or '
37               . 'regexp reference"'
38             ) . ';',
39         '}',
40         'if (!Moose::Util::_STRINGLIKE0($_[1]) && !Params::Util::_CODELIKE($_[1])) {',
41             $self->_inline_throw_error(
42                 '"The second argument passed to replace must be a string or '
43               . 'code reference"'
44             ) . ';',
45         '}',
46     );
47 }
48
49 sub _potential_value {
50     my $self = shift;
51     my ($slot_access) = @_;
52
53     return '(do { '
54              . 'my $val = ' . $slot_access . '; '
55              . 'ref $_[1] '
56                  . '? $val =~ s/$_[0]/$_[1]->()/e '
57                  . ': $val =~ s/$_[0]/$_[1]/; '
58              . '$val; '
59          . '})';
60 }
61
62 sub _inline_optimized_set_new_value {
63     my $self = shift;
64     my ($inv, $new, $slot_access) = @_;
65
66     return (
67         'ref $_[1]',
68             '? ' . $slot_access . ' =~ s/$_[0]/$_[1]->()/e',
69             ': ' . $slot_access . ' =~ s/$_[0]/$_[1]/;',
70      );
71 }
72
73 no Moose::Role;
74
75 1;