more tests
[dbsrgits/DBIx-Class-ParameterizedJoinHack.git] / lib / DBIx / Class / ResultSet / ParameterizedJoinHack.pm
1 package DBIx::Class::ResultSet::ParameterizedJoinHack;
2
3 use strict;
4 use warnings;
5 use DBIx::Class::ParameterizedJoinHack;
6 use base qw(DBIx::Class::ResultSet);
7
8 sub _parameterized_join_store {
9   $_[0]->result_source->result_class
10        ->$DBIx::Class::ParameterizedJoinHack::STORE
11 }
12
13 sub 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
26 sub _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
33 sub call_with_parameters {
34   my ($self, $method, @args) = @_;
35   my %params = %{$self->{attrs}{join_parameters}||{}};
36   my $store = $self->_parameterized_join_store;
37   return $self->_localize_parameters(
38     sub { $self->$method(@args) },
39     \%params, $store,
40     keys %params
41   );
42 }
43
44 sub _resolved_attrs { my $self = shift; $self->call_with_parameters($self->next::can, @_) }
45 sub related_resultset { my $self = shift; $self->call_with_parameters($self->next::can, @_) }
46
47 1;
48
49 =head1 NAME
50
51 DBIx::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
64 This is a ResultSet component allowing you to access the dynamically
65 parameterized relations declared with
66 L<DBIx::Class::ParameterizedJoinHack>.
67
68 Enable the component as usual with:
69     
70     __PACKAGE__->load_components(qw( ResultSet::ParameterizedJoinHack ));
71
72 in your ResultSet class.
73
74 See L<DBIx::Class::ParameterizedJoinHack> for declaration documentation,
75 a 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
86 This method constructs a ResultSet joined with the given C<$relation_name>
87 by the passed C<$parameters>. The C<$relation_name> is the name as
88 declared on the Result, C<$parameters> is a hash reference with the keys
89 being the parameter names, and the values being the arguments to the join
90 builder.
91
92 =head1 SPONSORS
93
94 Development 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
108 None yet.
109
110 =head1 COPYRIGHT
111
112 Copyright (c) 2015 the DBIx::Class::ParameterizedJoinHack L</AUTHOR> and L</CONTRIBUTORS>
113 as listed above.
114
115 =head1 LICENSE
116
117 This library is free software and may be distributed under the same terms
118 as perl itself.
119
120 =cut