| 1 | package Mandel; | ||||||||||||||||||||
| 2 | 18 | ▶ | use Mojo::Base 'Mojo::Base'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 18 | ▶ | use Mojo::Loader qw(find_modules load_class); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 18 | ▶ | use Mojo::Util; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 18 | ▶ | use Mandel::Collection; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 18 | ▶ | use Mandel::Model; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 18 | ▶ | use Mango; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 18 | ▶ | use Carp 'confess'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 18 | our $VERSION = '0.31'; | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 5 | ▶ | has model_class => sub { shift->_build_model_class }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 3 | ▶ | has namespaces => sub { shift->_build_namespaces }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 0 | ▶ | has storage => sub { shift->_build_storage }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 5 | ▶ | sub _build_model_class {'Mandel::Model'} | ||||||||||||||||||
|
Subroutine
_build_model_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 3 | ▶ | sub _build_namespaces { [ref $_[0]] } | ||||||||||||||||||
|
Subroutine
_build_namespaces: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 0 | ▶ | sub _build_storage { Mango->new('mongodb://localhost/mandeltest') } | ||||||||||||||||||
|
Subroutine
_build_storage: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 2 | ▶ | sub connect { | ||||||||||||||||||
|
Subroutine
connect: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||
| 21 | 2 | my ($self, @args) = @_; | |||||||||||||||||||
| 22 | 2 | my $storage = Mango->new(@args); | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 2 | ▶ | if (my $class = ref $self) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 25 | 2 | $self = $class->new(%$self, storage => $storage); | |||||||||||||||||||
| 26 | } | ||||||||||||||||||||
| 27 | else { | ||||||||||||||||||||
| 28 | 0 | $self = $self->new(storage => $storage); | |||||||||||||||||||
| 29 | } | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 2 | $self; | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 3 | ▶ | sub all_document_names { | ||||||||||||||||||
|
Subroutine
all_document_names: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||
| 35 | 3 | my $self = shift; | |||||||||||||||||||
| 36 | 0 | ▶ | my %names = map { $_, 1 } keys %{$self->{model} || {}}; | ||||||||||||||||||
|
Condition50%1/2
$self->{'model'} || {}
MC/DC0%0/1
$self->{'model'} || {}
$self->{'model'}
Truth table50%1/2
A
A= $self->{'model'}
| |||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 3 | for my $ns (@{$self->namespaces}) { | |||||||||||||||||||
| 39 | 3 | for my $name (find_modules $ns) { | |||||||||||||||||||
| 40 | 6 | my $e = load_class $name; | |||||||||||||||||||
| 41 | 6 | ▶ | next unless $name->isa('Mandel::Document'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | 3 | $name =~ s/^${ns}:://; | |||||||||||||||||||
| 43 | 3 | $names{Mojo::Util::decamelize($name)} = 1; | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 3 | keys %names; | |||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 5 | ▶ | sub class_for { | ||||||||||||||||||
|
Subroutine
class_for: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||
| 51 | 5 | my ($self, $name) = @_; | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 5 | ▶ | if (my $class = $self->{loaded}{$name}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | 2 | return $class; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 3 | for my $ns (@{$self->namespaces}) { | |||||||||||||||||||
| 58 | 3 | my $class = $ns . '::' . Mojo::Util::camelize($name); | |||||||||||||||||||
| 59 | 3 | my $e = load_class $class; | |||||||||||||||||||
| 60 | 3 | ▶ | die $e if ref $e; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | 3 | ▶ | next if $e; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | 3 | return $self->{loaded}{$name} = $class; | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | confess "Could not find class for $name"; | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 1 | ▶ | sub collection { | ||||||||||||||||||
|
Subroutine
collection: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 69 | 1 | my ($self, $name) = @_; | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 1 | $self->model($name)->new_collection($self); | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 22 | ▶ | sub model { | ||||||||||||||||||
|
Subroutine
model: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||
| 75 | 22 | my ($self, $name, $model) = @_; | |||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 22 | ▶ | if ($model) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | 10 | ▶ | $model = $self->model_class->new($model) if ref $model eq 'HASH'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 10 | $model->name($name); | |||||||||||||||||||
| 80 | 10 | $self->{loaded}{$name} = $model->document_class; | |||||||||||||||||||
| 81 | 10 | $self->{model}{$name} = $model; | |||||||||||||||||||
| 82 | 10 | return $self; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | else { | ||||||||||||||||||||
| 85 | 12 | ▶ | return $self->{model}{$name} ||= $self->class_for($name)->model; | ||||||||||||||||||
|
Condition66%2/3
$self->{"model"}{$name} ||= $self->class_for($name)->model
MC/DC0%0/2
$self->{"model"}{$name} ||= $self->class_for($name)->model
$self->{"model"}{$name}
$self->class_for($name)->model
Truth table66%2/3
A ||= B
A= $self->{"model"}{$name}B= $self->class_for($name)->model
| |||||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 3 | ▶ | sub initialize { | ||||||||||||||||||
|
Subroutine
initialize: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 90 | 3 | ▶ | my $args = ref $_[-1] eq 'HASH' ? pop : {}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | 3 | my $self = shift; | |||||||||||||||||||
| 92 | 3 | ▶ | my @documents = @_ ? @_ : $self->all_document_names; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 3 | for my $document (@documents) { | |||||||||||||||||||
| 95 | 3 | my $class = $self->class_for($document); | |||||||||||||||||||
| 96 | 3 | $class->initialize($self, $args); | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 0 | ▶ | sub _storage_collection { | ||||||||||||||||||
|
Subroutine
_storage_collection: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 101 | 0 | $_[0]->storage->db->collection($_[1]); | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 18 | 1; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | =encoding utf8 | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | =head1 NAME | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | Mandel - Async model layer for MongoDB objects using Mango | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | =head1 VERSION | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 0.31 | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | =head1 SYNOPSIS | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | # create your custom model class | ||||||||||||||||||||
| 119 | package MyModel; | ||||||||||||||||||||
| 120 | use Mojo::Base "Mandel"; | ||||||||||||||||||||
| 121 | 1; | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | # create a document class | ||||||||||||||||||||
| 124 | package MyModel::Cat; | ||||||||||||||||||||
| 125 | use Mandel::Document; | ||||||||||||||||||||
| 126 | use Types::Standard 'Str'; | ||||||||||||||||||||
| 127 | field name => ( isa => Str, builder => sub { "value" } ); | ||||||||||||||||||||
| 128 | field 'type'; | ||||||||||||||||||||
| 129 | belongs_to person => 'MyModel::Person'; | ||||||||||||||||||||
| 130 | 1; | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | # create another document class | ||||||||||||||||||||
| 133 | package MyModel::Person; | ||||||||||||||||||||
| 134 | use Mandel::Document; | ||||||||||||||||||||
| 135 | use Types::Standard 'Int'; | ||||||||||||||||||||
| 136 | field [qw(name)]; | ||||||||||||||||||||
| 137 | field age => ( isa => Int ); | ||||||||||||||||||||
| 138 | has_many cats => 'MyModel::Cat'; | ||||||||||||||||||||
| 139 | has_one favorite_cat => 'MyModel::Cat'; | ||||||||||||||||||||
| 140 | 1; | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | # use the model in your application | ||||||||||||||||||||
| 143 | package main; | ||||||||||||||||||||
| 144 | my $connection = MyModel->connect("mongodb://localhost/my_db"); | ||||||||||||||||||||
| 145 | my $persons = $connection->collection('person'); | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | my $p1 = $persons->create({ name => 'Bruce', age => 30 }); | ||||||||||||||||||||
| 148 | $p1->save(sub { | ||||||||||||||||||||
| 149 | my($p1, $err) = @_; | ||||||||||||||||||||
| 150 | }); | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | $persons->count(sub { | ||||||||||||||||||||
| 153 | my($persons, $err, $n_persons) = @_; | ||||||||||||||||||||
| 154 | }); | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | $persons->all(sub { | ||||||||||||||||||||
| 157 | my($persons, $err, $objs) = @_; | ||||||||||||||||||||
| 158 | for my $p (@$objs) { | ||||||||||||||||||||
| 159 | $p->age(25)->save(sub {}); | ||||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | }); | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | $persons->search({ name => 'Bruce' })->single(sub { | ||||||||||||||||||||
| 164 | my($persons, $err, $person) = @_; | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | $person->cats(sub { | ||||||||||||||||||||
| 167 | my($person, $err, $cats) = @_; | ||||||||||||||||||||
| 168 | $_->remove(sub {}) for @$cats; | ||||||||||||||||||||
| 169 | }); | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | $person->remove(sub { | ||||||||||||||||||||
| 172 | my($person, $err) = @_; | ||||||||||||||||||||
| 173 | }); | ||||||||||||||||||||
| 174 | }); | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =head1 DESCRIPTION | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | THIS IS ALPHA SOFTWARE! THE API MAY BE CHANGED AT ANY TIME! | ||||||||||||||||||||
| 179 | PLEASE CONTACT ME IF YOU HAVE ANY COMMENTS OR FEEDBACK. | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | L<Mandel> is an async object-document-mapper. It allows you to work with your | ||||||||||||||||||||
| 182 | MongoDB documents in Perl as objects. | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | This class binds it all together: | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | =over 4 | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | =item * L<Mandel::Model> | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | An object modelling a document. | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | =item * L<Mandel::Collection> | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | A collection of Mandel documents. | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | =item * L<Mandel::Document> | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | A single MongoDB document with logic. | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | =back | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | =head1 ATTRIBUTES | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | L<Mandel> inherits all attributes from L<Mojo::Base> and implements the | ||||||||||||||||||||
| 205 | following new ones. | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | =head2 namespaces | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | The namespaces which will be searched when looking for Types. By default, the | ||||||||||||||||||||
| 210 | (sub)class name of this module. | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | =head2 model_class | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | Returns L<Mandel::Model>. | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | =head2 storage | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | An instance of L<Mango> which acts as the database connection. If not | ||||||||||||||||||||
| 219 | provided. | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | =head1 METHODS | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | L<Mandel> inherits all methods from L<Mojo::Base> and implements the following | ||||||||||||||||||||
| 224 | new ones. | ||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | =head2 connect | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | $self = $class->connect(@connect_args); | ||||||||||||||||||||
| 229 | $clone = $self->connect(@connect_args); | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | C<@connect_args> will be passed on to L<Mango/new>, which again will be set | ||||||||||||||||||||
| 232 | as L</storage>. | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | Calling this on an object will return a clone, but with a fresh L</storage> | ||||||||||||||||||||
| 235 | object. | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | =head2 all_document_names | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | @names = $self->all_document_names; | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | Returns a list of all the documents in the L</namespaces>. | ||||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | =head2 class_for | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | $document_class = $self->class_for($name); | ||||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | Given a document name, find the related class name, ensure that it is loaded | ||||||||||||||||||||
| 248 | (or else die) and return it. | ||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | =head2 collection | ||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | $collection_obj = $self->collection($name); | ||||||||||||||||||||
| 253 | |||||||||||||||||||||
| 254 | Returns a L<Mango::Collection> object. | ||||||||||||||||||||
| 255 | |||||||||||||||||||||
| 256 | =head2 model | ||||||||||||||||||||
| 257 | |||||||||||||||||||||
| 258 | $model = $self->model($name); | ||||||||||||||||||||
| 259 | $self = $self->model($name => \%model_args); | ||||||||||||||||||||
| 260 | $self = $self->model($name => $model_obj); | ||||||||||||||||||||
| 261 | |||||||||||||||||||||
| 262 | Define or returns a L<Mandel::Model> object. Will die unless a model is | ||||||||||||||||||||
| 263 | registered by that name or L</class_for> returns a class which has the | ||||||||||||||||||||
| 264 | C<model()> method defined. | ||||||||||||||||||||
| 265 | |||||||||||||||||||||
| 266 | =head2 initialize | ||||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | $self->initialize(@names, \%args); | ||||||||||||||||||||
| 269 | $self->initialize(\%args); | ||||||||||||||||||||
| 270 | |||||||||||||||||||||
| 271 | Takes a list of document names. Calls the L<Mandel::Document/initialize> method | ||||||||||||||||||||
| 272 | on any document given as input. C<@names> default to L</all_document_names> | ||||||||||||||||||||
| 273 | unless specified. | ||||||||||||||||||||
| 274 | |||||||||||||||||||||
| 275 | C<%args> defaults to empty hash ref, unless specified as input. | ||||||||||||||||||||
| 276 | |||||||||||||||||||||
| 277 | The C<initialize()> method will be called like this: | ||||||||||||||||||||
| 278 | |||||||||||||||||||||
| 279 | $document_class->initialize($self, \%args); | ||||||||||||||||||||
| 280 | |||||||||||||||||||||
| 281 | =head1 SEE ALSO | ||||||||||||||||||||
| 282 | |||||||||||||||||||||
| 283 | L<Mojolicious>, L<Mango> | ||||||||||||||||||||
| 284 | |||||||||||||||||||||
| 285 | Still got MongoDB 2.4 on Ubuntu? Check out | ||||||||||||||||||||
| 286 | L<http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/> | ||||||||||||||||||||
| 287 | to upgrade. | ||||||||||||||||||||
| 288 | |||||||||||||||||||||
| 289 | =head1 SOURCE REPOSITORY | ||||||||||||||||||||
| 290 | |||||||||||||||||||||
| 291 | L<http://github.com/jhthorsen/mandel> | ||||||||||||||||||||
| 292 | |||||||||||||||||||||
| 293 | =head1 AUTHORS | ||||||||||||||||||||
| 294 | |||||||||||||||||||||
| 295 | Jan Henning Thorsen - C<jhthorsen@cpan.org> | ||||||||||||||||||||
| 296 | |||||||||||||||||||||
| 297 | Joel Berger - C<joel.a.berger@gmail.com> | ||||||||||||||||||||
| 298 | |||||||||||||||||||||
| 299 | Holger Rupprecht - C<Holger.Rupprecht@Itelligence.de> | ||||||||||||||||||||
| 300 | |||||||||||||||||||||
| 301 | Huo Linhe - C<zitsen@cpan.org> | ||||||||||||||||||||
| 302 | |||||||||||||||||||||
| 303 | This project is a fork of L<MangoModel|http://github.com/jberger/MangoModel>, | ||||||||||||||||||||
| 304 | created by Joel Berger, C<joel.a.berger@gmail.com>. | ||||||||||||||||||||
| 305 | |||||||||||||||||||||
| 306 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 307 | |||||||||||||||||||||
| 308 | Copyright (C) 2013 by Jan Henning Thorsen | ||||||||||||||||||||
| 309 | |||||||||||||||||||||
| 310 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||
| 311 | it under the same terms as Perl itself. | ||||||||||||||||||||
| 312 | |||||||||||||||||||||
| 313 | =cut | ||||||||||||||||||||