| 1 | package WebAPI::DBIC::TypeNamer; | ||||||||||||||||||
| 2 | 25 | $WebAPI::DBIC::TypeNamer::VERSION = '0.004002'; | |||||||||||||||||
| 3 | 25 | ▶ | use Moo; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | |||||||||||||||||||
| 5 | 25 | ▶ | use String::CamelCase qw(camelize decamelize); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | 25 | ▶ | use Lingua::EN::Inflect::Number qw(to_S to_PL); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | 25 | ▶ | use Carp qw(croak confess); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 25 | ▶ | use Devel::Dwarn; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | |||||||||||||||||||
| 10 | 25 | ▶ | use namespace::clean -except => [qw(meta)]; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | 25 | ▶ | use MooX::StrictConstructor; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 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 | 25 | has type_name_from => (is => 'ro', default => 'source_name'); # 'source_name', 'result_name' | |||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | # how type_name_from should be inflected | ||||||||||||||||||
| 19 | 25 | has type_name_inflect => (is => 'ro', default => 'original'); # 'original', 'singular', 'plural' | |||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | # how type_name_from should be capitalized | ||||||||||||||||||
| 22 | 25 | has type_name_style => (is => 'ro', default => 'under_score'); # 'original', 'CamelCase', 'camelCase', 'under_score' | |||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | |||||||||||||||||||
| 25 | 134 | ▶ | sub type_name_for_resultset { | ||||||||||||||||
|
Subroutine
type_name_for_resultset: called CC=4 SCAR=19.4
Pod
undocumented
| |||||||||||||||||||
| 26 | 134 | my ($self, $rs) = @_; | |||||||||||||||||
| 27 | |||||||||||||||||||
| 28 | 134 | my $type_name; | |||||||||||||||||
| 29 | 134 | ▶ | if ($self->type_name_from eq 'source_name') { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 30 | 134 | $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 | ▶ | $type_name = $$type_name if ref($type_name) eq 'SCALAR'; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 35 | } | ||||||||||||||||||
| 36 | else { | ||||||||||||||||||
| 37 | 0 | confess "Invalid type_name_from: ".$self->type_name_from; | |||||||||||||||||
| 38 | } | ||||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | 134 | return $self->_inflect_and_style($type_name); | |||||||||||||||||
| 41 | } | ||||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | |||||||||||||||||||
| 44 | 107 | ▶ | sub type_name_for_result_class { | ||||||||||||||||
|
Subroutine
type_name_for_result_class: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||
| 45 | 107 | my ($self, $result_class) = @_; | |||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | 107 | ▶ | confess "bad type_name_from" | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 48 | unless $self->type_name_from eq 'source_name'; | ||||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | 107 | (my $type_name = $result_class) =~ s/^.*:://; | |||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | 107 | return $self->_inflect_and_style($type_name); | |||||||||||||||||
| 53 | } | ||||||||||||||||||
| 54 | |||||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | 241 | ▶ | sub _inflect_and_style { | ||||||||||||||||
|
Subroutine
_inflect_and_style: called CC=8 SCAR=30.8
| |||||||||||||||||||
| 57 | 241 | my ($self, $type_name) = @_; | |||||||||||||||||
| 58 | |||||||||||||||||||
| 59 | 241 | ▶ | if ($self->type_name_inflect eq 'singular') { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 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 | 241 | ▶ | confess "Invalid type_name_inflect: ".$self->type_name_inflect | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 67 | unless $self->type_name_inflect eq 'original'; | ||||||||||||||||||
| 68 | } | ||||||||||||||||||
| 69 | |||||||||||||||||||
| 70 | 241 | ▶ | if ($self->type_name_style eq 'under_score') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 71 | 241 | $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 | ▶ | confess "Invalid type_name_style: ".$self->type_name_from | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 81 | unless $self->type_name_style eq 'original'; | ||||||||||||||||||
| 82 | } | ||||||||||||||||||
| 83 | |||||||||||||||||||
| 84 | 241 | return $type_name; | |||||||||||||||||
| 85 | } | ||||||||||||||||||
| 86 | |||||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | 25 | 1; | |||||||||||||||||
| 89 | |||||||||||||||||||
| 90 | __END__ | ||||||||||||||||||