1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
package DBIx::Class::Helpers::Util;
{
$DBIx::Class::Helpers::Util::VERSION = '2.013002';
}
use strict;
use warnings;
# ABSTRACT: Helper utilities for DBIx::Class components
use Sub::Exporter -setup => {
exports => [
qw(
get_namespace_parts is_load_namespaces is_not_load_namespaces
assert_similar_namespaces
),
],
};
sub get_namespace_parts {
my $package = shift;
if ($package =~ m/(^[\w:]+::Result)::([\w:]+)$/) {
return ($1, $2);
} else {
die "$package doesn't look like ".'$namespace::Result::$resultclass';
}
}
sub is_load_namespaces {
my $namespace = shift;
$namespace =~ /^[\w:]+::Result::[\w:]+$/;
}
sub is_not_load_namespaces {
my $namespace = shift;
$namespace =~ /^([\w:]+)::[\w]+$/ and
$1 !~ /::Result/;
}
sub assert_similar_namespaces {
my $ns1 = shift;
my $ns2 = shift;
die "Namespaces $ns1 and $ns2 are dissimilar"
unless is_load_namespaces($ns1) and is_load_namespaces($ns2) or
is_not_load_namespaces($ns1) and is_not_load_namespaces($ns2);
}
1;
=pod
=head1 NAME
DBIx::Class::Helpers::Util - Helper utilities for DBIx::Class components
=head1 VERSION
version 2.013002
=head1 SYNOPSIS
my ($namespace, $class) = get_namespace_parts('MyApp:Schema::Person');
is $namespace, 'MyApp::Schema';
is $class, 'Person';
if (is_load_namespaces('MyApp::Schema::Result::Person')) {
print 'correctly structured project';
}
if (is_not_load_namespaces('MyApp::Schema::Person')) {
print 'incorrectly structured project';
}
if (assert_similar_namespaces('MyApp::Schema::Person', 'FooApp::Schema::People')) {
print 'both projects are structured similarly';
}
if (assert_similar_namespaces('MyApp::Schema::Result::Person', 'FooApp::Schema::Result::People')) {
print 'both projects are structured similarly';
}
=head1 DESCRIPTION
A collection of various helper utilities for L<DBIx::Class> stuff. Probably
only useful for components.
=head1 METHODS
=head2 get_namespace_parts
Returns the namespace and class name of a package. See L</SYNOPSIS> for example.
=head2 is_load_namespaces
Returns true if a package is structured in a way that would work for
load_namespaces. See L</SYNOPSIS> for example.
=head2 is_not_load_namespaces
Returns true if a package is structured in a way that would not work for
load_namespaces. See L</SYNOPSIS> for example.
=head2 assert_similar_namespaces
Dies if both packages are structured in the same way. The same means both are
load_namespaces or both are not. See L</SYNOPSIS> for example.
=head1 AUTHOR
Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2012 by Arthur Axel "fREW" Schmidt.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
__END__
|