| 1 | package DBIx::NoSQL::Store::Manager::Model; | ||||||||||||
| 2 | BEGIN { | ||||||||||||
| 3 | 1 | ▶ | $DBIx::NoSQL::Store::Manager::Model::AUTHORITY = 'cpan:YANICK'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | } | ||||||||||||
| 5 | { | ||||||||||||
| 6 | 1 | $DBIx::NoSQL::Store::Manager::Model::VERSION = '0.2.2'; | |||||||||||
| 7 | } | ||||||||||||
| 8 | #ABSTRACT: Role for classes to be handled by DBIx::NoSQL::Store::Manager | ||||||||||||
| 9 | |||||||||||||
| 10 | 1 | ▶ | use 5.10.0; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | 1 | ▶ | use Moose::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | |||||||||||||
| 17 | 1 | ▶ | use Method::Signatures; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 1 | ▶ | use MooseX::ClassAttribute; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 1 | ▶ | use MooseX::Storage 0.31; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | |||||||||||||
| 21 | 1 | with Storage; | |||||||||||
| 22 | 1 | with 'DBIx::NoSQL::Store::Manager::StoreKey', | |||||||||||
| 23 | 'DBIx::NoSQL::Store::Manager::StoreIndex'; | ||||||||||||
| 24 | |||||||||||||
| 25 | |||||||||||||
| 26 | |||||||||||||
| 27 | |||||||||||||
| 28 | 1 | has store_db => ( | |||||||||||
| 29 | traits => [ 'DoNotSerialize' ], | ||||||||||||
| 30 | is => 'ro', | ||||||||||||
| 31 | required => 1, | ||||||||||||
| 32 | ); | ||||||||||||
| 33 | |||||||||||||
| 34 | |||||||||||||
| 35 | 0 | ▶ | class_has store_model => ( | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||
| 36 | isa => 'Str', | ||||||||||||
| 37 | 0 | is => 'rw', | |||||||||||
| 38 | default => method { | ||||||||||||
| 39 | 0 | # TODO probably over-complicated | |||||||||||
| 40 | 0 | my( $class ) = $self->class_precedence_list; | |||||||||||
| 41 | 0 | ||||||||||||
| 42 | $class =~ s/^.*::Model:://; | ||||||||||||
| 43 | 1 | $class =~ s/::/_/g; | |||||||||||
| 44 | return $class; | ||||||||||||
| 45 | }, | ||||||||||||
| 46 | 0 | ▶ | ); | ||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
| |||||||||||||
| 47 | |||||||||||||
| 48 | 0 | ||||||||||||
| 49 | 0 | has store_key => ( | |||||||||||
| 50 | 0 | ▶ | traits => [ 'DoNotSerialize' ], | ||||||||||
|
Condition0%0/2
join("-", map({my $m = $_->get_read_method; // die("no store key set for $self")
MC/DC0%0/1
join("-", map({my $m = $_->get_read_method;
$self->$m;} grep({$_->does("DBIx::NoSQL::Store::Manager::StoreKey");} $self->meta->get_all_attributes))) // die("no store key set for $self")
join("-", map({my $m = $_->get_read_method;
$self->$m;} grep({$_->does("DBIx::NoSQL::Store::Manager::StoreKey");} $self->meta->get_all_attributes)))
Truth table0%0/2
A
A= join("-", map({my $m = $_->get_read_method;
$self->$m;} grep({$_->does("DBIx::NoSQL::Store::Manager::StoreKey");} $self->meta->get_all_attributes)))
| |||||||||||||
| 51 | is => 'ro', | ||||||||||||
| 52 | lazy => 1, | ||||||||||||
| 53 | default => method { | ||||||||||||
| 54 | 1 | return join( '-', map { | |||||||||||
| 55 | my $m = $_->get_read_method; | ||||||||||||
| 56 | $self->$m; | ||||||||||||
| 57 | 1 | ▶ | } grep { $_->does('DBIx::NoSQL::Store::Manager::StoreKey') } | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
Subroutine
store: not called CC=2 SCAR=12.4
Branch: 1 branch
| |||||||||||||
| 58 | 0 | $self->meta->get_all_attributes ) | |||||||||||
| 59 | // die "no store key set for $self"; | ||||||||||||
| 60 | }, | ||||||||||||
| 61 | ); | ||||||||||||
| 62 | |||||||||||||
| 63 | |||||||||||||
| 64 | method store { | ||||||||||||
| 65 | 1 | ▶ | $self->store_db->set( | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
Subroutine
delete: not called CC=2 SCAR=12.4
Branch: 1 branch
| |||||||||||||
| 66 | 0 | $self->store_model => | |||||||||||
| 67 | $self->store_key => $self, | ||||||||||||
| 68 | ); | ||||||||||||
| 69 | 1 | ▶ | } | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
Subroutine
_entity: not called CC=2 SCAR=12.4
Branch: 1 branch
| |||||||||||||
| 70 | 0 | ||||||||||||
| 71 | |||||||||||||
| 72 | method delete { | ||||||||||||
| 73 | 1 | ▶ | $self->store_db->delete( $self->store_model => $self->store_key ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
Subroutine
indexes: not called CC=2 SCAR=13.6
Branch: 1 branch
| |||||||||||||
| 74 | 0 | } | |||||||||||
| 75 | 0 | ||||||||||||
| 76 | method _entity { | ||||||||||||
| 77 | return $self->pack; | ||||||||||||
| 78 | } | ||||||||||||
| 79 | 1 | ||||||||||||
| 80 | method indexes { | ||||||||||||
| 81 | return map { [ $_->name, ( isa => $_->store_isa ) x $_->has_store_isa ] } | ||||||||||||
| 82 | grep { $_->does('DBIx::NoSQL::Store::Manager::StoreIndex') } | ||||||||||||
| 83 | $self->meta->get_all_attributes; | ||||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | 1; | ||||||||||||
| 87 | |||||||||||||
| 88 | __END__ | ||||||||||||
| 89 | |||||||||||||
| 90 | =pod | ||||||||||||
| 91 | |||||||||||||
| 92 | =head1 NAME | ||||||||||||
| 93 | |||||||||||||
| 94 | DBIx::NoSQL::Store::Manager::Model - Role for classes to be handled by DBIx::NoSQL::Store::Manager | ||||||||||||
| 95 | |||||||||||||
| 96 | =head1 VERSION | ||||||||||||
| 97 | |||||||||||||
| 98 | version 0.2.2 | ||||||||||||
| 99 | |||||||||||||
| 100 | =head1 SYNOPSIS | ||||||||||||
| 101 | |||||||||||||
| 102 | package MyComics::Model::Comic; | ||||||||||||
| 103 | |||||||||||||
| 104 | use strict; | ||||||||||||
| 105 | use warnings; | ||||||||||||
| 106 | |||||||||||||
| 107 | use Moose; | ||||||||||||
| 108 | |||||||||||||
| 109 | with 'DBIx::NoSQL::Store::Manager::Model'; | ||||||||||||
| 110 | |||||||||||||
| 111 | has series => ( | ||||||||||||
| 112 | traits => [ 'StoreKey' ], | ||||||||||||
| 113 | is => 'ro', | ||||||||||||
| 114 | ); | ||||||||||||
| 115 | |||||||||||||
| 116 | has issue => ( | ||||||||||||
| 117 | traits => [ 'StoreKey' ], | ||||||||||||
| 118 | is => 'ro', | ||||||||||||
| 119 | isa => 'Int', | ||||||||||||
| 120 | ); | ||||||||||||
| 121 | |||||||||||||
| 122 | has penciller => ( | ||||||||||||
| 123 | traits => [ 'StoreIndex' ], | ||||||||||||
| 124 | is => 'ro', | ||||||||||||
| 125 | ); | ||||||||||||
| 126 | |||||||||||||
| 127 | has writer => ( | ||||||||||||
| 128 | is => 'ro', | ||||||||||||
| 129 | ); | ||||||||||||
| 130 | |||||||||||||
| 131 | __PACKAGE__->meta->make_immutable; | ||||||||||||
| 132 | |||||||||||||
| 133 | 1; | ||||||||||||
| 134 | |||||||||||||
| 135 | =head1 DESCRIPTION | ||||||||||||
| 136 | |||||||||||||
| 137 | Role for classes to be stashed in a L<DBIx::NoSQL::Store::Manager> store. | ||||||||||||
| 138 | |||||||||||||
| 139 | The only hard-requirement for a class consuming this role is to define a | ||||||||||||
| 140 | key to be used as the unique id of the object in the store. This can be done | ||||||||||||
| 141 | by applying the L<DBIx::NoSQL::Store::Manager::StoreIndex> trait to one or | ||||||||||||
| 142 | more attributes (the key will be the concatenation of those attributes). Or, | ||||||||||||
| 143 | if the generation of the key is more complicated, it can be done by playing | ||||||||||||
| 144 | with the C<store_key> attribute directly: | ||||||||||||
| 145 | |||||||||||||
| 146 | has '+store_key' => ( | ||||||||||||
| 147 | default => sub { | ||||||||||||
| 148 | my $self = shift; | ||||||||||||
| 149 | |||||||||||||
| 150 | return $self->generate_arcane_key; | ||||||||||||
| 151 | }, | ||||||||||||
| 152 | ); | ||||||||||||
| 153 | |||||||||||||
| 154 | Attributes of the class can be marked for indexed by giving them | ||||||||||||
| 155 | the L<DBIx::NoSQL::Store::Manager::StoreIndex> trait. | ||||||||||||
| 156 | |||||||||||||
| 157 | =head1 ATTRIBUTES | ||||||||||||
| 158 | |||||||||||||
| 159 | =head2 store_db | ||||||||||||
| 160 | |||||||||||||
| 161 | The L<DBIx::NoSQL::Store::Manager> store to which the object belongs | ||||||||||||
| 162 | to. Required. | ||||||||||||
| 163 | |||||||||||||
| 164 | =head2 store_model | ||||||||||||
| 165 | |||||||||||||
| 166 | Class-level attribute holding the model name of the class. | ||||||||||||
| 167 | If not given, defaults to the class name with everything up | ||||||||||||
| 168 | to a C<*::Model::> truncated (e.g., C<MyStore::Model::Thingy> | ||||||||||||
| 169 | would become C<Thingy>). | ||||||||||||
| 170 | |||||||||||||
| 171 | Not that as it's a class-level attribute, it can't be passed to | ||||||||||||
| 172 | C<new()>, but has to be set via C<class_has>: | ||||||||||||
| 173 | |||||||||||||
| 174 | class_has +store_model => ( | ||||||||||||
| 175 | default => 'SomethingElse', | ||||||||||||
| 176 | ); | ||||||||||||
| 177 | |||||||||||||
| 178 | =head2 store_key | ||||||||||||
| 179 | |||||||||||||
| 180 | The store id of the object. Defaults to the concatenation of the value of all | ||||||||||||
| 181 | attributes having the L<DBIx::NoSQL::Store::Manager::StoreKey> trait. | ||||||||||||
| 182 | |||||||||||||
| 183 | =head1 METHODS | ||||||||||||
| 184 | |||||||||||||
| 185 | =head2 store_db | ||||||||||||
| 186 | |||||||||||||
| 187 | Returns the L<DBIx::NoSQL::Store::Manager> store to which the object belongs | ||||||||||||
| 188 | to. | ||||||||||||
| 189 | |||||||||||||
| 190 | =head2 store() | ||||||||||||
| 191 | |||||||||||||
| 192 | Serializes the object into the store. | ||||||||||||
| 193 | |||||||||||||
| 194 | =head2 delete() | ||||||||||||
| 195 | |||||||||||||
| 196 | Deletes the object from the store. | ||||||||||||
| 197 | |||||||||||||
| 198 | =head1 AUTHOR | ||||||||||||
| 199 | |||||||||||||
| 200 | Yanick Champoux <yanick@babyl.dyndns.org> | ||||||||||||
| 201 | |||||||||||||
| 202 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 203 | |||||||||||||
| 204 | This software is copyright (c) 2012 by Yanick Champoux. | ||||||||||||
| 205 | |||||||||||||
| 206 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 207 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 208 | |||||||||||||
| 209 | =cut | ||||||||||||