File Coverage

blib/lib/DBIx/Class/Helper/Row/SubClass.pm
Criterion Covered Total %
statement 29 29 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod 3 3 100.0
total 40 40 100.0


line stmt bran cond sub pod time code
1             package DBIx::Class::Helper::Row::SubClass;
2             $DBIx::Class::Helper::Row::SubClass::VERSION = '2.035000';
3             # ABSTRACT: Convenient subclassing with DBIx::Class
4              
5 56     56   35648 use strict;
  56         147  
  56         1755  
6 56     56   324 use warnings;
  56         118  
  56         1615  
7              
8 56     56   354 use parent 'DBIx::Class::Row';
  56         124  
  56         350  
9              
10 56     56   15776 use DBIx::Class::Helpers::Util qw{get_namespace_parts assert_similar_namespaces};
  56         174  
  56         533  
11 56     56   5905 use DBIx::Class::Candy::Exports;
  56         321  
  56         1785  
12              
13             export_methods [qw(subclass generate_relationships set_table)];
14              
15             sub subclass {
16 112     112 1 635553 my $self = shift;
17 112         267 my $namespace = shift;
18 112         651 $self->set_table;
19 112         59968 $self->generate_relationships($namespace);
20             }
21              
22             sub generate_relationships {
23 112     112 1 294 my $self = shift;
24 112         435 my ($namespace) = get_namespace_parts($self);
25 112         2380 foreach my $rel ($self->relationships) {
26 336         115299 my $rel_info = $self->relationship_info($rel);
27 336         43818 my $class = $rel_info->{class};
28              
29 336         1261 assert_similar_namespaces($self, $class);
30 336         2523 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 336         2880 );
38             };
39             }
40              
41             sub set_table {
42 112     112 1 301 my $self = shift;
43 112         1526 $self->table($self->table);
44             }
45              
46             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) 2020 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