File Coverage

blib/lib/WebAPI/DBIC/TypeNamer.pm
Criterion Covered Total %
statement 21 44 47.7
branch 0 22 0.0
condition n/a
subroutine 7 10 70.0
pod 0 2 0.0
total 28 78 35.9


line stmt bran cond sub pod time code
1             package WebAPI::DBIC::TypeNamer;
2             $WebAPI::DBIC::TypeNamer::VERSION = '0.003002';
3 2     2   4115880 use Moo;
  2         35364  
  2         11  
4              
5 2     2   3492 use String::CamelCase qw(camelize decamelize);
  2         1033  
  2         207  
6 2     2   1129 use Lingua::EN::Inflect::Number qw(to_S to_PL);
  2         37269  
  2         15  
7 2     2   683 use Carp qw(croak confess);
  2         3  
  2         117  
8 2     2   1263 use Devel::Dwarn;
  2         16459  
  2         11  
9              
10 2     2   1312 use namespace::clean -except => [qw(meta)];
  2         22018  
  2         18  
11 2     2   1806 use MooX::StrictConstructor;
  2         19060  
  2         11  
12              
13              
14             # specify what information should be used to define the url path/type of a schema class
15             # (result_name is deprecated and only supported for backwards compatibility)
16             has type_name_from => (is => 'ro', default => 'source_name'); # 'source_name', 'result_name'
17              
18             # how type_name_from should be inflected
19             has type_name_inflect => (is => 'ro', default => 'original'); # 'original', 'singular', 'plural'
20              
21             # how type_name_from should be capitalized
22             has type_name_style => (is => 'ro', default => 'under_score'); # 'original', 'CamelCase', 'camelCase', 'under_score'
23              
24              
25             sub type_name_for_resultset {
26 0     0 0   my ($self, $rs) = @_;
27              
28 0           my $type_name;
29 0 0         if ($self->type_name_from eq 'source_name') {
    0          
30 0           $type_name = $rs->result_source->source_name;
31             }
32             elsif ($self->type_name_from eq 'result_name') { # deprecated
33 0           $type_name = $rs->name; # eg table name
34 0 0         $type_name = $$type_name if ref($type_name) eq 'SCALAR';
35             }
36             else {
37 0           confess "Invalid type_name_from: ".$self->type_name_from;
38             }
39              
40 0           return $self->_inflect_and_style($type_name);
41             }
42              
43              
44             sub type_name_for_result_class {
45 0     0 0   my ($self, $result_class) = @_;
46              
47 0 0         confess "bad type_name_from"
48             unless $self->type_name_from eq 'source_name';
49              
50 0           (my $type_name = $result_class) =~ s/^.*:://;
51              
52 0           return $self->_inflect_and_style($type_name);
53             }
54              
55              
56             sub _inflect_and_style {
57 0     0     my ($self, $type_name) = @_;
58              
59 0 0         if ($self->type_name_inflect eq 'singular') {
    0          
60 0           $type_name = to_S($type_name);
61             }
62             elsif ($self->type_name_inflect eq 'plural') {
63 0           $type_name = to_PL($type_name);
64             }
65             else {
66 0 0         confess "Invalid type_name_inflect: ".$self->type_name_inflect
67             unless $self->type_name_inflect eq 'original';
68             }
69              
70 0 0         if ($self->type_name_style eq 'under_score') {
    0          
    0          
71 0           $type_name = decamelize($type_name);
72             }
73             elsif ($self->type_name_style eq 'CamelCase') {
74 0           $type_name = camelize($type_name);
75             }
76             elsif ($self->type_name_style eq 'camelCase') {
77 0           $type_name = lcfirst(camelize($type_name));
78             }
79             else {
80 0 0         confess "Invalid type_name_style: ".$self->type_name_from
81             unless $self->type_name_style eq 'original';
82             }
83              
84 0           return $type_name;
85             }
86              
87              
88             1;
89              
90             __END__
91              
92             =pod
93              
94             =encoding UTF-8
95              
96             =head1 NAME
97              
98             WebAPI::DBIC::TypeNamer
99              
100             =head1 VERSION
101              
102             version 0.003002
103              
104             =head1 AUTHOR
105              
106             Tim Bunce <Tim.Bunce@pobox.com>
107              
108             =head1 COPYRIGHT AND LICENSE
109              
110             This software is copyright (c) 2015 by Tim Bunce.
111              
112             This is free software; you can redistribute it and/or modify it under
113             the same terms as the Perl 5 programming language system itself.
114              
115             =cut