| 1 | package Fey::Meta::HasOne::ViaFK; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 10 | $Fey::Meta::HasOne::ViaFK::VERSION = '0.46'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use List::AllUtils qw( any ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 10 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 10 | ▶ | use MooseX::StrictConstructor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 10 | with 'Fey::Meta::Role::Relationship::HasOne', | |||||||||||||||||||||||||||
| 16 | 'Fey::Meta::Role::Relationship::ViaFK'; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sub _build_allows_undef { | ||||||||||||||||||||||||||||
| 19 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_allows_undef: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 7 | ▶ | return any { $_->is_nullable() } @{ $self->fk()->source_columns() }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | sub _make_subref { | ||||||||||||||||||||||||||||
| 25 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_make_subref: called CC=2 SCAR=10.1
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 7 | my %column_map; | |||||||||||||||||||||||||||
| 28 | 7 | for my $pair ( @{ $self->fk()->column_pairs() } ) { | |||||||||||||||||||||||||||
| 29 | 7 | my ( $from, $to ) = @{$pair}; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 7 | $column_map{ $from->name() } = [ $to->name(), $to->is_nullable() ]; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 7 | my $target_table = $self->foreign_table(); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | return sub { | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | my %new_p; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | for my $from ( keys %column_map ) { | |||||||||||||||||||||||||||
| 42 | 0 | my $target_name = $column_map{$from}[0]; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | $new_p{$target_name} = $self->$from(); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | return | ||||||||||||||||||||||||||||
| 47 | 0 | ▶ | unless defined $new_p{$target_name} || $column_map{$from}[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $new_p{$target_name} or $column_map{$from}[1]
MC/DC0%0/2
defined $new_p{$target_name} or $column_map{$from}[1]
defined $new_p{$target_name}
$column_map{$from}[1]
Truth table0%0/3
A or B
A= defined $new_p{$target_name}B= $column_map{$from}[1]
| |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | return $self->meta()->ClassForTable($target_table)->new(%new_p); | |||||||||||||||||||||||||||
| 51 | 7 | }; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 10 | __PACKAGE__->meta()->make_immutable(); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 10 | 1; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # ABSTRACT: A parent for has-one metaclasses based on a L<Fey::FK> object | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | __END__ | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =pod | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =head1 NAME | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | Fey::Meta::HasOne::ViaFK - A parent for has-one metaclasses based on a L<Fey::FK> object | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head1 VERSION | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | version 0.46 | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | This class implements a has-one relationship for a class, based on a | ||||||||||||||||||||||||||||
| 75 | provided (or deduced) L<Fey::FK> object. | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =head1 CONSTRUCTOR OPTIONS | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | This class accepts the following constructor options: | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =over 4 | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =item * fk | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | If you don't provide this, the class looks for foreign keys between | ||||||||||||||||||||||||||||
| 86 | C<< $self->table() >> and and C<< $self->foreign_table() >>. If it | ||||||||||||||||||||||||||||
| 87 | finds exactly one, it uses that one. | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =item * allows_undef | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | This defaults to true if any of the columns in the local table are | ||||||||||||||||||||||||||||
| 92 | NULLable, otherwise it defaults to false. | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =back | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head1 METHODS | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | Besides the methods provided by L<Fey::Meta::Role::Relationship::HasOne> and | ||||||||||||||||||||||||||||
| 99 | L<Fey::Meta::Role::Relationship::ViaFK>, this class also provides the | ||||||||||||||||||||||||||||
| 100 | following methods: | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head2 $ho->fk() | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | Corresponds to the value passed to the constructor, or the calculated | ||||||||||||||||||||||||||||
| 105 | default. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | Dave Rolsky <autarch@urth.org> | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | This software is copyright (c) 2011 by Dave Rolsky. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 116 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =cut | ||||||||||||||||||||||||||||