Skip to content

Commit 11e469d

Browse files
committed
Convert the m2m helper generation to Sub::Quote (as in 8d73fcd)
This was left for a later time, which is about now, after the multiple passes through the actual (terrible) code Read under -w, should contain zero functional changes
1 parent 6983130 commit 11e469d

File tree

1 file changed

+42
-49
lines changed

1 file changed

+42
-49
lines changed

lib/DBIx/Class/Relationship/ManyToMany.pm

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ use strict;
55
use warnings;
66

77
use DBIx::Class::Carp;
8-
use Sub::Name 'subname';
9-
use Scalar::Util 'blessed';
10-
use DBIx::Class::_Util 'fail_on_internal_wantarray';
8+
use DBIx::Class::_Util qw(fail_on_internal_wantarray quote_sub);
9+
10+
# FIXME - this souldn't be needed
11+
my $cu;
12+
BEGIN { $cu = \&carp_unique }
13+
1114
use namespace::clean;
1215

1316
our %_pod_inherit_config =
@@ -26,10 +29,6 @@ sub many_to_many {
2629
"missing foreign relation in many-to-many"
2730
) unless $f_rel;
2831

29-
{
30-
no strict 'refs';
31-
no warnings 'redefine';
32-
3332
my $add_meth = "add_to_${meth}";
3433
my $remove_meth = "remove_from_${meth}";
3534
my $set_meth = "set_${meth}";
@@ -57,48 +56,45 @@ EOW
5756
}
5857
}
5958

60-
$rel_attrs->{alias} ||= $f_rel;
61-
59+
my $qsub_attrs = {
60+
'$rel_attrs' => \{ alias => $f_rel, %{ $rel_attrs||{} } },
61+
'$carp_unique' => \$cu,
62+
};
6263

63-
my $rs_meth_name = join '::', $class, $rs_meth;
64-
*$rs_meth_name = subname $rs_meth_name, sub {
64+
quote_sub "${class}::${rs_meth}", sprintf( <<'EOC', $rel, $f_rel ), $qsub_attrs;
6565
6666
# this little horror is there replicating a deprecation from
6767
# within search_rs() itself
68-
shift->search_related_rs($rel)
68+
shift->search_related_rs( q{%1$s} )
6969
->search_related_rs(
70-
$f_rel,
70+
q{%2$s},
7171
undef,
7272
( @_ > 1 and ref $_[-1] eq 'HASH' )
73-
? { %$rel_attrs, %{ pop @_ } }
73+
? { %%$rel_attrs, %%{ pop @_ } }
7474
: $rel_attrs
7575
)->search_rs(@_)
7676
;
77-
78-
};
77+
EOC
7978

8079

81-
my $meth_name = join '::', $class, $meth;
82-
*$meth_name = subname $meth_name, sub {
80+
quote_sub "${class}::${meth}", sprintf( <<'EOC', $rs_meth );
8381
84-
DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_WANTARRAY and my $sog = fail_on_internal_wantarray;
82+
DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_WANTARRAY and my $sog = DBIx::Class::_Util::fail_on_internal_wantarray;
8583
86-
my $rs = shift->$rs_meth( @_ );
84+
my $rs = shift->%s( @_ );
8785
8886
wantarray ? $rs->all : $rs;
89-
90-
};
87+
EOC
9188

9289

