| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Mongol::Roles::Relations; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 4759 | use Moose::Role; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 14 |  | 
| 4 | 2 |  |  | 2 |  | 7027 | use Moose::Util qw( does_role ); | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 13 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 2 |  |  | 2 |  | 279 | use Class::Load qw( load_class ); | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 98 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 2 |  |  | 2 |  | 1544 | use Lingua::EN::Inflect qw( PL ); | 
|  | 2 |  |  |  |  | 35321 |  | 
|  | 2 |  |  |  |  | 935 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | requires 'id'; | 
| 11 |  |  |  |  |  |  | requires 'find'; | 
| 12 |  |  |  |  |  |  | requires 'find_one'; | 
| 13 |  |  |  |  |  |  | requires 'retrieve'; | 
| 14 |  |  |  |  |  |  | requires 'delete'; | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | sub has_many { | 
| 17 | 0 |  |  | 0 | 1 |  | my ( $class, $type, $foreign_key, $moniker ) = @_; | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 0 | 0 |  |  |  |  | die( 'No type defined!' ) | 
| 20 |  |  |  |  |  |  | unless( defined( $type ) ); | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 0 | 0 |  |  |  |  | die( 'No foreign key defined!' ) | 
| 23 |  |  |  |  |  |  | unless( $foreign_key ); | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 0 | 0 |  |  |  |  | $moniker = _get_moniker( $type ) | 
| 26 |  |  |  |  |  |  | unless( $moniker ); | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 0 |  |  |  |  |  | load_class( $type ); | 
| 29 | 0 | 0 |  |  |  |  | die( sprintf( '%s cannot do basic operations!', $type ) ) | 
| 30 |  |  |  |  |  |  | unless( does_role( $type, 'Mongol::Roles::Core' ) ); | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | $class->meta()->add_method( sprintf( 'add_%s', $moniker ) => sub { | 
| 33 | 0 |  |  | 0 |  |  | my ( $self, $data, $options ) = @_; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 0 |  |  |  |  |  | $data->{ $foreign_key } = $self->id(); | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 0 |  |  |  |  |  | return $type->new( $data ) | 
| 38 |  |  |  |  |  |  | ->save(); | 
| 39 |  |  |  |  |  |  | } | 
| 40 | 0 |  |  |  |  |  | ); | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | $class->meta()->add_method( sprintf( 'get_%s', PL( $moniker ) ) => sub { | 
| 43 | 0 |  |  | 0 |  |  | my ( $self, $query, $options ) = @_; | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 0 |  | 0 |  |  |  | $query ||= {}; | 
| 46 | 0 |  |  |  |  |  | $query->{ $foreign_key } = $self->id(); | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 0 |  |  |  |  |  | return $type->find( $query, $options ); | 
| 49 |  |  |  |  |  |  | } | 
| 50 | 0 |  |  |  |  |  | ); | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | $class->meta()->add_method( sprintf( 'get_%s', $moniker ) => sub { | 
| 53 | 0 |  |  | 0 |  |  | my ( $self, $id ) = @_; | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 0 |  |  |  |  |  | return $type->find_one( | 
| 56 |  |  |  |  |  |  | { | 
| 57 |  |  |  |  |  |  | _id => $id, | 
| 58 |  |  |  |  |  |  | $foreign_key => $self->id(), | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  | ); | 
| 61 |  |  |  |  |  |  | } | 
| 62 | 0 |  |  |  |  |  | ); | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | $class->meta()->add_method( sprintf( 'remove_%s', PL( $moniker ) ) => sub { | 
| 65 | 0 |  |  | 0 |  |  | my ( $self, $query ) = @_; | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 0 |  | 0 |  |  |  | $query ||= {}; | 
| 68 | 0 |  |  |  |  |  | $query->{ $foreign_key } = $self->id(); | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 0 |  |  |  |  |  | return $type->delete( $query ); | 
| 71 |  |  |  |  |  |  | } | 
| 72 | 0 |  |  |  |  |  | ); | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | sub has_one { | 
| 76 | 0 |  |  | 0 | 1 |  | my ( $class, $type, $foreign_key, $moniker ) = @_; | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 0 | 0 |  |  |  |  | die( 'No type defined!' ) | 
| 79 |  |  |  |  |  |  | unless( defined( $type ) ); | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 0 | 0 |  |  |  |  | die( 'No foreign key defined!' ) | 
| 82 |  |  |  |  |  |  | unless( $foreign_key ); | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 0 | 0 |  |  |  |  | $moniker = _get_moniker( $type ) | 
| 85 |  |  |  |  |  |  | unless( $moniker ); | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 0 |  |  |  |  |  | load_class( $type ); | 
| 88 | 0 | 0 |  |  |  |  | die( sprintf( '%s cannot do basic operations!', $type ) ) | 
| 89 |  |  |  |  |  |  | unless( does_role( $type, 'Mongol::Roles::Core' ) ); | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | $class->meta()->add_method( sprintf( 'get_%s', $moniker ) => sub { | 
| 92 | 0 |  |  | 0 |  |  | my $self = shift(); | 
| 93 |  |  |  |  |  |  |  | 
| 94 | 0 |  |  |  |  |  | return $type->retrieve( $self->$foreign_key() ); | 
| 95 |  |  |  |  |  |  | } | 
| 96 | 0 |  |  |  |  |  | ); | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | sub _get_moniker { | 
| 100 | 0 |  |  | 0 |  |  | my $type = shift(); | 
| 101 |  |  |  |  |  |  |  | 
| 102 | 0 |  |  |  |  |  | ( my $name = $type ) =~ s/.+:://; | 
| 103 | 0 |  |  |  |  |  | return lc( $name ); | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  |  | 
| 106 | 2 |  |  | 2 |  | 19 | no Moose::Role; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 24 |  | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | 1; | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | __END__ | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | =pod | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | =head1 NAME | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | Mongol::Roles::Relations - Automatic relations builder | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | =head1 METHODS | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | =head2 has_many | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | To be implemented. | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | =head2 has_one | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | To be implemented. | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | =over 4 | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | =item * | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | L<MongoDB> | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | =back | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =cut |