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