join argument validation
[dbsrgits/DBIx-Class-ParameterizedJoinHack.git] / lib / DBIx / Class / ResultSet / ParameterizedJoinHack.pm
CommitLineData
638c1533 1package DBIx::Class::ResultSet::ParameterizedJoinHack;
2
3use strict;
4use warnings;
5use DBIx::Class::ParameterizedJoinHack;
6use base qw(DBIx::Class::ResultSet);
7
8sub _parameterized_join_store {
9 $_[0]->result_source->result_class
10 ->$DBIx::Class::ParameterizedJoinHack::STORE
11}
12
13sub with_parameterized_join {
14 my ($self, $rel, $params) = @_;
598b28ef 15 die "Missing relation name in with_parameterized_join"
16 unless defined $rel;
17 {
18 my $params_ref = ref($params);
19 $params_ref = 'non-reference-value'
20 unless $params_ref;
21 die "Parameters value must be a hash ref, not ${params_ref}"
22 unless $params_ref eq 'HASH';
23 }
638c1533 24 $self->search_rs(
25 {},
26 { join => $rel,
27 join_parameters => {
28 %{$self->{attrs}{join_parameters}||{}},
29 $rel => $params
30 }
31 },
32 );
33}
34
ddc15dd9 35sub _localize_parameters {
36 my ($self, $final, $params, $store, $first, @rest) = @_;
37 return $final->() unless $first;
38 local $store->{$first}{params} = $params->{$first};
39 $self->_localize_parameters($final, $params, $store, @rest);
40}
41
638c1533 42sub call_with_parameters {
43 my ($self, $method, @args) = @_;
44 my %params = %{$self->{attrs}{join_parameters}||{}};
45 my $store = $self->_parameterized_join_store;
ddc15dd9 46 return $self->_localize_parameters(
47 sub { $self->$method(@args) },
48 \%params, $store,
49 keys %params
50 );
638c1533 51}
52
53sub _resolved_attrs { my $self = shift; $self->call_with_parameters($self->next::can, @_) }
54sub related_resultset { my $self = shift; $self->call_with_parameters($self->next::can, @_) }
55
561;
57
58=head1 NAME
59
60DBIx::Class::ResultSet::ParameterizedJoinHack
61
62=head1 SYNOPSIS
63
64 package MySchema::ResultSet::Person;
65 use base qw(DBIx::Class::ResultSet);
66
67 __PACKAGE__->load_components(qw(ResultSet::ParameterizedJoinHack));
68
69 1;
70
71=head1 DESCRIPTION
72
73This is a ResultSet component allowing you to access the dynamically
74parameterized relations declared with
75L<DBIx::Class::ParameterizedJoinHack>.
76
77Enable the component as usual with:
78
79 __PACKAGE__->load_components(qw( ResultSet::ParameterizedJoinHack ));
80
81in your ResultSet class.
82
83See L<DBIx::Class::ParameterizedJoinHack> for declaration documentation,
84a general overview, and examples.
85
86=head1 METHODS
87
88=head2 with_parameterized_join
89
90 my $joined_rs = $resultset->with_parameterized_join(
91 $relation_name,
92 $parameters,
93 );
94
95This method constructs a ResultSet joined with the given C<$relation_name>
96by the passed C<$parameters>. The C<$relation_name> is the name as
97declared on the Result, C<$parameters> is a hash reference with the keys
98being the parameter names, and the values being the arguments to the join
99builder.
100
101=head1 SPONSORS
102
103Development of this module was sponsored by
104
105=over
106
107=item * Ctrl O L<http://ctrlo.com>
108
109=back
110
111=head1 AUTHOR
112
113 Matt S. Trout <mst@shadowcat.co.uk>
114
115=head1 CONTRIBUTORS
116
117None yet.
118
119=head1 COPYRIGHT
120
121Copyright (c) 2015 the DBIx::Class::ParameterizedJoinHack L</AUTHOR> and L</CONTRIBUTORS>
122as listed above.
123
124=head1 LICENSE
125
126This library is free software and may be distributed under the same terms
127as perl itself.
128
129=cut