File Coverage

blib/lib/DBIx/Class/AuditAny/Util/ResultMaker.pm
Criterion Covered Total %
statement 30 30 100.0
branch 3 6 50.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 40 43 93.0


line stmt bran cond sub pod time code
1             package DBIx::Class::AuditAny::Util::ResultMaker;
2 11     11   80 use strict;
  11         29  
  11         344  
3 11     11   66 use warnings;
  11         31  
  11         310  
4              
5             # ABSTRACT: On-the-fly creation of DBIC Result classes
6              
7             =head1 NAME
8              
9             DBIx::Class::AuditAny::Util::ResultMaker - On-the-fly creation of DBIC Result classes
10              
11             =head1 DESCRIPTION
12              
13             This package provides an easy way to conjurer new DBIC result classes into existence. It
14             is typically used by L<DBIx::Class::AuditAny::Util::SchemaMaker> and not called directly.
15              
16             =head1 ATTRIBUTES
17              
18             =cut
19              
20 11     11   61 use Moo;
  11         35  
  11         71  
21 11     11   14600 use MooX::Types::MooseLike::Base qw(:all);
  11         26  
  11         3481  
22              
23             require Class::MOP::Class;
24 11     11   94 use DBIx::Class::AuditAny::Util;
  11         31  
  11         4661  
25              
26              
27             =head2 class_name
28              
29             Required. Full class name of the result class to be created
30              
31             =cut
32             has 'class_name', is => 'ro', isa => Str, required => 1;
33              
34             =head2 class_opts
35              
36             Optional extra params to supply to C<< Class::MOP::Class->create >>
37              
38             =cut
39             has 'class_opts', is => 'ro', isa => HashRef, default => sub {{}};
40              
41             =head2 table_name
42              
43             Required. The name of the table as would be supplied to C<< ->table() >> in the
44             result class.
45              
46             =cut
47             has 'table_name', is => 'ro', isa => Str, required => 1;
48              
49             =head2 columns
50              
51             Required. ArrayRef of DBIC column definitions suitable as arguments for C<< ->add_columns() >>
52              
53             =cut
54             has 'columns', is => 'ro', isa => ArrayRef, required => 1;
55              
56             =head2 call_class_methods
57              
58             Optional ArrayRef consumed in pairs, with the first value used as a method name, and the
59             second value an ArrayRef holding the args to supply to the method. Each of these are called
60             as class methods on the result class. This allows for any other calls to be handled, such as
61             adding uniq keys, and so on.
62              
63             =cut
64             has 'call_class_methods', is => 'ro', isa => ArrayRef, default => sub {[]};
65              
66              
67             =head1 METHODS
68              
69             =head2 initialize
70              
71             Initialization constructor. Expects the above attrs as a HashRef as they would be passed to
72             C<new()>. Creates the specified result class and invokes all the setup methods as defined above.
73              
74             =cut
75             sub initialize {
76 44     44 1 108 my $self = shift;
77 44 50       1043 $self = $self->new(@_) unless (ref $self);
78            
79 44         4343 my $class = $self->class_name;
80 44 50       193 die "class/namespace '$class' already defined!" if (package_exists $class);
81            
82             Class::MOP::Class->create($class,
83             superclasses => [ 'DBIx::Class::Core' ],
84 44 50       144 %{ $self->class_opts }
  44         271  
85             ) or die $@;
86            
87 44         111112 $class->table( $self->table_name );
88            
89 44         23250 $class->add_columns( @{$self->columns} );
  44         719  
90            
91 44         98463 my @call_list = @{$self->call_class_methods};
  44         250  
92 44         181 while (my $meth = shift @call_list) {
93 99         12852 my $args = shift @call_list;
94 99         2074 $class->$meth(@$args);
95             }
96              
97 44         18665 return $class;
98             }
99              
100             1;
101              
102             __END__
103              
104             =head1 SEE ALSO
105              
106             =over
107              
108             =item *
109              
110             L<DBIx::Class::AuditAny::Util::SchemaMaker>
111              
112             =item *
113              
114             L<DBIx::Class::AuditAny>
115              
116             =item *
117              
118             L<DBIx::Class>
119              
120             =back
121              
122             =head1 SUPPORT
123            
124             IRC:
125            
126             Join #rapidapp on irc.perl.org.
127              
128             =head1 AUTHOR
129              
130             Henry Van Styn <vanstyn@cpan.org>
131              
132             =head1 COPYRIGHT AND LICENSE
133              
134             This software is copyright (c) 2012-2015 by IntelliTree Solutions llc.
135              
136             This is free software; you can redistribute it and/or modify it under
137             the same terms as the Perl 5 programming language system itself.
138              
139             =cut