Commit | Line | Data |
638c1533 |
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) = @_; |
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 |
37 | sub _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 |
44 | sub 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 | |
55 | sub _resolved_attrs { my $self = shift; $self->call_with_parameters($self->next::can, @_) } |
56 | sub related_resultset { my $self = shift; $self->call_with_parameters($self->next::can, @_) } |
57 | |
58 | 1; |
59 | |
60 | =head1 NAME |
61 | |
62 | DBIx::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 | |
75 | This is a ResultSet component allowing you to access the dynamically |
76 | parameterized relations declared with |
77 | L<DBIx::Class::ParameterizedJoinHack>. |
78 | |
79 | Enable the component as usual with: |
80 | |
81 | __PACKAGE__->load_components(qw( ResultSet::ParameterizedJoinHack )); |
82 | |
83 | in your ResultSet class. |
84 | |
85 | See L<DBIx::Class::ParameterizedJoinHack> for declaration documentation, |
86 | a 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 | |
97 | This method constructs a ResultSet joined with the given C<$relation_name> |
98 | by the passed C<$parameters>. The C<$relation_name> is the name as |
99 | declared on the Result, C<$parameters> is a hash reference with the keys |
100 | being the parameter names, and the values being the arguments to the join |
101 | builder. |
102 | |
103 | =head1 SPONSORS |
104 | |
105 | Development 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 | |
119 | None yet. |
120 | |
121 | =head1 COPYRIGHT |
122 | |
123 | Copyright (c) 2015 the DBIx::Class::ParameterizedJoinHack L</AUTHOR> and L</CONTRIBUTORS> |
124 | as listed above. |
125 | |
126 | =head1 LICENSE |
127 | |
128 | This library is free software and may be distributed under the same terms |
129 | as perl itself. |
130 | |
131 | =cut |