AuditAny/Util/SchemaMaker.pm

filter: Statementstmt 100% 36 / 36 Branchbran 66.6% 4 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 96.0% 49 / 51 CC 5 SCAR 9.9
CC
5
Cov
95%
initialize
5.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.7
« AuditAny/Util/ResultMaker.pm ↑ Summary AuditAny/DataPoint.pm »
1 package DBIx::Class::AuditAny::Util::SchemaMaker;
2 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 #use Moose;
23 #use MooseX::Types::Moose qw(HashRef ArrayRef Str Bool Maybe Object CodeRef);
24  
25 9 require Class::MOP::Class;
26 9 ▶use DBIx::Class::AuditAny::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 9 ▶use DBIx::Class::AuditAny::Util::ResultMaker;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 =head2 schema_namespace
30  
31 Required - the class name of the DBIC schema to be created
32  
33 =cut
34 9 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 11 ▶has 'class_opts', is => 'ro', isa => HashRef, default => sub {{}};
Subroutine __ANON__: called CC=1 SCAR=0.0
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 9 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 ▶ my $self = shift;
Subroutine initialize: called CC=5 SCAR=16.2
Pod documented
64 11 ▶ $self = $self->new(@_) unless (ref $self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 11 0 11
65
66 11 my $class = $self->schema_namespace;
67 11 ▶ die "class/namespace '$class' already defined!" if (package_exists $class);
Branch: 1 branch
BranchTrueFalseTotal
if &package_exists($class) 2 9 11
68
69 Class::MOP::Class->create($class,
70 superclasses => [ 'DBIx::Class::Schema' ],
71 9 ▶ %{ $self->class_opts }
Branch: 1 branch
BranchTrueFalseTotal
"Class::MOP::Class"->create($class, "superclasses", ["DBIx::Class::Schema"], %{$self->class_opts;}) or die $@ 0 9 9
72 ) or die $@;
73
74 9 my @Results = sort keys %{$self->results};
75
76 DBIx::Class::AuditAny::Util::ResultMaker->initialize(
77 class_name => $class . '::' . $_,
78 36 %{$self->results->{$_}}
79 9 ) for (@Results);
80
81 9 $class->load_classes(@Results);
82
83 9 return $class;
84 }
85  
86 9 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
« AuditAny/Util/ResultMaker.pm ↑ Summary AuditAny/DataPoint.pm »