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