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.034002';
3             # ABSTRACT: Convenient subclassing with DBIx::Class
4              
5 55     55   29491 use strict;
  55         110  
  55         2573  
6 55     55   250 use warnings;
  55         135  
  55         1328  
7              
8 55     55   244 use parent 'DBIx::Class::Row';
  55         108  
  55         261  
9              
10 55     55   13757 use DBIx::Class::Helpers::Util qw{get_namespace_parts assert_similar_namespaces};
  55         109  
  55         413  
11 55     55   4938 use DBIx::Class::Candy::Exports;
  55         200  
  55         381  
12              
13             export_methods [qw(subclass generate_relationships set_table)];
14              
15             sub subclass {
16 110     110 1 511510 my $self = shift;
17 110         237 my $namespace = shift;
18 110         865 $self->set_table;
19 110         49337 $self->generate_relationships($namespace);
20             }
21              
22             sub generate_relationships {
23 110     110 1 262 my $self = shift;
24 110         1596 my ($namespace) = get_namespace_parts($self);
25 110         2046 foreach my $rel ($self->relationships) {
26 330         96619 my $rel_info = $self->relationship_info($rel);
27 330         35469 my $class = $rel_info->{class};
28              
29 330         1071 assert_similar_namespaces($self, $class);
30 330         963 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 330         2502 );
38             };
39             }
40              
41             sub set_table {
42 110     110 1 281 my $self = shift;
43 110         1317 $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) 2019 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