File Coverage

blib/lib/Teng/Plugin/FindOrCreate.pm
Criterion Covered Total %
statement 13 13 100.0
branch 2 2 100.0
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 20 20 100.0


line stmt bran cond sub pod time code
1             package Teng::Plugin::FindOrCreate;
2 2     2   1537 use strict;
  2         4  
  2         65  
3 2     2   9 use warnings;
  2         4  
  2         50  
4 2     2   12 use utf8;
  2         4  
  2         10  
5              
6             our @EXPORT = qw/find_or_create/;
7              
8             sub find_or_create {
9 5     5 1 5535 my ($self, $table, $args) = @_;
10 5         27 my $row = $self->single($table, $args);
11 5 100       31 return $row if $row;
12 3         29 $self->insert($table, $args)->refetch;
13             }
14              
15             1;
16              
17             __END__
18              
19             =head1 NAME
20              
21             Teng::Plugin::FindOrCreate - provide find_or_create method for your Teng class.
22              
23             =head1 NAME
24              
25             package MyDB;
26             use parent qw/Teng/;
27             __PACKAGE__->load_plugin('FindOrCreate');
28              
29             package main;
30             my $db = MyDB->new(...);
31             my $row = $db->find_or_create('user',{name => 'lestrrat'});
32              
33             =head1 DESCRIPTION
34              
35             This plugin provides find_or_create method.
36              
37             =head1 METHODS
38              
39             =over 4
40              
41             =item $row = $db->find_or_create($table[, \%args]])
42              
43             I<$table> table name for counting
44              
45             I<\%args> : HashRef for row data.
46              
47             =back