| 1 | package Mongoose::Role::Naming; | ||||||||||
| 2 | 25 | $Mongoose::Role::Naming::VERSION = '2.02'; | |||||||||
| 3 | 25 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 25 | ▶ | use Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | # naming templates | ||||||||||
| 7 | my %naming_template = ( | ||||||||||
| 8 | 0 | ▶ | same => sub { $_[0] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 9 | 0 | ▶ | short => sub { $_[0] =~ s{^.*\:\:(.*?)$}{$1}g; $_[0] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 10 | 0 | ▶ | plural => sub { $_[0] =~ s{^.*\:\:(.*?)$}{$1}g; lc "$_[0]s" }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 11 | 0 | ▶ | decamel => sub { $_[0] =~ s{([a-z])([A-Z])}{$1_$2}g; lc $_[0] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 12 | 0 | ▶ | undercolon => sub { $_[0] =~ s{\:\:}{_}g; lc $_[0] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 13 | 0 | ▶ | lower => sub { lc $_[0] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 14 | 0 | ▶ | lc => sub { lc $_[0] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 15 | 0 | ▶ | upper => sub { uc $_[0] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 16 | 0 | ▶ | uc => sub { uc $_[0] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 17 | 2 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 2 | $_[0] =~ s{([a-z])([A-Z])}{$1_$2}g; | |||||||||
| 19 | 2 | $_[0] =~ s{\:\:}{_}g; | |||||||||
| 20 | 2 | lc $_[0]; | |||||||||
| 21 | } | ||||||||||
| 22 | 25 | ); | |||||||||
| 23 | |||||||||||
| 24 | 25 | subtype 'Mongoose::CodeRef' => as 'CodeRef'; | |||||||||
| 25 | coerce 'Mongoose::CodeRef' | ||||||||||
| 26 | 0 | ▶ | => from 'Str' => via { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 27 | 0 | ▶ | my $template = $naming_template{ $_[0] } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | or die "naming template '$_[0]' not found"; | ||||||||||
| 29 | 0 | return $template; | |||||||||
| 30 | } | ||||||||||
| 31 | 0 | ▶ | => from 'ArrayRef' => via { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=23.3
| |||||||||||
| 32 | 0 | my @filters; | |||||||||
| 33 | 0 | for( @{ $_[0] } ) { | |||||||||
| 34 | 0 | ▶ | my $template = $naming_template{ $_ } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | or die "naming template '$_' not found"; | ||||||||||
| 36 | # add filter to list | ||||||||||
| 37 | 0 | ▶ | push @filters, sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 38 | 0 | my $name = shift; | |||||||||
| 39 | 0 | return $template->($name); | |||||||||
| 40 | } | ||||||||||
| 41 | 0 | } | |||||||||
| 42 | # now, accumulate all filters | ||||||||||
| 43 | 0 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 44 | 0 | my $name = shift; | |||||||||
| 45 | 0 | map { $name = $_->($name) } @filters; | |||||||||
| 46 | 0 | return $name; | |||||||||
| 47 | } | ||||||||||
| 48 | 0 | }; | |||||||||
| 49 | |||||||||||
| 50 | has 'naming' => ( | ||||||||||
| 51 | is => 'rw', | ||||||||||
| 52 | isa => 'Mongoose::CodeRef', | ||||||||||
| 53 | coerce => 1, | ||||||||||
| 54 | 25 | ▶ | default => sub {$naming_template{default}} | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 55 | 25 | ); | |||||||||
| 56 | |||||||||||
| 57 | 25 | 1; | |||||||||
| 58 | |||||||||||
| 59 | =head1 NAME | ||||||||||
| 60 | |||||||||||
| 61 | Mongoose::Role::Naming | ||||||||||
| 62 | |||||||||||
| 63 | =head1 DESCRIPTION | ||||||||||
| 64 | |||||||||||
| 65 | This role implement class to collection name methods for Mongoose objects. | ||||||||||
| 66 | |||||||||||
| 67 | =cut | ||||||||||
| 68 | |||||||||||
| 69 | =head2 naming | ||||||||||
| 70 | |||||||||||
| 71 | By default will compose the MongoDB collection name from your package name | ||||||||||
| 72 | by replacing double-colon C<::> with underscores C<_>, separating camel-case, | ||||||||||
| 73 | such as C<aB> with C<a_b> and uppercase with lowercase letters. | ||||||||||
| 74 | |||||||||||
| 75 | This behaviour can be changed by choosing a named method or by setting | ||||||||||
| 76 | the collection naming routine with a C<closure>. | ||||||||||
| 77 | |||||||||||
| 78 | This are the available named methods: | ||||||||||
| 79 | |||||||||||
| 80 | named method | package name | collection | ||||||||||
| 81 | --------------+-----------------------+----------------------- | ||||||||||
| 82 | short | MyApp::Schema::FooBar | foobar | ||||||||||
| 83 | plural | MyApp::Schema::FooBar | foobars | ||||||||||
| 84 | decamel | MyApp::Schema::FooBar | foo_bar | ||||||||||
| 85 | lower | MyApp::Schema::FooBar | myapp::schema::foobar | ||||||||||
| 86 | upper | MyApp::Schema::FooBar | MYAPP::SCHEMA::FOOBAR | ||||||||||
| 87 | undercolon | MyApp::Schema::FooBar | myapp_schema_foobar | ||||||||||
| 88 | default | MyApp::Schema::FooBar | myapp_schema_foo_bar | ||||||||||
| 89 | none | MyApp::Schema::FooBar | MyApp::Schema::FooBar | ||||||||||
| 90 | |||||||||||
| 91 | You can choose a predefined naming method | ||||||||||
| 92 | |||||||||||
| 93 | Mongoose->naming( 'plural' ); | ||||||||||
| 94 | |||||||||||
| 95 | ... or combine them | ||||||||||
| 96 | |||||||||||
| 97 | Mongoose->naming( ['decamel','plural' ] ); # same as 'shorties' | ||||||||||
| 98 | |||||||||||
| 99 | If you set a closure it will receive the package name as it only parameter and | ||||||||||
| 100 | should return the collection name. | ||||||||||
| 101 | |||||||||||
| 102 | # plain lowercase | ||||||||||
| 103 | Mongoose->naming( sub { lc(shift) } ); | ||||||||||
| 104 | |||||||||||