| 1 | package DBIx::Class::AuditAny::Util::SchemaMaker; | ||||||||||
| 2 | 11 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 11 | ▶ | 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 | 11 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 11 | ▶ | 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 | 11 | require Class::MOP::Class; | |||||||||
| 26 | 11 | ▶ | use DBIx::Class::AuditAny::Util; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 11 | ▶ | 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 | 11 | 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 | 13 | ▶ | 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 | 11 | 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 | 13 | ▶ | sub initialize { | ||||||||
|
Subroutine
initialize: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||
| 63 | 13 | my $self = shift; | |||||||||
| 64 | 13 | ▶ | $self = $self->new(@_) unless (ref $self); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | |||||||||||
| 66 | 13 | my $class = $self->schema_namespace; | |||||||||
| 67 | 13 | ▶ | die "class/namespace '$class' already defined!" if (package_exists $class); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | |||||||||||
| 69 | Class::MOP::Class->create($class, | ||||||||||
| 70 | superclasses => [ 'DBIx::Class::Schema' ], | ||||||||||
| 71 | 11 | ▶ | %{ $self->class_opts } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | ) or die $@; | ||||||||||
| 73 | |||||||||||
| 74 | 11 | my @Results = sort keys %{$self->results}; | |||||||||
| 75 | |||||||||||
| 76 | DBIx::Class::AuditAny::Util::ResultMaker->initialize( | ||||||||||
| 77 | class_name => $class . '::' . $_, | ||||||||||
| 78 | 44 | %{$self->results->{$_}} | |||||||||
| 79 | 11 | ) for (@Results); | |||||||||
| 80 | |||||||||||
| 81 | 11 | $class->load_classes(@Results); | |||||||||
| 82 | |||||||||||
| 83 | 11 | return $class; | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 11 | 1; | |||||||||
| 87 | |||||||||||
| 88 | __END__ | ||||||||||