File Coverage

blib/lib/Fey/Meta/HasMany/ViaFK.pm
Criterion Covered Total %
statement 37 39 94.8
branch 2 2 100.0
condition n/a
subroutine 8 9 88.8
pod n/a
total 47 50 94.0


line stmt bran cond sub pod time code
1             package Fey::Meta::HasMany::ViaFK;
2              
3 10     10   49 use strict;
  10         18  
  10         345  
4 10     10   45 use warnings;
  10         15  
  10         365  
5 10     10   49 use namespace::autoclean;
  10         17  
  10         94  
6              
7             our $VERSION = '0.47';
8              
9 10     10   1003 use Fey::ORM::Types qw( ArrayRef );
  10         18  
  10         106  
10 10     10   47135 use List::AllUtils qw( any );
  10         21  
  10         692  
11              
12 10     10   54 use Moose;
  10         17  
  10         88  
13 10     10   58802 use MooseX::StrictConstructor;
  10         21  
  10         94  
14              
15             with 'Fey::Meta::Role::Relationship::HasMany',
16             'Fey::Meta::Role::Relationship::ViaFK';
17              
18             has 'order_by' => (
19             is => 'ro',
20             isa => ArrayRef,
21             );
22              
23             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
24             sub _make_iterator_maker {
25 7     7   14 my $self = shift;
26              
27 7         2140 my $target_table = $self->foreign_table();
28              
29 7         1202 my $select = $self->associated_class()->schema_class()->SQLFactoryClass()
30             ->new_select();
31 7         3175 $select->select( $target_table->columns() )->from($target_table);
32              
33 7         64475 my @ph_names;
34              
35 7         294 my $ph = Fey::Placeholder->new();
36 7         205 for my $pair ( @{ $self->fk()->column_pairs() } ) {
  7         354  
37 5         794 my ( $from, $to ) = @{$pair};
  5         14  
38              
39 5         39 $select->where( $to, '=', $ph );
40              
41 5         4524 push @ph_names, $from->name();
42             }
43              
44 5 100       233 $select->order_by( @{ $self->order_by() } )
  1         24  
45             if $self->order_by();
46              
47             my $bind_params_sub = sub {
48 0     0   0 return map { $_[0]->$_() } @ph_names;
  0         0  
49 5         1098 };
50              
51 5         38 return $self->_make_subref_for_sql(
52             $select,
53             $bind_params_sub,
54             );
55             }
56             ## use critic
57              
58             __PACKAGE__->meta()->make_immutable();
59              
60             1;
61              
62             # ABSTRACT: A parent for has-one metaclasses based on a L<Fey::FK> object
63              
64             __END__
65              
66             =pod
67              
68             =head1 NAME
69              
70             Fey::Meta::HasMany::ViaFK - A parent for has-one metaclasses based on a L<Fey::FK> object
71              
72             =head1 VERSION
73              
74             version 0.47
75              
76             =head1 DESCRIPTION
77              
78             This class implements a has-one relationship for a class, based on a
79             provided (or deduced) L<Fey::FK> object.
80              
81             =head1 CONSTRUCTOR OPTIONS
82              
83             This class accepts the following constructor options:
84              
85             =over 4
86              
87             =item * fk
88              
89             If you don't provide this, the class looks for foreign keys between
90             C<< $self->table() >> and and C<< $self->foreign_table() >>. If it
91             finds exactly one, it uses that one.
92              
93             =item * order_by
94              
95             This will be appended to the SQL which is generated to select the
96             foreign rows. It should be an arrayref which can be passed to C<<
97             Fey::SQL::Select->order_by() >>.
98              
99             =item * allows_undef
100              
101             This defaults to true if any of the columns in the local table are
102             NULLable, otherwise it defaults to false.
103              
104             =back
105              
106             =head1 METHODS
107              
108             Besides the methods provided by L<Fey::Meta::Role::Relationship::HasMany> and
109             L<Fey::Meta::Role::Relationship::ViaFK>, this class also provides the
110             following methods:
111              
112             =head2 $ho->fk()
113              
114             Corresponds to the value passed to the constructor, or the calculated
115             default.
116              
117             =head2 $ho->order_by()
118              
119             Corresponds to the value passed to the constructor.
120              
121             =head1 AUTHOR
122              
123             Dave Rolsky <autarch@urth.org>
124              
125             =head1 COPYRIGHT AND LICENSE
126              
127             This software is copyright (c) 2011 - 2015 by Dave Rolsky.
128              
129             This is free software; you can redistribute it and/or modify it under
130             the same terms as the Perl 5 programming language system itself.
131              
132             =cut