File Coverage

blib/lib/Pinto/Target/Distribution.pm
Criterion Covered Total %
statement 30 30 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 40 40 100.0


line stmt bran cond sub pod time code
1             # ABSTRACT: Specifies a distribution by author and archive
2              
3             package Pinto::Target::Distribution;
4              
5 56     56   53324 use Moose;
  56         418531  
  56         586  
6 56     56   369554 use MooseX::MarkAsMethods ( autoclean => 1 );
  56         20333  
  56         617  
7 56     56   203704 use MooseX::Types::Moose qw(ArrayRef Str);
  56         52397  
  56         657  
8              
9 56     56   266034 use Pinto::Types qw(AuthorID);
  56         292  
  56         427  
10 56     56   329546 use Pinto::Util qw(throw author_dir);
  56         265  
  56         4095  
11              
12 56     56   570 use overload ( '""' => 'to_string' );
  56         136  
  56         970  
13              
14             #------------------------------------------------------------------------------
15              
16             our $VERSION = '0.14'; # VERSION
17              
18             #------------------------------------------------------------------------------
19              
20             has author => (
21             is => 'ro',
22             isa => AuthorID,
23             coerce => 1,
24             required => 1,
25             );
26              
27             has archive => (
28             is => 'ro',
29             isa => Str,
30             required => 1,
31             );
32              
33             has subdirs => (
34             is => 'ro',
35             isa => ArrayRef[Str],
36             default => sub { [] },
37             );
38              
39             #------------------------------------------------------------------------------
40              
41             around BUILDARGS => sub {
42             my $orig = shift;
43             my $class = shift;
44              
45             my @args = @_;
46             if ( @args == 1 and not ref $args[0] ) {
47             my @path_parts = split m{/+}x, $args[0];
48              
49             my $author = shift @path_parts; # First element
50             my $archive = pop @path_parts; # Last element
51             my $subdirs = [@path_parts]; # Everything else
52              
53             throw "Invalid distribution target: $args[0]"
54             if not( $author and $archive );
55              
56             @args = ( author => $author, subdirs => $subdirs, archive => $archive );
57             }
58              
59             return $class->$orig(@args);
60             };
61              
62             #------------------------------------------------------------------------------
63              
64              
65             sub path {
66 7     7 1 23 my ($self) = @_;
67              
68 7         191 my $author_dir = author_dir($self->author);
69 7         564 my @subdirs = @{ $self->subdirs };
  7         233  
70 7         160 my $archive = $self->archive;
71              
72 7         38 return join '/', $author_dir, @subdirs, $archive;
73             }
74              
75             #------------------------------------------------------------------------------
76              
77              
78             sub to_string {
79 54     54 1 1561 my ($self) = @_;
80              
81 54         1396 my $author = $self->author;
82 54         96 my @subdirs = @{ $self->subdirs };
  54         1334  
83 54         1288 my $archive = $self->archive;
84              
85 54         316 return join '/', $author, @subdirs, $archive;
86             }
87              
88             #------------------------------------------------------------------------------
89              
90             __PACKAGE__->meta->make_immutable;
91              
92             #------------------------------------------------------------------------------
93             1;
94              
95             __END__
96              
97             =pod
98              
99             =encoding UTF-8
100              
101             =for :stopwords Jeffrey Ryan Thalhammer
102              
103             =head1 NAME
104              
105             Pinto::Target::Distribution - Specifies a distribution by author and archive
106              
107             =head1 VERSION
108              
109             version 0.14
110              
111             =head1 METHODS
112              
113             =head2 path()
114              
115             Returns the canonical string form of this DistributionSpec, which is suitable
116             for constructing a URI.
117              
118             =head2 to_string
119              
120             Serializes this Target to its string form. This method is called whenever the
121             Target is evaluated in string context.
122              
123             =head1 AUTHOR
124              
125             Jeffrey Ryan Thalhammer <jeff@stratopan.com>
126              
127             =head1 COPYRIGHT AND LICENSE
128              
129             This software is copyright (c) 2015 by Jeffrey Ryan Thalhammer.
130              
131             This is free software; you can redistribute it and/or modify it under
132             the same terms as the Perl 5 programming language system itself.
133              
134             =cut