File Coverage

blib/lib/DBIx/Class/AuditAny/Util/SchemaMaker.pm
Criterion Covered Total %
statement 30 30 100.0
branch 3 6 50.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 41 44 93.1


line stmt bran cond sub pod time code
1             package DBIx::Class::AuditAny::Util::SchemaMaker;
2 11     11   85 use strict;
  11         30  
  11         346  
3 11     11   65 use warnings;
  11         28  
  11         303  
4              
5             # ABSTRACT: On-the-fly creation of DBIC Schema classes
6              
7             =head1 NAME
8              
9             DBIx::Class::AuditAny::Util::SchemaMaker - On-the-fly creation of DBIC Schema classes
10              
11             =head1 DESCRIPTION
12              
13             This package provides an easy way to conjurer new DBIC schema classes into existence
14              
15             =head1 ATTRIBUTES
16              
17             =cut
18              
19 11     11   59 use Moo;
  11         35  
  11         72  
20 11     11   14701 use MooX::Types::MooseLike::Base qw(:all);
  11         40  
  11         3645  
21              
22             #use Moose;
23             #use MooseX::Types::Moose qw(HashRef ArrayRef Str Bool Maybe Object CodeRef);
24              
25             require Class::MOP::Class;
26 11     11   95 use DBIx::Class::AuditAny::Util;
  11         28  
  11         1091  
27 11     11   5748 use DBIx::Class::AuditAny::Util::ResultMaker;
  11         42  
  11         3485  
28              
29             =head2 schema_namespace
30              
31             Required - the class name of the DBIC schema to be created
32              
33             =cut
34             has 'schema_namespace', is => 'ro', isa => Str, required => 1;
35              
36             =head2 class_opts
37              
38             Optional extra params to supply to C<< Class::MOP::Class->create >>
39              
40             =cut
41             has 'class_opts', is => 'ro', isa => HashRef, default => sub {{}};
42              
43             =head2 results
44              
45             HashRef of key/value pairs defining the result/sources to be created. The key
46             is the source name, while the value must be a HashRef to be supplied to the
47             C<initialize> constructor of L<DBIx::Class::AuditAny::Util::ResultMaker>. The
48             C<class_name> does not need to be specified here as it is automatically set
49             according to the C<schema_namespace> and the source name (key value).
50              
51             =cut
52             has 'results', is => 'ro', isa => HashRef[HashRef], required => 1;
53              
54             =head1 METHODS
55              
56             =head2 initialize
57              
58             Initialization constructor. Expects the above attrs as a HashRef as they would be passed to
59             C<new()>. Creates the specified schema and associated result classes on-the-spot.
60              
61             =cut
62             sub initialize {
63 11     11 1 1353 my $self = shift;
64 11 50       100 $self = $self->new(@_) unless (ref $self);
65            
66 11         1805 my $class = $self->schema_namespace;
67 11 50       81 die "class/namespace '$class' already defined!" if (package_exists $class);
68            
69             Class::MOP::Class->create($class,
70             superclasses => [ 'DBIx::Class::Schema' ],
71 11 50       49 %{ $self->class_opts }
  11         191  
72             ) or die $@;
73            
74 11         34253 my @Results = sort keys %{$self->results};
  11         132  
75            
76             DBIx::Class::AuditAny::Util::ResultMaker->initialize(
77             class_name => $class . '::' . $_,
78 44         416 %{$self->results->{$_}}
79 11         79 ) for (@Results);
80            
81 11         179 $class->load_classes(@Results);
82            
83 11         28550 return $class;
84             }
85              
86             1;
87              
88             __END__
89              
90             =head1 SEE ALSO
91              
92             =over
93              
94             =item *
95              
96             L<DBIx::Class::AuditAny>
97              
98             =item *
99              
100             L<DBIx::Class>
101              
102             =back
103              
104             =head1 SUPPORT
105            
106             IRC:
107            
108             Join #rapidapp on irc.perl.org.
109              
110             =head1 AUTHOR
111              
112             Henry Van Styn <vanstyn@cpan.org>
113              
114             =head1 COPYRIGHT AND LICENSE
115              
116             This software is copyright (c) 2012-2015 by IntelliTree Solutions llc.
117              
118             This is free software; you can redistribute it and/or modify it under
119             the same terms as the Perl 5 programming language system itself.
120              
121             =cut