DBIx-Class-Schema-Loader

 view release on metacpan or  search on metacpan

lib/DBIx/Class/Schema/Loader/RelBuilder.pm  view on Meta::CPAN

            another_uniq_constraint_name => [ 'col1', 'col2' ],
        ],
    ],

This method will return the generated relationships as a hashref keyed on the
class names.  The values are arrayrefs of hashes containing method name and
arguments, like so:

    {
        'Some::Source::Class' => [
            { method => 'belongs_to', arguments => [
              'col1', 'Another::Source::Class' ] },
            { method => 'has_many', arguments => [
              'anothers', 'Yet::Another::Source::Class', 'col15' ] },
        ],
        'Another::Source::Class' => [
            # ...
        ],
        # ...
    }

=cut

__PACKAGE__->mk_group_accessors('simple', qw/
    loader
    schema
    inflect_plural
    inflect_singular
    relationship_attrs
    rel_collision_map
    rel_name_map
    allow_extra_m2m_cols
    _temp_classes
    __tagger
/);

sub new {
    my ($class, $loader) = @_;

    # from old POD about this constructor:
    # C<$schema_class> should be a schema class name, where the source
    # classes have already been set up and registered.  Column info,
    # primary key, and unique constraints will be drawn from this
    # schema for all of the existing source monikers.

    # Options inflect_plural and inflect_singular are optional, and
    # are better documented in L<DBIx::Class::Schema::Loader::Base>.

    my $self = {
        loader             => $loader,
        (map { $_ => $loader->$_ } qw(
            schema inflect_plural inflect_singular
            relationship_attrs rel_collision_map
            rel_name_map allow_extra_m2m_cols
        )),
        _temp_classes      => [],
    };

    weaken $self->{loader}; #< don't leak

    bless $self => $class;

    # validate the relationship_attrs arg
    if( defined $self->relationship_attrs ) {
        (ref $self->relationship_attrs eq 'HASH' || ref $self->relationship_attrs eq 'CODE')
            or croak "relationship_attrs must be a hashref or coderef";
    }

    return $self;
}


# pluralize a relationship name
sub _inflect_plural {
    my ($self, $relname) = @_;

    return '' if !defined $relname || $relname eq '';

    my $result;
    my $mapped = 0;

    if( ref $self->inflect_plural eq 'HASH' ) {
        if (exists $self->inflect_plural->{$relname}) {
            $result = $self->inflect_plural->{$relname};
            $mapped = 1;
        }
    }
    elsif( ref $self->inflect_plural eq 'CODE' ) {
        my $inflected = $self->inflect_plural->($relname);
        if ($inflected) {
            $result = $inflected;
            $mapped = 1;
        }
    }

    return ($result, $mapped) if $mapped;

    return ($self->_to_PL($relname), 0);
}

# Singularize a relationship name
sub _inflect_singular {
    my ($self, $relname) = @_;

    return '' if !defined $relname || $relname eq '';

    my $result;
    my $mapped = 0;

    if( ref $self->inflect_singular eq 'HASH' ) {
        if (exists $self->inflect_singular->{$relname}) {
            $result = $self->inflect_singular->{$relname};
            $mapped = 1;
        }
    }
    elsif( ref $self->inflect_singular eq 'CODE' ) {
        my $inflected = $self->inflect_singular->($relname);
        if ($inflected) {
            $result = $inflected;
            $mapped = 1;
        }

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.430 second using v1.00-cache-2.02-grep-82fe00e-cpan-dad7e4baca0 )