error if we have a lazy attr with no default or builder
[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
88e88a7b 6use Moose::Util ();
7use Params::Util ();
8
245478d5 9our $VERSION = '1.19';
e7724627 10$VERSION = eval $VERSION;
11our $AUTHORITY = 'cpan:STEVAN';
12
8b9641b8 13use Moose::Role;
14
15with 'Moose::Meta::Method::Accessor::Native::Writer' => {
16 -excludes => [
17 qw(
18 _minimum_arguments
19 _maximum_arguments
20 _inline_check_arguments
53a4677c 21 _optimized_set_new_value
8b9641b8 22 )
23 ]
7f5ec80d 24 };
e7724627 25
26sub _minimum_arguments { 1 }
8b9641b8 27
e7724627 28sub _maximum_arguments { 2 }
29
30sub _inline_check_arguments {
31 my $self = shift;
32
53a4677c 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 );
e7724627 47}
48
49sub _potential_value {
53a4677c 50 my $self = shift;
51 my ($slot_access) = @_;
e7724627 52
53a4677c 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 . '})';
e7724627 60}
61
53a4677c 62sub _optimized_set_new_value {
63 my $self = shift;
64 my ($inv, $new, $slot_access) = @_;
e7724627 65
53a4677c 66 return '(do { '
67 . 'ref $_[1] '
68 . '? ' . $slot_access . ' =~ s/$_[0]/$_[1]->()/e '
69 . ': ' . $slot_access . ' =~ s/$_[0]/$_[1]/; '
70 . '})';
e7724627 71}
72
8b9641b8 73no Moose::Role;
74
e7724627 751;