Use dzil Authority plugin - remove $AUTHORITY from code
[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 use Moose::Role;
10
11 with 'Moose::Meta::Method::Accessor::Native::Writer' => {
12     -excludes => [
13         qw(
14             _minimum_arguments
15             _maximum_arguments
16             _inline_check_arguments
17             _inline_optimized_set_new_value
18             )
19     ]
20     };
21
22 sub _minimum_arguments { 1 }
23
24 sub _maximum_arguments { 2 }
25
26 sub _inline_check_arguments {
27     my $self = shift;
28
29     return (
30         'if (!Moose::Util::_STRINGLIKE0($_[0]) && !Params::Util::_REGEX($_[0])) {',
31             $self->_inline_throw_error(
32                 '"The first argument passed to replace must be a string or '
33               . 'regexp reference"'
34             ) . ';',
35         '}',
36         'if (!Moose::Util::_STRINGLIKE0($_[1]) && !Params::Util::_CODELIKE($_[1])) {',
37             $self->_inline_throw_error(
38                 '"The second argument passed to replace must be a string or '
39               . 'code reference"'
40             ) . ';',
41         '}',
42     );
43 }
44
45 sub _potential_value {
46     my $self = shift;
47     my ($slot_access) = @_;
48
49     return '(do { '
50              . 'my $val = ' . $slot_access . '; '
51              . 'ref $_[1] '
52                  . '? $val =~ s/$_[0]/$_[1]->()/e '
53                  . ': $val =~ s/$_[0]/$_[1]/; '
54              . '$val; '
55          . '})';
56 }
57
58 sub _inline_optimized_set_new_value {
59     my $self = shift;
60     my ($inv, $new, $slot_access) = @_;
61
62     return (
63         'ref $_[1]',
64             '? ' . $slot_access . ' =~ s/$_[0]/$_[1]->()/e',
65             ': ' . $slot_access . ' =~ s/$_[0]/$_[1]/;',
66      );
67 }
68
69 no Moose::Role;
70
71 1;