| 1 | package JSONAPI::Document::Builder::Role::Type; | ||||||||||
| 2 | 6 | $JSONAPI::Document::Builder::Role::Type::VERSION = '2.0'; | |||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | JSONAPI::Document::Builder::Role::Type - Normalizer for document types | ||||||||||
| 6 | |||||||||||
| 7 | =head1 VERSION | ||||||||||
| 8 | |||||||||||
| 9 | version 2.0 | ||||||||||
| 10 | |||||||||||
| 11 | =head1 DESCRIPTION | ||||||||||
| 12 | |||||||||||
| 13 | Provides methods to correctly format a rows source name. | ||||||||||
| 14 | |||||||||||
| 15 | =cut | ||||||||||
| 16 | |||||||||||
| 17 | 6 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 6 | ▶ | use Lingua::EN::Inflexion (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | 6 | has chi => ( | |||||||||
| 22 | is => 'ro', | ||||||||||
| 23 | required => 1, | ||||||||||
| 24 | ); | ||||||||||
| 25 | |||||||||||
| 26 | 6 | has segmenter => ( | |||||||||
| 27 | is => 'ro', | ||||||||||
| 28 | required => 1, | ||||||||||
| 29 | ); | ||||||||||
| 30 | |||||||||||
| 31 | =head2 format_type(Str $type) : Str | ||||||||||
| 32 | |||||||||||
| 33 | Returns a dash cased version of the type. | ||||||||||
| 34 | |||||||||||
| 35 | =cut | ||||||||||
| 36 | |||||||||||
| 37 | sub format_type { | ||||||||||
| 38 | 49 | ▶ | my ($self, $type) = @_; | ||||||||
|
Subroutine
format_type: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||
| 39 | 49 | ▶ | unless ($type) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 0 | Carp::confess('Missing argument: type'); | |||||||||
| 41 | } | ||||||||||
| 42 | 49 | $type =~ s/[_-]+/-/g; | |||||||||
| 43 | 49 | return lc $type; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | =head2 document_type(Str $type) : Str | ||||||||||
| 47 | |||||||||||
| 48 | Takes the type and creates its correct, pluralised type. | ||||||||||
| 49 | |||||||||||
| 50 | =cut | ||||||||||
| 51 | |||||||||||
| 52 | sub document_type { | ||||||||||
| 53 | 56 | ▶ | my ($self, $type) = @_; | ||||||||
|
Subroutine
document_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 54 | 56 | my $noun = Lingua::EN::Inflexion::noun($type); | |||||||||
| 55 | my $result = $self->chi->compute( | ||||||||||
| 56 | 'JSONAPI::Document:' . $noun->plural, | ||||||||||
| 57 | undef, | ||||||||||
| 58 | sub { | ||||||||||
| 59 | 14 | ▶ | my @words = $self->segmenter->segment($noun->plural); | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||
| 60 | 14 | ▶ | unless (scalar(@words) > 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 0 | push @words, $noun->plural; | |||||||||
| 62 | } | ||||||||||
| 63 | 14 | @words = map { $_ } grep { $_ =~ m/\A(?:[A-Za-z_]+)\z/ } @words; | |||||||||
| 64 | 14 | return $self->format_type(join('-', @words)); | |||||||||
| 65 | 56 | }); | |||||||||
| 66 | 56 | return $result; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 6 | 1; | |||||||||