93-
my $add_meth_name = join '::', $class, $add_meth;
94-
*$add_meth_name = subname $add_meth_name, sub {
90+
quote_sub "${class}::${add_meth}", sprintf( <<'EOC', $add_meth, $rel, $f_rel ), $qsub_attrs;
9591
9692
( @_ >= 2 and @_ <= 3 ) or $_[0]->throw_exception(
97-
"'$add_meth' expects an object or hashref to link to, and an optional hashref of link data"
93+
"'%1$s' expects an object or hashref to link to, and an optional hashref of link data"
9894
);
9995
10096
$_[0]->throw_exception(
101-
"The optional link data supplied to '$add_meth' is not a hashref (it was previously ignored)"
97+
"The optional link data supplied to '%1$s' is not a hashref (it was previously ignored)"
10298
) if $_[2] and ref $_[2] ne 'HASH';
10399
104100
my( $self, $far_obj ) = @_;
@@ -107,44 +103,43 @@ EOW
107103
108104
# the API needs is always expected to return the far object, possibly
109105
# creating it in the process
110-
if( not defined blessed $far_obj ) {
106+
if( not defined Scalar::Util::blessed( $far_obj ) ) {
111107
112108
$guard = $self->result_source->schema->storage->txn_scope_guard;
113109
114110
# reify the hash into an actual object
115111
$far_obj = $self->result_source
116-
->related_source( $rel )
117-
->related_source( $f_rel )
112+
->related_source( q{%2$s} )
113+
->related_source( q{%3$s} )
118114
->resultset
119115
->search_rs( undef, $rel_attrs )
120116
->find_or_create( $far_obj );
121117
}
122118
123119
my $link = $self->new_related(
124-
$rel,
120+
q{%2$s},
125121
$_[2] || {},
126122
);
127123
128-
$link->set_from_related( $f_rel, $far_obj );
124+
$link->set_from_related( q{%3$s}, $far_obj );
129125
130126
$link->insert();
131127
132128
$guard->commit if $guard;
133129
134130
$far_obj;
135-
};
131+
EOC
136132

137133

138-
my $set_meth_name = join '::', $class, $set_meth;
139-
*$set_meth_name = subname $set_meth_name, sub {
134+
quote_sub "${class}::${set_meth}", sprintf( <<'EOC', $set_meth, $add_meth, $rel, $f_rel ), $qsub_attrs;
140135
141136
my $self = shift;
142137
143138
my $set_to = ( ref $_[0] eq 'ARRAY' )
144139
? ( shift @_ )
145140
: do {
146-
carp_unique(
147-
"Calling '$set_meth' with a list of items to link to is deprecated, use an arrayref instead"
141+
$carp_unique->(
142+
"Calling '%1$s' with a list of items to link to is deprecated, use an arrayref instead"
148143
);
149144
150145
# gobble up everything from @_ into a new arrayref
@@ -154,7 +149,7 @@ EOW
154149
155150
# make sure folks are not invoking a bizarre mix of deprecated and curent syntax
156151
$self->throw_exception(
157-
"'$set_meth' expects an arrayref of objects or hashrefs to link to, and an optional hashref of link data"
152+
"'%1$s' expects an arrayref of objects or hashrefs to link to, and an optional hashref of link data"
158153
) if (
159154
@_ > 1
160155
or
@@ -170,35 +165,33 @@ EOW
170165
# if there is a where clause in the attributes, ensure we only delete
171166
# rows that are within the where restriction
172167
$self->search_related(
173-
$rel,
168+
q{%3$s},
174169
( $rel_attrs->{where}
175-
? ( $rel_attrs->{where}, { join => $f_rel } )
170+
? ( $rel_attrs->{where}, { join => q{%4$s} } )
176171
: ()
177172
)
178173
)->delete;
179174
180175
# add in the set rel objects
181-
$self->$add_meth(
176+
$self->%2$s(
182177
$_,
183178
@_, # at this point @_ is either empty or contains a lone link-data hash
184179
) for @$set_to;
185180
186181
$guard->commit if $guard;
187-
};
182+
EOC
188183

189184

190-
my $remove_meth_name = join '::', $class, $remove_meth;
191-
*$remove_meth_name = subname $remove_meth_name, sub {
185+
quote_sub "${class}::${remove_meth}", sprintf( <<'EOC', $remove_meth, $rel, $f_rel );
192186
193-
$_[0]->throw_exception("'$remove_meth' expects an object")
194-
unless defined blessed $_[1];
187+
$_[0]->throw_exception("'%1$s' expects an object")
188+
unless defined Scalar::Util::blessed( $_[1] );
195189
196-
$_[0]->search_related_rs( $rel )
197-
->search_rs( $_[1]->ident_condition( $f_rel ), { join => $f_rel } )
190+
$_[0]->search_related_rs( q{%2$s} )
191+
->search_rs( $_[1]->ident_condition( q{%3$s} ), { join => q{%3$s} } )
198192
->delete;
199-
};
193+
EOC
200194

201-
}
202195
}
203196

204197
1;

0 commit comments

Comments
 (0)