| 1 | package Mongoose::Document; | ||||||||||||||||||||
| 2 | 1 | $Mongoose::Document::VERSION = '2.02'; | |||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use Mongoose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use Mongoose::Join; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use Mongoose::File; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use MooseX::Role::Parameterized; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use Mongoose::Meta::AttributeTraits; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | parameter '-engine' => ( isa => 'Mongoose::Role::Engine', ); | |||||||||||||||||||
| 11 | 1 | parameter '-collection_name' => ( isa => 'Str', ); | |||||||||||||||||||
| 12 | 1 | parameter '-pk' => ( isa => 'ArrayRef[Str]', ); | |||||||||||||||||||
| 13 | 1 | parameter '-as' => ( isa => 'Str', ); | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 2 | ▶ | role { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.9
| |||||||||||||||||||||
| 16 | 2 | my $p = shift; | |||||||||||||||||||
| 17 | 2 | my %args = @_; | |||||||||||||||||||
| 18 | 2 | my $class_name; | |||||||||||||||||||
| 19 | 2 | ▶ | if ( $args{consumer}->isa('Moose::Meta::Class') ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 20 | 1 | $class_name = $args{consumer}->name; | |||||||||||||||||||
| 21 | } | ||||||||||||||||||||
| 22 | else { | ||||||||||||||||||||
| 23 | # If we get into this block of code, it means that Mongoose was consumed | ||||||||||||||||||||
| 24 | # not by a class but by another (intermediate) role. Mongoose needs to | ||||||||||||||||||||
| 25 | # know the original class for various reasons (naming the collection | ||||||||||||||||||||
| 26 | # name being the most obvious one but not the only one). | ||||||||||||||||||||
| 27 | # What follows is an ugly hack to climb back up the consumption hierarchy | ||||||||||||||||||||
| 28 | # to find out the name of the class which was originally used. If anyone | ||||||||||||||||||||
| 29 | # can do it differently than it has to be better than the below! | ||||||||||||||||||||
| 30 | # -- Allan Whiteford | ||||||||||||||||||||
| 31 | 1 | my $i=1; | |||||||||||||||||||
| 32 | 1 | ▶ | while ( my @caller = do { package | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 33 | 11 | DB; caller( $i++ ) } ) | |||||||||||||||||||
| 34 | { | ||||||||||||||||||||
| 35 | 11 | ▶ | if ( $caller[3] eq 'MooseX::Role::Parameterized::Meta::Trait::Parameterizable::generate_role' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | 2 | my @args = @DB::args; | |||||||||||||||||||
| 37 | 2 | my %args = @args[1..$#args]; | |||||||||||||||||||
| 38 | 2 | ▶ | if ($args{'consumer'}->isa('Moose::Meta::Class')) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | 1 | $class_name = $args{'consumer'}->name; | |||||||||||||||||||
| 40 | 1 | last; | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 2 | ▶ | die("Cannot find a class name to use") unless($class_name); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 2 | ▶ | my $collection_name = $p->{'-collection_name'} || do{ Mongoose->naming->("$class_name") }; | ||||||||||||||||||
|
Condition33%1/3
$p->{'-collection_name'} || do {
'Mongoose'->naming->("$class_name")
}
MC/DC0%0/2
$p->{'-collection_name'} || do {
'Mongoose'->naming->("$class_name")
}
$p->{'-collection_name'}
do {
'Mongoose'->naming->("$class_name")
}
Truth table33%1/3
A || B
A= $p->{'-collection_name'}B= do {
'Mongoose'->naming->("$class_name")
}
| |||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | # load the selected engine and consume it | ||||||||||||||||||||
| 51 | 2 | ▶ | with( $p->{'-engine'} || 'Mongoose::Engine' ); | ||||||||||||||||||
|
Condition50%1/2
$p->{'-engine'} || 'Mongoose::Engine'
MC/DC0%0/1
$p->{'-engine'} || 'Mongoose::Engine'
$p->{'-engine'}
Truth table50%1/2
A
A= $p->{'-engine'}
| |||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | # attributes | ||||||||||||||||||||
| 54 | 2 | has '_id' => ( is => 'rw', isa => 'BSON::OID', traits => ['DoNotMongoSerialize'] ); | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | # aliasing | ||||||||||||||||||||
| 57 | 2 | ▶ | if ( my $as = $p->{'-as'} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 59 | 0 | *{ $as . "::" } = \*{ $class_name . "::" }; | |||||||||||||||||||
| 60 | 0 | Mongoose->aliased( $as => $class_name ); | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | # Store this class config on mongoose | ||||||||||||||||||||
| 64 | Mongoose->class_config( $class_name => { | ||||||||||||||||||||
| 65 | pk => $p->{'-pk'}, | ||||||||||||||||||||
| 66 | 2 | as => $p->{'-as'}, | |||||||||||||||||||
| 67 | collection_name => $collection_name, | ||||||||||||||||||||
| 68 | }); | ||||||||||||||||||||
| 69 | 1 | }; | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | =head1 NAME | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | Mongoose::Document - a Mongoose document role | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | =head1 SYNOPSIS | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | package Person; | ||||||||||||||||||||
| 78 | use Moose; | ||||||||||||||||||||
| 79 | with 'Mongoose::Document'; | ||||||||||||||||||||
| 80 | has 'name' => ( is => 'rw', isa => 'Str', required => 1 ); | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | =head1 SEE ALSO | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | Read the Mongoose L<intro|Mongoose::Intro> or L<cookbook|Mongoose::Cookbook>. | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 1 | 1; | |||||||||||||||||||