| 1 | package MooseX::Role::Nameable; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | MooseX::Role::Nameable - automatic class short names | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | =cut | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 1 | ▶ | use MooseX::Role::Parameterized; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | =head1 VERSION | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | Version 1.01 | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =cut | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 1 | our $VERSION = '1.01'; | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | This role provides a method to automatically generate a short name for a Moose | ||||||||||||||||||||
| 25 | class from the package name. This is especially useful for classes that are | ||||||||||||||||||||
| 26 | used in conjunction with the factory pattern. When this role is included, the | ||||||||||||||||||||
| 27 | name for the access function is required. In addition, a regex may be defined | ||||||||||||||||||||
| 28 | to modify how the value is extracted. By default, the last namespace in the | ||||||||||||||||||||
| 29 | package is used. The short name assumes the package is in camel-case and | ||||||||||||||||||||
| 30 | normalizes it following some simple rules: | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | - convert to lower case | ||||||||||||||||||||
| 33 | - words are separated by underscores | ||||||||||||||||||||
| 34 | - acronyms are treated as a single word | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | Example usage: | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | package Foo::Base; | ||||||||||||||||||||
| 39 | use Moose; | ||||||||||||||||||||
| 40 | with | ||||||||||||||||||||
| 41 | 'MooseX::Role::Nameable' => {name => 'type'}, | ||||||||||||||||||||
| 42 | 'MooseX::Role::Nameable' => {name => 'nationality', regex => qr{::([^:]+)Child$}}; | ||||||||||||||||||||
| 43 | __PACKAGE__->meta->make_immutable; | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | package Foo::AmericanChild; | ||||||||||||||||||||
| 46 | use Moose; | ||||||||||||||||||||
| 47 | extends 'Foo::Base'; | ||||||||||||||||||||
| 48 | __PACKAGE__->meta->make_immutable; | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | package Foo::BritishChild; | ||||||||||||||||||||
| 51 | use Moose; | ||||||||||||||||||||
| 52 | extends 'Foo::Base'; | ||||||||||||||||||||
| 53 | __PACKAGE__->meta->make_immutable; | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | package main; | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | my $child1 = Foo::AmericanChild->new; | ||||||||||||||||||||
| 58 | my $child2 = Foo::BritishChild->new; | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | local $\="\n"; | ||||||||||||||||||||
| 61 | print $_->type . ':' . $_->nationality for ($child1, $child2); | ||||||||||||||||||||
| 62 | # => | ||||||||||||||||||||
| 63 | # american_child:american | ||||||||||||||||||||
| 64 | # british_child:british | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | =cut | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 1 | my %CACHED_NAMES; | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 1 | parameter name => (isa => 'Str', required => 1); | |||||||||||||||||||
| 71 | 1 | parameter regex => (); | |||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 2 | ▶ | role { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 74 | 2 | my $p = shift; | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 2 | my $name = $p->name; | |||||||||||||||||||
| 77 | 2 | ▶ | my $regex = $p->regex || '([^:]*)$'; | ||||||||||||||||||
|
Condition100%2/2
$p->regex || '([^:]*)$'
MC/DC100%1/1
$p->regex || '([^:]*)$'
$p->regex
Truth table100%2/2
A
A= $p->regex
| |||||||||||||||||||||
| 78 | 2 | $regex = qr{$regex}; | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 6 | ▶ | method $name => sub { | ||||||||||||||||||
|
Subroutine
name: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 81 | 6 | my $class = shift; | |||||||||||||||||||
| 82 | 6 | ▶ | $class = ref $class || $class; | ||||||||||||||||||
|
Condition66%2/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table66%2/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 6 | my $n = $CACHED_NAMES{"$class\::$name"}; | |||||||||||||||||||
| 85 | 6 | ▶ | unless (defined $n) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 5 | ($n) = $class =~ $regex; | |||||||||||||||||||
| 87 | 5 | $n =~ s/([[:lower:]])([[:upper:]])/$1\_$2/g; | |||||||||||||||||||
| 88 | 5 | $n =~ s/([[:upper:]])([[:upper:]][[:lower:]])/$1\_$2/g; | |||||||||||||||||||
| 89 | 5 | $n = $CACHED_NAMES{"$class\::$name"} = lc $n; | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | 6 | return $n; | |||||||||||||||||||
| 92 | 2 | }; | |||||||||||||||||||
| 93 | 1 | }; | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | =head1 AUTHOR | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | Aaron Cohen, C<< <aarondcohen at gmail.com> >> | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | This module was made possible by L<Shutterstock|http://www.shutterstock.com/> | ||||||||||||||||||||
| 102 | (L<@ShutterTech|https://twitter.com/ShutterTech>). Additional open source | ||||||||||||||||||||
| 103 | projects from Shutterstock can be found at | ||||||||||||||||||||
| 104 | L<code.shutterstock.com|http://code.shutterstock.com/>. | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | =head1 BUGS | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | Please report any bugs or feature requests to C<bug-MooseX-Role-Nameable at rt.cpan.org>, or through | ||||||||||||||||||||
| 109 | the web interface at L<https://github.com/aarondcohen/perl-moosex-role-nameable/issues>. I will | ||||||||||||||||||||
| 110 | be notified, and then you'll automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | =head1 SUPPORT | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | perldoc MooseX::Role::Nameable | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | You can also look for information at: | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | =over 4 | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =item * Official GitHub Repo | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | L<https://github.com/aarondcohen/perl-moosex-role-nameable> | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | =item * GitHub's Issue Tracker (report bugs here) | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | L<https://github.com/aarondcohen/perl-moosex-role-nameable/issues> | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =item * CPAN Ratings | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | L<http://cpanratings.perl.org/d/MooseX-Role-Nameable> | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | =item * Official CPAN Page | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | L<http://search.cpan.org/dist/MooseX-Role-Nameable/> | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | =back | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | Copyright 2013 Aaron Cohen. | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 145 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||
| 146 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | =cut | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | 1 | 1; # End of MooseX::Role::Nameable | |||||||||||||||||||