Fey::Meta::HasOne::ViaFK - A parent for has-one metaclasses based on a L<Fey::FK> object


Fey-ORM documentation Contained in the Fey-ORM distribution.

Index


Code Index:

NAME

Top

Fey::Meta::HasOne::ViaFK - A parent for has-one metaclasses based on a Fey::FK object

VERSION

Top

version 0.43

DESCRIPTION

Top

This class implements a has-one relationship for a class, based on a provided (or deduced) Fey::FK object.

CONSTRUCTOR OPTIONS

Top

This class accepts the following constructor options:

* fk

If you don't provide this, the class looks for foreign keys between $self->table() and and $self->foreign_table(). If it finds exactly one, it uses that one.

* allows_undef

This defaults to true if any of the columns in the local table are NULLable, otherwise it defaults to false.

METHODS

Top

Besides the methods inherited from Fey::Meta::HasOne, it also provides the following methods:

$ho->fk()

Corresponds to the value passed to the constructor, or the calculated default.

AUTHOR

Top

Dave Rolsky <autarch@urth.org>

COPYRIGHT AND LICENSE

Top


Fey-ORM documentation Contained in the Fey-ORM distribution.

package Fey::Meta::HasOne::ViaFK;
BEGIN {
  $Fey::Meta::HasOne::ViaFK::VERSION = '0.43';
}

use strict;
use warnings;
use namespace::autoclean;

use List::AllUtils qw( any );

use Moose;
use MooseX::StrictConstructor;

with 'Fey::Meta::Role::Relationship::HasOne',
    'Fey::Meta::Role::Relationship::ViaFK';

sub _build_allows_undef {
    my $self = shift;

    return any { $_->is_nullable() } @{ $self->fk()->source_columns() };
}

sub _make_subref {
    my $self = shift;

    my %column_map;
    for my $pair ( @{ $self->fk()->column_pairs() } ) {
        my ( $from, $to ) = @{$pair};

        $column_map{ $from->name() } = [ $to->name(), $to->is_nullable() ];
    }

    my $target_table = $self->foreign_table();

    return sub {
        my $self = shift;

        my %new_p;

        for my $from ( keys %column_map ) {
            my $target_name = $column_map{$from}[0];

            $new_p{$target_name} = $self->$from();

            return
                unless defined $new_p{$target_name} || $column_map{$from}[1];
        }

        return $self->meta()->ClassForTable($target_table)->new(%new_p);
    };
}

__PACKAGE__->meta()->make_immutable();

1;

# ABSTRACT: A parent for has-one metaclasses based on a L<Fey::FK> object




__END__