File Coverage

blib/lib/DBIx/Class/Helper/Schema/GenerateSource.pm
Criterion Covered Total %
statement 22 22 100.0
branch 2 4 50.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 34 36 94.4


line stmt bran cond sub pod time code
1             package DBIx::Class::Helper::Schema::GenerateSource;
2             $DBIx::Class::Helper::Schema::GenerateSource::VERSION = '2.036000';
3             # ABSTRACT: Generate sources directly from your Schema
4              
5 1     1   574 use strict;
  1         2  
  1         25  
6 1     1   4 use warnings;
  1         2  
  1         22  
7              
8 1     1   5 use parent 'DBIx::Class::Schema';
  1         2  
  1         4  
9              
10 1     1   59 use Scalar::Util 'blessed';
  1         2  
  1         185  
11              
12 1 50   1   7 sub _schema_class { blessed($_[0]) || $_[0] }
13              
14             sub _generate_class_name {
15 1     1   4 $_[0]->_schema_class . '::GeneratedResult::__' . uc $_[1]
16             }
17              
18             sub _generate_class {
19 1 50   1   6 die $@ unless eval "
  1     1   3  
  1         3  
  1         59  
20             package $_[1]; use parent '$_[2]'; __PACKAGE__->table(__PACKAGE__->table); 1;
21             ";
22             }
23              
24             sub generate_source {
25 1     1 1 189 my ($self, $moniker, $base) = @_;
26              
27 1         5 my $class = $self->_generate_class_name($moniker);
28 1         6 $self->_generate_class($class, $base);
29 1         14 $self->register_class($moniker, $class);
30             }
31              
32             1;
33              
34             __END__
35              
36             =pod
37              
38             =head1 NAME
39              
40             DBIx::Class::Helper::Schema::GenerateSource - Generate sources directly from your Schema
41              
42             =head1 SYNOPSIS
43              
44             package MyApp::Schema;
45              
46             __PACKAGE__->load_components('Helper::Schema::GenerateSource');
47              
48             __PACKAGE__->generate_source(User => 'MyCompany::BaseResult::User');
49              
50             =head1 DESCRIPTION
51              
52             This helper allows you to handily and correctly add new result sources to your
53             schema based on existing result sources. Typically this would be done with
54             something like:
55              
56             package MyApp::Schema::Result::MessegeQueue;
57              
58             use parent 'MyCo::Schema::Result::MessageQueue';
59              
60             __PACKAGE__->table(__PACKAGE__->table);
61              
62             1;
63              
64             which clearly is in its own file. This should still be done when you need to
65             add columns or really do B<anything> other than just basic addition of the
66             result source to your schema.
67              
68             B<Note>: This component correctly generates an "anonymous" subclass of the given
69             base class. Do not depend on the name of the subclass as it is currently
70             considered unstable.
71              
72             =head1 METHODS
73              
74             =head2 generate_source
75              
76             $schema->generate_source(User => 'MyCompany::BaseResult::User')
77              
78             The first argument to C<generate_source> is the C<moniker> to register the
79             class as, the second argument is the base class for the new result source.
80              
81             =head1 AUTHOR
82              
83             Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
84              
85             =head1 COPYRIGHT AND LICENSE
86              
87             This software is copyright (c) 2020 by Arthur Axel "fREW" Schmidt.
88              
89             This is free software; you can redistribute it and/or modify it under
90             the same terms as the Perl 5 programming language system itself.
91              
92             =cut