whitespace clean up
[catagits/Catalyst-Controller-DBIC-API.git] / lib / Catalyst / Controller / DBIC / API / JoinBuilder.pm
CommitLineData
d2739840 1package Catalyst::Controller::DBIC::API::JoinBuilder;
2
3#ABSTRACT: Provides a helper class to automatically keep track of joins in complex searches
4use Moose;
5use MooseX::Types::Moose(':all');
6use Catalyst::Controller::DBIC::API::Types(':all');
7use namespace::autoclean;
8
9=attribute_public parent is: ro, isa: 'Catalyst::Controller::DBIC::API::JoinBuilder'
10
11parent stores the direct ascendant in the datastructure that represents the join
12
13=cut
14
15has parent =>
16(
17 is => 'ro',
18 isa => JoinBuilder,
19 predicate => 'has_parent',
20 weak_ref => 1,
21 trigger => sub { my ($self, $new) = @_; $new->add_child($self); },
22);
23
24=attribute_public children is: ro, isa: ArrayRef['Catalyst::Controller::DBIC::API::JoinBuilder'], traits => ['Array']
25
26children stores the immediate descendants in the datastructure that represents the join.
27
28Handles the following methods:
29
30 all_children => 'elements'
31 has_children => 'count'
32 add_child => 'push'
33
34=cut
35
36has children =>
37(
38 is => 'ro',
39 isa => ArrayRef[JoinBuilder],
40 traits => ['Array'],
41 default => sub { [] },
42 handles =>
43 {
44 all_children => 'elements',
45 has_children => 'count',
46 add_child => 'push',
47 }
48);
49
50=attribute_public joins is: ro, isa: HashRef, lazy_build: true
51
52joins holds the cached generated join datastructure.
53
54=cut
55
56has joins =>
57(
58 is => 'ro',
59 isa => HashRef,
60 lazy_build => 1,
61);
62
63=attribute_public name is: ro, isa: Str, required: 1
64
65Sets the key for this level in the generated hash
66
67=cut
68
69has name =>
70(
71 is => 'ro',
72 isa => Str,
73 required => 1,
74);
75
76=method_private _build_joins
77
78_build_joins finds the top parent in the structure and then recursively iterates the children building out the join datastructure
79
80=cut
81
82sub _build_joins
83{
84 my ($self) = @_;
406086f3 85
d2739840 86 my $parent;
87 while(my $found = $self->parent)
88 {
89 if($found->has_parent)
90 {
91 $self = $found;
92 next;
93 }
94 $parent = $found;
95 }
96
97 my $builder;
98 $builder = sub
99 {
100 my ($node) = @_;
101 my $foo = {};
102 map { $foo->{$_->name} = $builder->($_) } $node->all_children;
103 return $foo;
104 };
105
106 return $builder->($parent || $self);
107}
108
109=head1 DESCRIPTION
110
111JoinBuilder is used to keep track of joins automgically for complex searches. It accomplishes this by building a simple tree of parents and children and then recursively drilling into the tree to produce a useable join attribute for ->search.
112
113=cut
114
1151;