File Coverage

blib/lib/Mongoose/Document.pm
Criterion Covered Total %
statement 21 21 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 28 28 100.0


line stmt bran cond sub pod time code
1             package Mongoose::Document;
2             $Mongoose::Document::VERSION = '1.02';
3 1     1   669 use strict;
  1         1  
  1         26  
4 1     1   284 use Mongoose;
  1         19047  
  1         44  
5 1     1   570 use Mongoose::Join;
  1         262  
  1         33  
6 1     1   449 use Mongoose::File;
  1         217  
  1         29  
7 1     1   483 use MooseX::Role::Parameterized;
  1         36609  
  1         3  
8 1     1   23309 use Mongoose::Meta::AttributeTraits;
  1         2  
  1         239  
9              
10             parameter '-engine' => ( isa => 'Mongoose::Role::Engine', );
11             parameter '-collection_name' => ( isa => 'Str', );
12             parameter '-pk' => ( isa => 'ArrayRef[Str]', );
13             parameter '-as' => ( isa => 'Str', );
14              
15             role {
16             my $p = shift;
17             my %args = @_;
18             my $class_name;
19             if ( $args{consumer}->isa('Moose::Meta::Class') ) {
20             $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             my $i=1;
32             while ( my @caller = do { package
33             DB; caller( $i++ ) } )
34             {
35             if ( $caller[3] eq 'MooseX::Role::Parameterized::Meta::Trait::Parameterizable::generate_role' ) {
36             my @args = @DB::args;
37             my %args = @args[1..$#args];
38             if ($args{'consumer'}->isa('Moose::Meta::Class')) {
39             $class_name = $args{'consumer'}->name;
40             last;
41             }
42             }
43             }
44             }
45              
46             die("Cannot find a class name to use") unless($class_name);
47              
48             my $collection_name = $p->{'-collection_name'} || do{ Mongoose->naming->("$class_name") };
49              
50             # load the selected engine and consume it
51             with( $p->{'-engine'} || 'Mongoose::Engine' );
52              
53             # attributes
54             has '_id' => ( is => 'rw', isa => 'MongoDB::OID', traits => ['DoNotMongoSerialize'] );
55              
56             # aliasing
57             if ( my $as = $p->{'-as'} ) {
58 1     1   6 no strict 'refs';
  1         1  
  1         174  
59             *{ $as . "::" } = \*{ $class_name . "::" };
60             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             as => $p->{'-as'},
67             collection_name => $collection_name,
68             });
69             };
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;