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