File Coverage

blib/lib/DBIx/Class/Schema/ResultSetNames.pm
Criterion Covered Total %
statement 57 58 98.2
branch 8 10 80.0
condition 4 6 66.6
subroutine 13 13 100.0
pod 1 1 100.0
total 83 88 94.3


line stmt bran cond sub pod time code
1             package DBIx::Class::Schema::ResultSetNames;
2 2     2   286058 use Modern::Perl;
  2         15462  
  2         11  
3             our $VERSION = '1.0301'; # VERSION
4             our $AUTHORITY = 'cpan:GEEKRUTH'; # AUTHORITY
5             # ABSTRACT: Create resultset accessors from schema result class names
6 2     2   336 use base qw(DBIx::Class::Schema);
  2         4  
  2         168  
7 2     2   11 use Carp;
  2         5  
  2         80  
8 2     2   841 use Lingua::EN::Inflect::Phrase;
  2         109238  
  2         247  
9              
10             __PACKAGE__->mk_group_accessors( inherited => 'resultset_name_methods' );
11              
12             __PACKAGE__->resultset_name_methods( {} );
13              
14             sub register_source {
15 5     5 1 160549 my ( $class, $source_name, @rest ) = @_;
16 5         26 my $source = $class->next::method( $source_name, @rest );
17 5         1657 $class->_register_resultset_name_methods($source_name);
18 4         14 return $source;
19             }
20              
21             sub _ensure_resultset_name_method {
22 8     8   19 my ( $class, $name, $sub ) = @_;
23 8 50       88 return if $class->can($name);
24             {
25 2     2   13 no strict 'refs';
  2         6  
  2         1183  
  8         12  
26 8         12 *{"${class}::${name}"} = $sub;
  8         26  
27             }
28 8         14 $class->resultset_name_methods( { %{ $class->resultset_name_methods }, $name => 1 }, );
  8         156  
29 8         322 return;
30             }
31              
32             sub _register_resultset_name_methods {
33 5     5   10 my ( $class, $source_name ) = @_;
34 5         12 my $rsname_overrides = {};
35 5 100       35 if ( $class->can('override_rsnames') ) {
36 4         12 $rsname_overrides = $class->override_rsnames;
37             }
38             my $method_name = $rsname_overrides->{$source_name}->{singular}
39 5   66     56 || $class->_source_name_to_method_name($source_name);
40             my $plural_name = $rsname_overrides->{$source_name}->{plural}
41 5   66     60 || $class->_source_name_to_plural_name($source_name);
42 5 100       17 if ( $method_name eq $plural_name ) {
43 1         342 croak << "END_MESSAGE";
44             The ResultSet $source_name is the same word in both singular and
45             plural forms. Use an override to choose different words for one
46             or the other, or both. Consult the documentation for assistance
47             in doing this.
48              
49             END_MESSAGE
50             }
51             $class->_ensure_resultset_name_method(
52             $method_name => sub {
53 11     11   54560 my ( $self, @args ) = @_;
54 11 100       50 die "Can't call ${method_name} without arguments" unless @args;
55 9         30 $self->resultset($source_name)->find(@args);
56             }
57 4         31 );
58             $class->_ensure_resultset_name_method(
59             $plural_name => sub {
60 8     8   135179 my ( $self, @args ) = @_;
61 8         30 my $rs = $self->resultset($source_name);
62 8 50       2677 return $rs unless @args;
63 0         0 return $rs->search_rs(@args);
64             }
65 4         20 );
66 4         13 return;
67             }
68              
69             sub _source_name_to_method_name {
70 4     4   19 my ( $class, $source_name ) = @_;
71 4         26 my $phrase = $class->_source_name_to_phrase($source_name);
72 4         16 my $singularised = Lingua::EN::Inflect::Phrase::to_S($phrase);
73 4         247839 return join '_', split q{ }, $singularised;
74             }
75              
76             sub _source_name_to_phrase {
77 8     8   17 my ( $class, $source_name ) = @_;
78             join q{ }, map {
79 8         27 join( q{ }, map { lc } grep { length } split /([A-Z]{1}[^A-Z]*)/ )
  8         36  
  8         50  
  16         30  
80             } split /::/, $source_name;
81             }
82              
83             sub _source_name_to_plural_name {
84 4     4   27 my ( $class, $source_name ) = @_;
85 4         12 my $phrase = $class->_source_name_to_phrase($source_name);
86 4         15 my $pluralised = Lingua::EN::Inflect::Phrase::to_PL($phrase);
87 4         5353 return join '_', split q{ }, $pluralised;
88             }
89              
90             1;
91              
92             __END__