File Coverage

blib/lib/Teng/Plugin/TmpSuppressRowObjects.pm
Criterion Covered Total %
statement 14 14 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 19 19 100.0


line stmt bran cond sub pod time code
1             package Teng::Plugin::TmpSuppressRowObjects;
2 8     8   523247 use 5.008001;
  8         54  
3 8     8   40 use strict;
  8         21  
  8         167  
4 8     8   35 use warnings;
  8         17  
  8         706  
5              
6             our $VERSION = "0.02";
7              
8             our @EXPORT;
9              
10             {
11             my @origs = qw(
12             insert
13             search
14             single
15             search_by_sql
16             single_by_sql
17             search_named
18             single_named
19             );
20             my $suffix = '_hashref';
21              
22 8     8   57 no strict 'refs';
  8         18  
  8         1181  
23             for my $orig (@origs) {
24             my $method = $orig . $suffix;
25             push @EXPORT, $method;
26             *{__PACKAGE__ . '::' . $method} = sub {
27 7     7   150362 my $self = shift;
28 7         49 local $self->{suppress_row_objects} = 1;
29 7         89 $self->$orig(@_);
30             };
31             }
32             }
33              
34             1;
35             __END__
36              
37             =encoding utf-8
38              
39             =head1 NAME
40              
41             Teng::Plugin::TmpSuppressRowObjects - add methods with temporary use of suppress_row_objects
42              
43             =head1 SYNOPSIS
44              
45             # In your Model ...
46             package Your::Model;
47             use parent qw(Teng);
48              
49             __PACKAGE__->load_plugin('TmpSuppressRowObjects');
50              
51              
52             # In case suppress_row_objects = 0 ...
53             my $teng = Your::Model->new(dbh => $dbh, suppress_row_objects => 0);
54             my $row;
55              
56             # same usage with original 'search'
57             $row = $teng->search_hashref(test_table => +{ id => 100 }); # $row is hashref
58              
59             # does not affect original 'search'
60             $row = $teng->search(test_table => +{ id => 100 }); # $row is row object
61              
62              
63             =head1 DESCRIPTION
64              
65             This plugin adds some methods, which suppress generating row objects, even when C<suppress_row_objects> is 0.
66             It is helpful when we want to use row objects in default, but temporarily use hashref to improve performance.
67              
68              
69             =head1 METHODS
70              
71             insert_hashref
72             search_hashref
73             single_hashref
74             search_by_sql_hashref
75             single_by_sql_hashref
76             search_named_hashref
77             single_named_hashref
78              
79             Usage of those methods are the same to original methods (without C<_hashref>).
80              
81              
82             =head1 LICENSE
83              
84             Copyright (C) egawata.
85              
86             This library is free software; you can redistribute it and/or modify
87             it under the same terms as Perl itself.
88              
89             =head1 AUTHOR
90              
91             egawata E<lt>egawa.takashi@gmail.comE<gt>
92              
93             =cut
94