File Coverage

blib/lib/WebAPI/DBIC/TypeNamer.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package WebAPI::DBIC::TypeNamer;
2             $WebAPI::DBIC::TypeNamer::VERSION = '0.004001';
3 2     2   7016050 use Moo;
  2         31231  
  2         34  
4              
5 2     2   3416 use String::CamelCase qw(camelize decamelize);
  0            
  0            
6             use Lingua::EN::Inflect::Number qw(to_S to_PL);
7             use Carp qw(croak confess);
8             use Devel::Dwarn;
9              
10             use namespace::clean -except => [qw(meta)];
11             use MooX::StrictConstructor;
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             my ($self, $rs) = @_;
27              
28             my $type_name;
29             if ($self->type_name_from eq 'source_name') {
30             $type_name = $rs->result_source->source_name;
31             }
32             elsif ($self->type_name_from eq 'result_name') { # deprecated
33             $type_name = $rs->name; # eg table name
34             $type_name = $$type_name if ref($type_name) eq 'SCALAR';
35             }
36             else {
37             confess "Invalid type_name_from: ".$self->type_name_from;
38             }
39              
40             return $self->_inflect_and_style($type_name);
41             }
42              
43              
44             sub type_name_for_result_class {
45             my ($self, $result_class) = @_;
46              
47             confess "bad type_name_from"
48             unless $self->type_name_from eq 'source_name';
49              
50             (my $type_name = $result_class) =~ s/^.*:://;
51              
52             return $self->_inflect_and_style($type_name);
53             }
54              
55              
56             sub _inflect_and_style {
57             my ($self, $type_name) = @_;
58              
59             if ($self->type_name_inflect eq 'singular') {
60             $type_name = to_S($type_name);
61             }
62             elsif ($self->type_name_inflect eq 'plural') {
63             $type_name = to_PL($type_name);
64             }
65             else {
66             confess "Invalid type_name_inflect: ".$self->type_name_inflect
67             unless $self->type_name_inflect eq 'original';
68             }
69              
70             if ($self->type_name_style eq 'under_score') {
71             $type_name = decamelize($type_name);
72             }
73             elsif ($self->type_name_style eq 'CamelCase') {
74             $type_name = camelize($type_name);
75             }
76             elsif ($self->type_name_style eq 'camelCase') {
77             $type_name = lcfirst(camelize($type_name));
78             }
79             else {
80             confess "Invalid type_name_style: ".$self->type_name_from
81             unless $self->type_name_style eq 'original';
82             }
83              
84             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.004001
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