Alzabo
view release on metacpan or search on metacpan
lib/Alzabo/Create/ForeignKey.pm view on Meta::CPAN
package Alzabo::Create::ForeignKey;
use strict;
use vars qw($VERSION);
use Alzabo::Create;
use Alzabo::Exceptions ( abbr => 'params_exception' );
use Alzabo::Utils;
use Params::Validate qw( :all );
Params::Validate::validation_options
( on_fail => sub { params_exception join '', @_ } );
use base qw(Alzabo::ForeignKey);
$VERSION = 2.0;
1;
sub new
{
my $proto = shift;
my $class = ref $proto || $proto;
validate( @_, { columns_from => { type => ARRAYREF | OBJECT },
columns_to => { type => ARRAYREF | OBJECT },
cardinality => { type => ARRAYREF },
from_is_dependent => { type => SCALAR },
to_is_dependent => { type => SCALAR },
comment => { type => UNDEF | SCALAR,
default => '' },
} );
my %p = @_;
my $self = bless {}, $class;
$self->set_columns_from( $p{columns_from} );
$self->set_columns_to( $p{columns_to} );
$self->set_cardinality( @{ $p{cardinality} } );
$self->set_from_is_dependent( $p{from_is_dependent} );
$self->set_to_is_dependent( $p{to_is_dependent} );
$self->set_comment( $p{comment} );
return $self;
}
sub set_columns_from
{
my $self = shift;
my $c = Alzabo::Utils::is_arrayref( $_[0] ) ? shift : [ shift ];
validate_pos( @$c, ( { isa => 'Alzabo::Create::Column' } ) x @$c );
if ( exists $self->{columns_to} )
{
params_exception
"The number of columns in each part of the relationship must be the same"
unless @{ $self->{columns_to} } == @$c;
}
$self->{columns_from} = $c;
}
sub set_columns_to
{
my $self = shift;
my $c = Alzabo::Utils::is_arrayref( $_[0] ) ? shift : [ shift ];
validate_pos( @$c, ( { isa => 'Alzabo::Create::Column' } ) x @$c );
if ( exists $self->{columns_from} )
{
params_exception
"The number of columns in each part of the relationship must be the same"
unless @{ $self->{columns_from} } == @$c;
}
$self->{columns_to} = $c;
}
sub set_cardinality
{
my $self = shift;
my @card = @_;
params_exception "Incorrect number of elements for cardinality"
unless scalar @card == 2;
foreach my $c ( @card )
{
params_exception "Invalid cardinality piece: $c"
unless $c =~ /^[1n]$/i;
}
params_exception "Invalid cardinality: $card[0]..$card[1]"
if $card[0] eq 'n' && $card[1] eq 'n';
$self->{cardinality} = \@card;
}
sub set_from_is_dependent
{
my $self = shift;
$self->{from_is_dependent} = shift;
}
sub set_to_is_dependent
{
my $self = shift;
$self->{to_is_dependent} = shift;
}
sub set_comment { $_[0]->{comment} = defined $_[1] ? $_[1] : '' }
__END__
=head1 NAME
Alzabo::Create::ForeignKey - Foreign key objects for schema creation.
=head1 SYNOPSIS
use Alzabo::Create::ForeignKey;
=for pod_merge DESCRIPTION
( run in 0.721 second using v1.01-cache-2.11-cpan-39bf76dae61 )