File Coverage

blib/lib/App/hopen/Asset.pm
Criterion Covered Total %
statement 24 31 77.4
branch 7 14 50.0
condition 1 6 16.6
subroutine 6 7 85.7
pod 3 3 100.0
total 41 61 67.2


line stmt bran cond sub pod time code
1             # App::hopen::Asset - record representing a file to be produced
2             package App::hopen::Asset;
3 2     2   14 use Data::Hopen::Base;
  2         4  
  2         14  
4              
5 2     2   3419 use Path::Class;
  2         57290  
  2         593  
6             # and we use Class::Tiny below.
7              
8             our $VERSION = '0.000010';
9              
10             # Docs {{{1
11              
12             =head1 NAME
13              
14             App::hopen::Asset - record representing a file to be produced
15              
16             =head1 SYNOPSIS
17              
18             An asset is something to be produced, e.g., a file on disk or something
19             else that could be a target in a Makefile.
20              
21             =head1 ATTRIBUTES
22              
23             =head2 target
24              
25             TODO: should on-disk targets be required to be BasedPath instances?
26              
27             The name of the asset. Must be one of:
28              
29             =over
30              
31             =item *
32              
33             A L<Path::Class> instance, representing a file or directory on disk
34              
35             =item *
36              
37             An L<App::hopen::Util::BasedPath> instance, representing a file or directory
38             on disk
39              
40             =item *
41              
42             Something that stringifies to a non-disk target (e.g., a goal). Anything in
43             this category will be stored as its stringified value, NOT as its original
44             value.
45              
46             =back
47              
48             No default, so don't call C<< $obj->target >> until you've assigned a target!
49              
50             =head2 made_by
51              
52             The L<App::hopen::G::Cmd> (or subclass) instance that produced this asset.
53             Used to distinguish assets from different sources.
54              
55             =head2 name
56              
57             An optional asset name. If you don't specify one, a unique one will be
58             generated automatically.
59              
60             =head1 METHODS
61              
62             =cut
63              
64             # }}}1
65              
66             # The accessor for the target attribute.
67             sub target {
68 2     2 1 6695 my $self = shift;
69 2 100       47 if (@_) {
    50          
70 1         4 my $candidate = shift;
71 1 50       21 croak "targets must not be falsy" unless $candidate;
72 1 50 33     31 if(eval { $candidate->DOES('Path::Class::File') ||
  1 50       62  
73             $candidate->DOES('Path::Class::Dir') ||
74             $candidate->DOES('App::hopen::Util::BasedPath' ) }
75             ) {
76 1         5 return $self->{target} = $candidate;
77             } else {
78 0         0 return $self->{target} = "$candidate";
79             }
80             } elsif ( exists $self->{target} ) {
81 1         6 return $self->{target};
82             } else { # No default.
83 0         0 croak "I don't have a target to give you";
84             }
85             } #target()
86              
87             # Create the accessor that enforces the restriction on made_by
88             use Class::Tiny::ConstrainedAccessor [NOBUILD => true],
89 0         0 made_by => [ sub { eval { $_[0]->DOES('App::hopen::G::Cmd') } },
  0         0  
90 2     2   535 sub { 'made_by values must implement App::hopen::G::Cmd' } ];
  2         1510  
  2         26  
  0         0  
91              
92             # Set up the rest of the class
93 2     2   247 use Class::Tiny qw(target made_by name);
  2         6  
  2         10  
94              
95             =head2 isdisk
96              
97             Returns truthy if the L</target> is an on-disk entity, i.e., a
98             directory or file.
99              
100             =cut
101              
102             sub isdisk {
103 0 0   0 1 0 my $self = shift or croak 'Need an instance';
104 0   0     0 return ($self->target->DOES('Path::Class::File') ||
105             $self->target->DOES('Path::Class::Dir') ||
106             $self->target->DOES('App::hopen::Util::BasedPath')
107             );
108             } #isdisk()
109              
110             =head2 BUILD
111              
112             Enforces the requirement for C<target> and C<made_by>.
113              
114             =cut
115              
116             my $_id_counter;
117              
118             sub BUILD {
119 1     1 1 137 my ($self) = @_;
120 1 50       41 $self->name('__R_Asset_' . $_id_counter++) unless $self->name;
121 1         50 $self->target($self->{target}); # Check the custom constraint on target
122             # by re-setting target.
123 1         6 $self->_check_all_constraints(); # Checks made_by.
124             } #BUILD()
125              
126             1;
127             __END__
128             # vi: set fdm=marker: #