NinjaORM/StaticClassInfo.pm

filter: Statementstmt 100% 28 / 28 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 100% 47 / 47 CC 6 SCAR 8.7
CC
6
Cov
100%
set
4.0
get
3.0
BEGIN
1.0
CRAP 2.4
« NinjaORM/Schema/Table.pm ↑ Summary NinjaORM/Utils.pm »
1 package DBIx::NinjaORM::StaticClassInfo;
2  
3 73 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 73 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 73 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 73 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 73 ▶use Data::Validate::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 =head1 NAME
13  
14 DBIx::NinjaORM::StaticClassInfo - Hold the configuration information for L<DBIX::NinjaORM> classes.
15  
16  
17 =head1 VERSION
18  
19 Version 3.1.0
20  
21 =cut
22  
23 73 our $VERSION = '3.1.0';
24  
25  
26 =head1 DESCRIPTION
27  
28 This package is used to store and retrieve defaults as well as general
29 information for a specific class. It allows for example indicating what table
30 the objects will be related to, or what database handle to use.
31  
32 Here's the full list of the options that can be set or overridden:
33  
34 =over 4
35  
36 =item * default_dbh
37  
38 The database handle to use when performing queries. The methods that interact
39 with the database always provide a C<dbh> argument to allow using a specific
40 database handle, but setting it here means you won't have to systematically
41 pass that argument.
42  
43 $info->{'default_dbh'} = DBI->connect(
44 "dbi:mysql:[database_name]:localhost:3306",
45 "[user]",
46 "[password]",
47 );
48  
49 =item * memcache
50  
51 Optionally, C<DBIx::NinjaORM> uses memcache to cache objects and queries,
52 in conjunction with the C<list_cache_time> and C<object_cache_time> arguments.
53  
54 If you want to enable the cache features, you can set this to a valid
55 C<Cache::Memcached> object (or a compatible module, such as
56 C<Cache::Memcached::Fast>).
57  
58 $info->{'memcache'} = Cache::Memcached::Fast->new(
59 {
60 servers =>
61 [
62 'localhost:11211',
63 ],
64 }
65 );
66  
67 =item * table_name
68  
69 Mandatory, the name of the table that this class will be the interface for.
70  
71 # Interface with a 'books' table.
72 $info->{'table_name'} = 'books';
73  
74 =item * primary_key_name
75  
76 The name of the primary key on the table specified with C<table_name>.
77  
78 $info->{'primary_key_name'} = 'book_id';
79  
80 =item * list_cache_time
81  
82 Control the list cache, which is an optional cache system in
83 C<retrieve_list()> to store how search criteria translate into object IDs.
84  
85 By default it is disabled (with C<undef>), but it is activated by setting it to
86 an integer that represents the cache time in seconds.
87  
88 # Cache for 10 seconds.
89 $info->{'list_cache_time'} = 10;
90  
91 # Don't cache.
92 $info->{'list_cache_time'} = undef;
93  
94 A good use case for this would be retrieving a list of books for a given author.
95 We would pass the author ID as a search criteria, and the resulting list of book
96 objects does not change often. Provided that you can tolerate a 1 hour delay
97 for a new book to show up associated with a given author, then it makes sense
98 to set the list_cache_time to 3600 and save most of the queries to find what
99 book otherwise belongs to the author.
100  
101 =item * object_cache_time
102  
103 Control the object cache, which is an optional cache system in
104 C<retrieve_list()> to store the objects returned and be able to look them up
105 by object ID.
106  
107 By default it is disabled (with C<undef>), but it is activated by setting it to
108 an integer that represents the cache time in seconds.
109  
110 # Cache for 10 seconds.
111 $info->{'object_cache_time'} = 10;
112  
113 # Don't cache.
114 $info->{'object_cache_time'} = undef;
115  
116 A good use case for this are objects that are expensive to build. You will see
117 more in C<retrieve_list()> on how to cache objects.
118  
119 =item * unique_fields
120  
121 The list of unique fields on the object.
122  
123 Note: L<DBIx::NinjaORM> does not support unique indexes made of more than one
124 field. If you add more than one field in this arrayref, the ORM will treat them
125 as separate unique indexes.
126  
127 # Declare books.isbn as unique.
128 $info->{'unique_fields'} = [ 'isbn' ];
129  
130 # Declare books.isbn and books.upc as unique.
131 $info->{'unique_fields'} = [ 'isbn', 'upc' ];
132  
133 =item * filtering_fields
134  
135 The list of fields that can be used to filter on in C<retrieve_list()>.
136  
137 # Allow filtering based on the book name and author ID.
138 $info->{'unique_fields'} = [ 'name', 'author_id' ];
139  
140 =item * readonly_fields
141  
142 The list of fields that cannot be set directly. They will be populated in
143 C<retrieve_list>, but you won't be able to insert / update / set them directly.
144  
145 =item * has_created_field
146  
147 Indicate whether the table has a field name C<created> to store the UNIX time
148 at which the row was created. Default: 1.
149  
150 # The table doesn't have a 'created' field.
151 $info->{'has_created_field'} = 0;
152  
153 =item * has_modified_field
154  
155 Indicate whether the table has a field name C<modified> to store the UNIX time
156 at which the row was modified. Default: 1.
157  
158 # The table doesn't have a 'modified' field.
159 $info->{'has_modified_field'} = 0;
160  
161 =item * cache_key_field
162  
163 By default, the object cache uses the primary key value to make cached objects
164 available to look up, but this allows specifying a different field for that
165 purpose.
166  
167 For example, you may want to use books.isbn instead of books.book_id to cache
168 objects:
169  
170 $info->{'cache_key_field'} = 'isbn';
171  
172 =item * verbose
173  
174 Add debugging and tracing information, 0 by default.
175  
176 # Show debugging information for operations on this class.
177 $info->{'verbose'} = 1;
178  
179 =item * verbose_cache_operations
180  
181 Add information in the logs regarding cache operations and uses.
182  
183 =back
184  
185  
186 =head1 SYNOPSIS
187  
188 my $static_class_info = $class->SUPER::static_class_info();
189  
190 # Set or override information.
191 $static_class_info->set(
192 {
193 table_name => 'books',
194 primary_key_name => 'book_id',
195 default_dbh => DBI->connect(
196 "dbi:mysql:[database_name]:localhost:3306",
197 "[user]",
198 "[password]",
199 ),
200 }
201 );
202  
203 # Retrieve information.
204 my $table_name = $static_class_info->get('table_name');
205  
206  
207 =head1 METHODS
208  
209 =head2 new()
210  
211 Create a new L<DBIx::NinjaORM::StaticClassInfo> object.
212  
213 my $static_class_info = DBIx::NinjaORM::StaticClassInfo->new();
214  
215 =cut
216  
217 sub new
218 82 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
219 82 my ( $class ) = @_;
220  
221 82 return bless(
222 {
223 'default_dbh' => undef,
224 'memcache' => undef,
225 'table_name' => undef,
226 'primary_key_name' => undef,
227 'list_cache_time' => undef,
228 'object_cache_time' => undef,
229 'unique_fields' => [],
230 'filtering_fields' => [],
231 'readonly_fields' => [],
232 'has_created_field' => 1,
233 'has_modified_field' => 1,
234 'cache_key_field' => undef,
235 'verbose' => 0,
236 'verbose_cache_operations' => 0,
237 },
238 $class,
239 );
240 }
241  
242  
243 =head2 get()
244  
245 Retrieve the value of one of the configuration variables.
246  
247 my $value = $static_class_info->get( $key );
248  
249 =cut
250  
251 sub get
252 965 ▶{
Subroutine get: called CC=3 SCAR=11.0
Pod documented
253 965 my ( $self, $key ) = @_;
254  
255 965 ▶ croak "The key name must be defined"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 1 964 965
256 if !defined( $key );
257 croak "The key '$key' is not valid"
258 964 ▶ if !exists( $self->{ $key } );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$key} 1 963 964
259  
260 963 return $self->{ $key };
261 }
262  
263 =head2 set()
264  
265 Set values for one or more configuration variables.
266  
267 $static_class_info->set(
268 {
269 unique_fields => [],
270 filtering_fields => [],
271 ...
272 }
273 );
274  
275 =cut
276  
277 sub set ## no critic (NamingConventions::ProhibitAmbiguousNames, Subroutines::RequireArgUnpacking)
278 64 ▶{
Subroutine set: called CC=4 SCAR=13.9
Pod documented
279 64 ▶ croak 'The first argument passed must be a hashref'
Branch: 1 branch
BranchTrueFalseTotal
unless Data::Validate::Type::is_hashref($_[1]) 2 62 64
280 if !Data::Validate::Type::is_hashref( $_[1] );
281  
282 62 my ( $self, $values ) = @_;
283  
284 62 foreach my $key ( keys %$values )
285 {
286 croak "The key '$key' is not valid"
287 257 ▶ if !exists( $self->{ $key } );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$key} 1 256 257
288  
289 256 $self->{ $key } = $values->{ $key };
290 }
291  
292 61 return;
293 }
294  
295  
296 =head1 BUGS
297  
298 Please report any bugs or feature requests through the web interface at
299 L<https://github.com/guillaumeaubert/DBIx-NinjaORM/issues/new>.
300 I will be notified, and then you'll automatically be notified of progress on
301 your bug as I make changes.
302  
303  
304 =head1 SUPPORT
305  
306 You can find documentation for this module with the perldoc command.
307  
308 perldoc DBIx::NinjaORM::StaticClassInfo
309  
310  
311 You can also look for information at:
312  
313 =over 4
314  
315 =item * GitHub's request tracker
316  
317 L<https://github.com/guillaumeaubert/DBIx-NinjaORM/issues>
318  
319 =item * AnnoCPAN: Annotated CPAN documentation
320  
321 L<http://annocpan.org/dist/DBIx-NinjaORM>
322  
323 =item * CPAN Ratings
324  
325 L<http://cpanratings.perl.org/d/DBIx-NinjaORM>
326  
327 =item * MetaCPAN
328  
329 L<https://metacpan.org/release/DBIx-NinjaORM>
330  
331 =back
332  
333  
334 =head1 AUTHOR
335  
336 Guillaume Aubert, C<< <aubertg at cpan.org> >>.
337  
338  
339 =head1 COPYRIGHT & LICENSE
340  
341 Copyright 2009-2017 Guillaume Aubert.
342  
343 This code is free software; you can redistribute it and/or modify it under the
344 same terms as Perl 5 itself.
345  
346 This program is distributed in the hope that it will be useful, but WITHOUT ANY
347 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
348 PARTICULAR PURPOSE. See the LICENSE file for more details.
349  
350 =cut
351  
352 73 1;
« NinjaORM/Schema/Table.pm ↑ Summary NinjaORM/Utils.pm »