File Coverage

blib/lib/Mongoose/Class.pm
Criterion Covered Total %
statement 6 27 22.2
branch 0 6 0.0
condition 0 11 0.0
subroutine 2 6 33.3
pod 3 3 100.0
total 11 53 20.7


line stmt bran cond sub pod time code
1             package Mongoose::Class;
2             $Mongoose::Class::VERSION = '1.02';
3 2     2   433446 use Moose ();
  2         617964  
  2         69  
4 2     2   16 use Moose::Exporter;
  2         2  
  2         13  
5              
6             Moose::Exporter->setup_import_methods(
7             with_meta => [ 'has_many', 'belongs_to', 'has_one' ],
8             also => 'Moose',
9             );
10              
11             sub has_many {
12 0     0 1   my ( $meta, $name ) = ( shift, shift );
13 0 0         my %options = @_ == 1 ? ( isa => shift ) : @_;
14 0           my $isa_original = $options{isa};
15              
16 0           $options{isa} = 'Mongoose::Join[' . $options{isa} . ']';
17 0   0 0     $options{default} ||= sub{ Mongoose::Join->new( with_class => "$isa_original" ) };
  0            
18 0   0       $options{is} ||= 'ro';
19              
20 0           $meta->add_attribute( $name, %options );
21             }
22              
23             sub has_one {
24 0     0 1   my $meta = shift;
25 0           my $name = shift;
26 0           my %options;
27 0 0 0       if ( @_ > 0 && @_ % 2 ) {
28 0           $options{isa} = shift;
29 0           $options{is} = 'rw';
30 0 0         if( @_ > 1 ) { # allow: has_one 'att' => 'Str', required=>1;
31 0           %options = ( %options, @_ );
32             }
33             }
34             else {
35 0           %options = @_;
36 0   0       $options{isa} ||= 'Any';
37 0   0       $options{is} ||= 'rw';
38             }
39              
40 0           $meta->add_attribute( $name, %options, );
41             }
42              
43 0     0 1   sub belongs_to { goto &has_one }
44              
45             =head1 NAME
46              
47             Mongoose::Class - sugary Mongoose-oriented replacement for Moose
48              
49             =head1 SYNOPSIS
50              
51             package MySchema::Person;
52             use Mongoose::Class; # uses Moose for you
53             with 'Mongoose::Document';
54              
55             has 'name' => ( is => 'rw', isa => 'Str' );
56             has_many 'siblings' => ( is => 'rw', isa => 'Person' );
57             belongs_to 'club' => ( is => 'rw', isa => 'Club' );
58             has_one 'father' => ( is => 'rw', isa => 'Person' );
59              
60             =head1 DESCRIPTION
61              
62             This is very much a work-in-progress.
63              
64             Basically, this module adds some sugar into your Mongoose
65             Document class by defining some stand-in replacements for
66             Moose's own C<has>.
67              
68             has_many
69             has_one
70             belongs_to
71              
72             The idea: fewer keystrokes and improved readability
73             by self-documenting your class.
74              
75             =head1 METHODS
76              
77             =head2 has_one
78              
79             Wrapper around Moose's own C<has>, but allows
80             for a shorter syntax:
81              
82             has_one 'name'; # isa=>'Any', is=>'rw' added
83             has_one 'age' => 'Num'; # is=>'rw' added
84             has_one 'age' => 'Num', default=>99;
85              
86             =head2 belongs_to
87              
88             It's the same as using C<has_one> from above.
89             It exists to improve your code expressiveness.
90              
91             =head2 has_many
92              
93             Wraps the defined relationship with another class using C<Mongoose::Join>.
94              
95             This:
96              
97             has_many 'employees' => ( isa=>'Employee' );
98              
99             # or
100              
101             has_many 'employees' => 'Employee';
102              
103             Becomes this:
104              
105             has 'employees' => (
106             is => 'ro',
107             isa => 'Mongoose::Join[Employee]',
108             default => sub { Mongoose::Join->new( with_class=>'Employee' ) }
109             );
110              
111             =cut
112              
113             1;