« blib/lib/DBIx/NoSQL/Store/Manager/Model.pm
↑ Summary
blib/lib/DBIx/NoSQL/Store/Manager/StoreIndex.pm »
| 1 | package DBIx::NoSQL::Store::Manager; | ||||||||||||||
| 2 | BEGIN { | ||||||||||||||
| 3 | 2 | ▶ | $DBIx::NoSQL::Store::Manager::AUTHORITY = 'cpan:YANICK'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | } | ||||||||||||||
| 5 | { | ||||||||||||||
| 6 | 2 | $DBIx::NoSQL::Store::Manager::VERSION = '0.2.2'; | |||||||||||||
| 7 | } | ||||||||||||||
| 8 | #ABSTRACT: DBIx::NoSQL as a Moose object store | ||||||||||||||
| 9 | |||||||||||||||
| 10 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 2 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 2 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | 2 | ▶ | use DBIx::NoSQL 0.0020; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | 2 | ▶ | use Method::Signatures; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 2 | ▶ | use Module::Pluggable require => 1; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | |||||||||||||||
| 21 | 2 | extends 'DBIx::NoSQL::Store'; | |||||||||||||
| 22 | |||||||||||||||
| 23 | |||||||||||||||
| 24 | 2 | subtype Model | |||||||||||||
| 25 | => as 'ArrayRef[Str]'; | ||||||||||||||
| 26 | |||||||||||||||
| 27 | coerce Model | ||||||||||||||
| 28 | => from 'Str' | ||||||||||||||
| 29 | 0 | ▶ | => via { [ $_ ] }; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||
| 30 | |||||||||||||||
| 31 | 2 | ▶ | has models => ( | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
| |||||||||||||||
| 32 | 2 | traits => [ 'Array' ], | |||||||||||||
| 33 | is => 'ro', | ||||||||||||||
| 34 | 2 | isa => 'Model', | |||||||||||||
| 35 | default => method { | ||||||||||||||
| 36 | [ join "::", ($self->meta->class_precedence_list)[0], 'Model', '' ]; | ||||||||||||||
| 37 | }, | ||||||||||||||
| 38 | handles => { | ||||||||||||||
| 39 | arg_models => 'elements', | ||||||||||||||
| 40 | 2 | }, | |||||||||||||
| 41 | ); | ||||||||||||||
| 42 | |||||||||||||||
| 43 | |||||||||||||||
| 44 | has _models => ( | ||||||||||||||
| 45 | traits => [ 'Hash' ], | ||||||||||||||
| 46 | is => 'ro', | ||||||||||||||
| 47 | isa => 'HashRef', | ||||||||||||||
| 48 | handles => { | ||||||||||||||
| 49 | model_names => 'keys', | ||||||||||||||
| 50 | model_classes => 'values', | ||||||||||||||
| 51 | model_class => 'get', | ||||||||||||||
| 52 | 2 | ▶ | _set_model => 'set', | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
_register_models: called CC=5 SCAR=22.5
| |||||||||||||||
| 53 | }, | ||||||||||||||
| 54 | ); | ||||||||||||||
| 55 | 2 | ▶ | |||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | 2 | method _register_models( @models ) { | |||||||||||||
| 57 | 2 | # expand namespaces into their plugins | |||||||||||||
| 58 | @models = map { | ||||||||||||||
| 59 | s/::$// ? do { | ||||||||||||||
| 60 | $self->search_path( new => $_ ); | ||||||||||||||
| 61 | 2 | $self->plugins } : $_ | |||||||||||||
| 62 | 0 | ▶ | } @models; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | 0 | ||||||||||||||
| 64 | |||||||||||||||
| 65 | 0 | for my $model ( @models ) { | |||||||||||||
| 66 | eval "use $model; 1" or die "couldn't load '$_': $@\n"; | ||||||||||||||
| 67 | $self->_set_model( $model->store_model => $model ); | ||||||||||||||
| 68 | |||||||||||||||
| 69 | 0 | ▶ | my $store_model = $self->model($model->store_model); | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 70 | 0 | ▶ | |||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | 0 | ||||||||||||||
| 72 | 0 | $store_model->_wrap( sub { | |||||||||||||
| 73 | my $ref = shift; | ||||||||||||||
| 74 | 0 | $ref = $ref->[0] if ref($ref) eq 'ARRAY'; | |||||||||||||
| 75 | $model->unpack($ref, inject => { store_db => $self } ); | ||||||||||||||
| 76 | }); | ||||||||||||||
| 77 | |||||||||||||||
| 78 | 2 | ▶ | $store_model->index(@$_) for $model->indexes; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
BUILD: called CC=4 SCAR=14.2
Branch: 2 branches
| |||||||||||||||
| 79 | } | ||||||||||||||
| 80 | 2 | ▶ | } | ||||||||||||
|
Condition50%1/2
$args->{"models"} ||= [join("::", ($self->meta->class_precedence_list)[0], "Model", "")]
MC/DC0%0/1
$args->{"models"} ||= [join("::", ($self->meta->class_precedence_list)[0], "Model", "")]
$args->{"models"}
Truth table50%1/2
A
A= $args->{"models"}
| |||||||||||||||
| 81 | |||||||||||||||
| 82 | method BUILD($args) { | ||||||||||||||
| 83 | 2 | $args->{models} ||= [ | |||||||||||||
| 84 | join "::", ($self->meta->class_precedence_list)[0], 'Model', '' | ||||||||||||||
| 85 | ]; | ||||||||||||||
| 86 | |||||||||||||||
| 87 | 2 | ▶ | $self->_register_models( @{ $args->{models} } ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.0
Subroutine
new_model_object: not called CC=1 SCAR=2.0
| |||||||||||||||
| 88 | }; | ||||||||||||||
| 89 | 2 | ▶ | |||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
create: called CC=2 SCAR=7.0
Branch: 1 branch
| |||||||||||||||
| 90 | 1 | ||||||||||||||
| 91 | method new_model_object(@args) { $self->create(@args) } | ||||||||||||||
| 92 | |||||||||||||||
| 93 | 2 | method create ( $model, @args ) { | |||||||||||||
| 94 | $self->model_class($model)->new( store_db => $self, @args); | ||||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 1; | ||||||||||||||
| 98 | |||||||||||||||
| 99 | __END__ | ||||||||||||||
| 100 | |||||||||||||||
| 101 | =pod | ||||||||||||||
| 102 | |||||||||||||||
| 103 | =head1 NAME | ||||||||||||||
| 104 | |||||||||||||||
| 105 | DBIx::NoSQL::Store::Manager - DBIx::NoSQL as a Moose object store | ||||||||||||||
| 106 | |||||||||||||||
| 107 | =head1 VERSION | ||||||||||||||
| 108 | |||||||||||||||
| 109 | version 0.2.2 | ||||||||||||||
| 110 | |||||||||||||||
| 111 | =head1 SYNOPSIS | ||||||||||||||
| 112 | |||||||||||||||
| 113 | package MyStore; | ||||||||||||||
| 114 | |||||||||||||||
| 115 | use Moose; | ||||||||||||||
| 116 | |||||||||||||||
| 117 | extends 'DBIx::NoSQL::Store::Manager'; | ||||||||||||||
| 118 | |||||||||||||||
| 119 | __PACKAGE__->meta->make_immutable; | ||||||||||||||
| 120 | |||||||||||||||
| 121 | =head1 DESCRIPTION | ||||||||||||||
| 122 | |||||||||||||||
| 123 | Just like L<DBIx::NoSQL> is a layer providing the | ||||||||||||||
| 124 | flexibility of a NoSQL store on top of L<DBIx::Class>, I<DBIx::NoSQL::Store::Manager> | ||||||||||||||
| 125 | provides a mechanism to drop and retrieve L<Moose> objects from that store. | ||||||||||||||
| 126 | |||||||||||||||
| 127 | As can be seen in the L</SYNOPSIS>, the store class itself is typically fairly | ||||||||||||||
| 128 | bare; most of the work is done by L<DBIx::NoSQL::Store::Manager::Model>, the | ||||||||||||||
| 129 | role the models (i.e., the classes to be stored in the database) must consume. | ||||||||||||||
| 130 | |||||||||||||||
| 131 | I<DBIx::NoSQL::Store::Manager> extends L<DBIx::NoSQL> and inherits all its | ||||||||||||||
| 132 | methods. | ||||||||||||||
| 133 | |||||||||||||||
| 134 | =head1 METHODS | ||||||||||||||
| 135 | |||||||||||||||
| 136 | =head2 new( models => \@classes ) | ||||||||||||||
| 137 | |||||||||||||||
| 138 | Creates a new store manager. | ||||||||||||||
| 139 | |||||||||||||||
| 140 | =head3 Arguments | ||||||||||||||
| 141 | |||||||||||||||
| 142 | =over | ||||||||||||||
| 143 | |||||||||||||||
| 144 | =item models => \@classes | ||||||||||||||
| 145 | |||||||||||||||
| 146 | =item models => $class | ||||||||||||||
| 147 | |||||||||||||||
| 148 | Classes to be imported as models for the store. Namespaces can also be given | ||||||||||||||
| 149 | with a trailing C<::>, in which case all modules found under that namespace | ||||||||||||||
| 150 | will be imported. If only one class is to be used, it can be passed as a | ||||||||||||||
| 151 | single string. | ||||||||||||||
| 152 | |||||||||||||||
| 153 | If not given, defaults | ||||||||||||||
| 154 | to the C<Model> sub-namespace under the store's (e.g., for store | ||||||||||||||
| 155 | class C<MyStore>, that would be C<MyStore::Model::>). | ||||||||||||||
| 156 | |||||||||||||||
| 157 | my $store = MyStore->new; | ||||||||||||||
| 158 | # will import MyStore::Model::* | ||||||||||||||
| 159 | |||||||||||||||
| 160 | my $store = MyStore->new( models => [ 'Foo::Bar', 'Something::Else' ] ); | ||||||||||||||
| 161 | # imports specific classes | ||||||||||||||
| 162 | |||||||||||||||
| 163 | my $store = MyStore->new( models => [ 'Foo::Bar', 'MyStore::Model::' ] ); | ||||||||||||||
| 164 | # imports Foo::Bar and all classes under MyStore::Model::* | ||||||||||||||
| 165 | |||||||||||||||
| 166 | =back | ||||||||||||||
| 167 | |||||||||||||||
| 168 | =head2 model_names() | ||||||||||||||
| 169 | |||||||||||||||
| 170 | Returns the name of all models known to the store. | ||||||||||||||
| 171 | |||||||||||||||
| 172 | =head2 model_classes() | ||||||||||||||
| 173 | |||||||||||||||
| 174 | Returns the full class name of all models known to the store. | ||||||||||||||
| 175 | |||||||||||||||
| 176 | =head2 model_class( $name ) | ||||||||||||||
| 177 | |||||||||||||||
| 178 | Returns the full class name of the given model. | ||||||||||||||
| 179 | |||||||||||||||
| 180 | =head2 create( $model_name, @args ) | ||||||||||||||
| 181 | |||||||||||||||
| 182 | =head2 new_model_object( $model_name, @args ) | ||||||||||||||
| 183 | |||||||||||||||
| 184 | Shortcut constructor for a model class of the store. Equivalent to | ||||||||||||||
| 185 | |||||||||||||||
| 186 | my $class = $store->model_class( $model_name ); | ||||||||||||||
| 187 | my $thingy = $class->new( store_db => $store, @args ); | ||||||||||||||
| 188 | |||||||||||||||
| 189 | =head1 SEE ALSO | ||||||||||||||
| 190 | |||||||||||||||
| 191 | * Original blog entry introducing the module: L<http://babyl.dyndns.org/techblog/entry/shaving-the-white-whale> | ||||||||||||||
| 192 | |||||||||||||||
| 193 | =head2 Similar Modules | ||||||||||||||
| 194 | |||||||||||||||
| 195 | * L<KiokuDB> | ||||||||||||||
| 196 | |||||||||||||||
| 197 | * L<Elastic::Model> | ||||||||||||||
| 198 | |||||||||||||||
| 199 | =head1 AUTHOR | ||||||||||||||
| 200 | |||||||||||||||
| 201 | Yanick Champoux <yanick@babyl.dyndns.org> | ||||||||||||||
| 202 | |||||||||||||||
| 203 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 204 | |||||||||||||||
| 205 | This software is copyright (c) 2012 by Yanick Champoux. | ||||||||||||||
| 206 | |||||||||||||||
| 207 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 208 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 209 | |||||||||||||||
| 210 | =cut | ||||||||||||||