File Coverage

blib/lib/DBIx/Class/Smooth/Schema.pm
Criterion Covered Total %
statement 39 40 97.5
branch 3 10 30.0
condition 0 3 0.0
subroutine 10 10 100.0
pod 1 1 100.0
total 53 64 82.8


line stmt bran cond sub pod time code
1 2     2   722348 use 5.20.0;
  2         22  
2 2     2   12 use strict;
  2         5  
  2         50  
3 2     2   12 use warnings;
  2         6  
  2         128  
4              
5             package DBIx::Class::Smooth::Schema;
6              
7             # ABSTRACT: Short intro
8             our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
9             our $VERSION = '0.0101';
10              
11 2     2   14 use parent 'DBIx::Class::Schema';
  2         4  
  2         11  
12 2     2   101992 use Carp qw/croak/;
  2         6  
  2         97  
13              
14 2     2   960 use experimental qw/postderef signatures/;
  2         6985  
  2         13  
15              
16             our $dbix_class_smooth_methods_created = 0;
17              
18 2     2 1 3170 sub connection($self, @rest) {
  2         6  
  2         5  
  2         4  
19 2         12 $self = $self->next::method(@rest);
20              
21 2 50       120460 if(!$dbix_class_smooth_methods_created) {
22 2         16 $self->_dbix_class_smooth_create_methods();
23             }
24 2         10 return $self;
25             }
26              
27 2     2   5 sub _dbix_class_smooth_create_methods($self) {
  2         6  
  2         5  
28 2     2   726 no strict 'refs';
  2         8  
  2         740  
29 2         11 for my $source (sort $self->sources) {
30 8         125 (my $method = $source) =~ s{::}{_}g;
31              
32 8 50       66 if($self->can($method)) {
33 0         0 croak(caller(1) . " already has a method named <$method>.");
34             }
35              
36 8         53 *{ caller(1) . "::$method" } = sub {
37 5     5   4109 my $rs = shift->resultset($source);
38              
39 5 0 0     5280 return !scalar @_ ? $rs
    0          
    50          
40             : defined $_[0] && !ref $_[0] ? $rs->find(@_)
41             : ref $_[0] eq 'ARRAY' ? $rs->find(@$_[1..$#_], { key => $_->[0] })
42             : $rs->search(@_)
43             ;
44 8         36 };
45             }
46 2         13 $dbix_class_smooth_methods_created = 1;
47              
48             }
49              
50             1;
51              
52             __END__
53              
54             =pod
55              
56             =encoding UTF-8
57              
58             =head1 NAME
59              
60             DBIx::Class::Smooth::Schema - Short intro
61              
62             =head1 VERSION
63              
64             Version 0.0101, released 2018-11-29.
65              
66             =head1 SYNOPSIS
67              
68             # in MyApp::Schema, instead of inheriting from DBIx::Class::Schema
69             use parent 'DBIx::Class::Smooth::Schema';
70              
71             =head1 DESCRIPTION
72              
73             DBIx::Class::Smooth::Schema adds method accessors for all resultsets.
74              
75             In short, instead of this:
76              
77             my $schema = MyApp::Schema->connect(...);
78             my $result = $schema->resultset('Author');
79              
80             You can do this:
81              
82             my $schema = MyApp::Schema->connect(...);
83             my $result = $schema->Author;
84              
85             =head2 What is returned?
86              
87             The resultset methods can be called in four different ways.
88              
89             =head3 Without arguments
90              
91             # $schema->resultset('Author')
92             $schema->Author;
93              
94             =head3 With a scalar
95              
96             # $schema->resultset('Author')->find(5)
97             $schema->Author(5);
98              
99             =head3 With an array reference
100              
101             # $schema->resultset('Book')->find({ author => 'J.R.R Tolkien', title => 'The Hobbit' }, { key => 'book_author_title' });
102             $schema->Book([book_author_title => { author => 'J.R.R Tolkien', title => 'The Hobbit' }]);
103              
104             =head3 With anything else
105              
106             # $schema->resultset('Author')->search({ last_name => 'Tolkien'}, { order_by => { -asc => 'first_name' }});
107             $schema->Author({ last_name => 'Tolkien'}, { order_by => { -asc => 'first_name' }});
108              
109             =head1 SEE ALSO
110              
111             =over 4
112              
113             =item *
114              
115             L<DBIx::Class::Smooth>
116              
117             =back
118              
119             =head1 SOURCE
120              
121             L<https://github.com/Csson/p5-DBIx-Class-Smooth>
122              
123             =head1 HOMEPAGE
124              
125             L<https://metacpan.org/release/DBIx-Class-Smooth>
126              
127             =head1 AUTHOR
128              
129             Erik Carlsson <info@code301.com>
130              
131             =head1 COPYRIGHT AND LICENSE
132              
133             This software is copyright (c) 2018 by Erik Carlsson.
134              
135             This is free software; you can redistribute it and/or modify it under
136             the same terms as the Perl 5 programming language system itself.
137              
138             =cut