line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mongoose::Role::Naming; |
2
|
|
|
|
|
|
|
$Mongoose::Role::Naming::VERSION = '2.02'; |
3
|
25
|
|
|
25
|
|
15339
|
use Moose::Role; |
|
25
|
|
|
|
|
58
|
|
|
25
|
|
|
|
|
233
|
|
4
|
25
|
|
|
25
|
|
125031
|
use Moose::Util::TypeConstraints; |
|
25
|
|
|
|
|
70
|
|
|
25
|
|
|
|
|
209
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# naming templates |
7
|
|
|
|
|
|
|
my %naming_template = ( |
8
|
|
|
|
|
|
|
same => sub { $_[0] }, |
9
|
|
|
|
|
|
|
short => sub { $_[0] =~ s{^.*\:\:(.*?)$}{$1}g; $_[0] }, |
10
|
|
|
|
|
|
|
plural => sub { $_[0] =~ s{^.*\:\:(.*?)$}{$1}g; lc "$_[0]s" }, |
11
|
|
|
|
|
|
|
decamel => sub { $_[0] =~ s{([a-z])([A-Z])}{$1_$2}g; lc $_[0] }, |
12
|
|
|
|
|
|
|
undercolon => sub { $_[0] =~ s{\:\:}{_}g; lc $_[0] }, |
13
|
|
|
|
|
|
|
lower => sub { lc $_[0] }, |
14
|
|
|
|
|
|
|
lc => sub { lc $_[0] }, |
15
|
|
|
|
|
|
|
upper => sub { uc $_[0] }, |
16
|
|
|
|
|
|
|
uc => sub { uc $_[0] }, |
17
|
|
|
|
|
|
|
default => sub { |
18
|
|
|
|
|
|
|
$_[0] =~ s{([a-z])([A-Z])}{$1_$2}g; |
19
|
|
|
|
|
|
|
$_[0] =~ s{\:\:}{_}g; |
20
|
|
|
|
|
|
|
lc $_[0]; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
subtype 'Mongoose::CodeRef' => as 'CodeRef'; |
25
|
|
|
|
|
|
|
coerce 'Mongoose::CodeRef' |
26
|
|
|
|
|
|
|
=> from 'Str' => via { |
27
|
|
|
|
|
|
|
my $template = $naming_template{ $_[0] } |
28
|
|
|
|
|
|
|
or die "naming template '$_[0]' not found"; |
29
|
|
|
|
|
|
|
return $template; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
=> from 'ArrayRef' => via { |
32
|
|
|
|
|
|
|
my @filters; |
33
|
|
|
|
|
|
|
for( @{ $_[0] } ) { |
34
|
|
|
|
|
|
|
my $template = $naming_template{ $_ } |
35
|
|
|
|
|
|
|
or die "naming template '$_' not found"; |
36
|
|
|
|
|
|
|
# add filter to list |
37
|
|
|
|
|
|
|
push @filters, sub { |
38
|
|
|
|
|
|
|
my $name = shift; |
39
|
|
|
|
|
|
|
return $template->($name); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
# now, accumulate all filters |
43
|
|
|
|
|
|
|
return sub { |
44
|
|
|
|
|
|
|
my $name = shift; |
45
|
|
|
|
|
|
|
map { $name = $_->($name) } @filters; |
46
|
|
|
|
|
|
|
return $name; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
}; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
has 'naming' => ( |
51
|
|
|
|
|
|
|
is => 'rw', |
52
|
|
|
|
|
|
|
isa => 'Mongoose::CodeRef', |
53
|
|
|
|
|
|
|
coerce => 1, |
54
|
|
|
|
|
|
|
default => sub {$naming_template{default}} |
55
|
|
|
|
|
|
|
); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|