| 1 | package DBIx::Class::Helper::Row::SubClass; | ||
| 2 | 54 | $DBIx::Class::Helper::Row::SubClass::VERSION = '2.032002'; | |
| 3 | # ABSTRACT: Convenient subclassing with DBIx::Class | ||
| 4 | |||
| 5 | 54 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 54 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 54 | ▶ | use parent 'DBIx::Class::Row'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 54 | ▶ | use DBIx::Class::Helpers::Util qw{get_namespace_parts assert_similar_namespaces}; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 54 | ▶ | use DBIx::Class::Candy::Exports; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | 54 | export_methods [qw(subclass generate_relationships set_table)]; | |
| 14 | |||
| 15 | sub subclass { | ||
| 16 | 108 | ▶ | my $self = shift; |
|
Subroutine
subclass: called CC=1 SCAR=0.0
Pod
documented
| |||
| 17 | 108 | my $namespace = shift; | |
| 18 | 108 | $self->set_table; | |
| 19 | 108 | $self->generate_relationships($namespace); | |
| 20 | } | ||
| 21 | |||
| 22 | sub generate_relationships { | ||
| 23 | 108 | ▶ | my $self = shift; |
|
Subroutine
generate_relationships: called CC=2 SCAR=6.9
Pod
documented
| |||
| 24 | 108 | my ($namespace) = get_namespace_parts($self); | |
| 25 | 108 | foreach my $rel ($self->relationships) { | |
| 26 | 216 | my $rel_info = $self->relationship_info($rel); | |
| 27 | 216 | my $class = $rel_info->{class}; | |
| 28 | |||
| 29 | 216 | assert_similar_namespaces($self, $class); | |
| 30 | 216 | my (undef, $result) = get_namespace_parts($class); | |
| 31 | |||
| 32 | $self->add_relationship( | ||
| 33 | $rel, | ||
| 34 | "${namespace}::$result", | ||
| 35 | $rel_info->{cond}, | ||
| 36 | $rel_info->{attrs} | ||
| 37 | 216 | ); | |
| 38 | }; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub set_table { | ||
| 42 | 108 | ▶ | my $self = shift; |
|
Subroutine
set_table: called CC=1 SCAR=0.0
Pod
documented
| |||
| 43 | 108 | $self->table($self->table); | |
| 44 | } | ||
| 45 | |||
| 46 | 54 | 1; | |
| 47 | |||
| 48 | __END__ | ||
| 49 | |||
| 50 | =pod | ||
| 51 | |||
| 52 | =head1 NAME | ||
| 53 | |||
| 54 | DBIx::Class::Helper::Row::SubClass - Convenient subclassing with DBIx::Class | ||
| 55 | |||
| 56 | =head1 SYNOPSIS | ||
| 57 | |||
| 58 | # define parent class | ||
| 59 | package ParentSchema::Result::Bar; | ||
| 60 | |||
| 61 | use strict; | ||
| 62 | use warnings; | ||
| 63 | |||
| 64 | use parent 'DBIx::Class'; | ||
| 65 | |||
| 66 | __PACKAGE__->load_components('Core'); | ||
| 67 | |||
| 68 | __PACKAGE__->table('Bar'); | ||
| 69 | |||
| 70 | __PACKAGE__->add_columns(qw/ id foo_id /); | ||
| 71 | |||
| 72 | __PACKAGE__->set_primary_key('id'); | ||
| 73 | |||
| 74 | __PACKAGE__->belongs_to( foo => 'ParentSchema::Result::Foo', 'foo_id' ); | ||
| 75 | |||
| 76 | # define subclass | ||
| 77 | package MySchema::Result::Bar; | ||
| 78 | |||
| 79 | use strict; | ||
| 80 | use warnings; | ||
| 81 | |||
| 82 | use parent 'ParentSchema::Result::Bar'; | ||
| 83 | |||
| 84 | __PACKAGE__->load_components(qw{Helper::Row::SubClass Core}); | ||
| 85 | |||
| 86 | __PACKAGE__->subclass; | ||
| 87 | |||
| 88 | or with L<DBIx::Class::Candy>: | ||
| 89 | |||
| 90 | # define subclass | ||
| 91 | package MySchema::Result::Bar; | ||
| 92 | |||
| 93 | use DBIx::Class::Candy | ||
| 94 | -base => 'ParentSchema::Result::Bar', | ||
| 95 | -components => ['Helper::Row::SubClass']; | ||
| 96 | |||
| 97 | subclass; | ||
| 98 | |||
| 99 | =head1 DESCRIPTION | ||
| 100 | |||
| 101 | This component is to allow simple subclassing of L<DBIx::Class> Result classes. | ||
| 102 | |||
| 103 | =head1 METHODS | ||
| 104 | |||
| 105 | =head2 subclass | ||
| 106 | |||
| 107 | This is probably the method you want. You call this in your child class and it | ||
| 108 | imports the definitions from the parent into itself. | ||
| 109 | |||
| 110 | =head2 generate_relationships | ||
| 111 | |||
| 112 | This is where the cool stuff happens. This assumes that the namespace is laid | ||
| 113 | out in the recommended C<MyApp::Schema::Result::Foo> format. If the parent has | ||
| 114 | C<Parent::Schema::Result::Foo> related to C<Parent::Schema::Result::Bar>, and you | ||
| 115 | inherit from C<Parent::Schema::Result::Foo> in C<MyApp::Schema::Result::Foo>, you | ||
| 116 | will automatically get the relationship to C<MyApp::Schema::Result::Bar>. | ||
| 117 | |||
| 118 | =head2 set_table | ||
| 119 | |||
| 120 | This is a super basic method that just sets the current classes' table to the | ||
| 121 | parent classes' table. | ||
| 122 | |||
| 123 | =head1 CANDY EXPORTS | ||
| 124 | |||
| 125 | If used in conjunction with L<DBIx::Class::Candy> this component will export: | ||
| 126 | |||
| 127 | =over | ||
| 128 | |||
| 129 | =item join_table | ||
| 130 | |||
| 131 | =item subclass | ||
| 132 | |||
| 133 | =item generate_relationships | ||
| 134 | |||
| 135 | =item set_table | ||
| 136 | |||
| 137 | =back | ||
| 138 | |||
| 139 | =head1 NOTE | ||
| 140 | |||
| 141 | This Component is mostly aimed at those who want to subclass parts of a schema, | ||
| 142 | maybe for sharing a login system in a few different projects. Do not confuse | ||
| 143 | it with L<DBIx::Class::DynamicSubclass>, which solves an entirely different | ||
| 144 | problem. DBIx::Class::DynamicSubclass is for when you want to store a few very | ||
| 145 | similar classes in the same table (Employee, Person, Boss, etc) whereas this | ||
| 146 | component is merely for reusing an existing schema. | ||
| 147 | |||
| 148 | =head1 AUTHOR | ||
| 149 | |||
| 150 | Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com> | ||
| 151 | |||
| 152 | =head1 COPYRIGHT AND LICENSE | ||
| 153 | |||
| 154 | This software is copyright (c) 2016 by Arthur Axel "fREW" Schmidt. | ||
| 155 | |||
| 156 | This is free software; you can redistribute it and/or modify it under | ||
| 157 | the same terms as the Perl 5 programming language system itself. | ||
| 158 | |||
| 159 | =cut | ||