File Coverage

lib/Dist/Zilla/Tempdir/Item.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1 1     1   1304 use 5.008; # 08 -> [utf8], 06 -> [pragmas, our]
  1         4  
  1         44  
2 1     1   1122 use utf8;
  1         11  
  1         7  
3 1     1   35 use strict;
  1         2  
  1         40  
4 1     1   7 use warnings;
  1         2  
  1         104  
5              
6             package Dist::Zilla::Tempdir::Item;
7              
8             our $VERSION = '1.001001';
9              
10             # ABSTRACT: A result object for things that DO() DZ::R::Tempdir;
11              
12             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
13              
14 1     1   491 use Moose qw( has );
  0            
  0            
15             use MooseX::LazyRequire;
16             use namespace::autoclean;
17              
18             use Carp qw(croak);
19             use Scalar::Util qw( blessed );
20              
21              
22              
23              
24              
25              
26              
27              
28              
29              
30              
31              
32              
33              
34              
35              
36             has 'status' => (
37             isa => 'Str',
38             lazy_required => 1,
39             is => 'rw',
40             );
41              
42              
43              
44              
45              
46              
47              
48              
49              
50              
51              
52              
53              
54             has 'file' => (
55             isa => 'Dist::Zilla::Role::File',
56             required => 1,
57             is => 'rw',
58             handles => { name => 'name' },
59             );
60              
61              
62              
63              
64              
65              
66              
67              
68              
69             sub _mk_status {
70             my $name = shift;
71             my $value = shift;
72              
73             my $setter = sub {
74             my $self = shift;
75             return croak( $name . 'is an instance method, not a class method' ) unless blessed($self);
76             return croak( 'too many arguments ( 0 expected ) to ->' . $name ) if @_;
77             $self->status($value);
78             };
79              
80             my $getter = sub {
81             my $self = shift;
82             return croak( $name . 'is an instance method, not a class method' ) unless blessed($self);
83             return croak( 'too many arguments ( 0 expected ) to ->' . $name ) if @_;
84             $self->status() eq $value;
85             };
86              
87             {
88             ## no critic ( ProhibitNoStrict )
89             no strict 'refs';
90             *{ __PACKAGE__ . q[::set_] . $name } = $setter;
91             *{ __PACKAGE__ . q[::is_] . $name } = $getter;
92             }
93             return 1;
94             }
95              
96              
97              
98              
99              
100              
101              
102              
103              
104              
105              
106             _mk_status( 'modified', 'M' );
107              
108              
109              
110              
111              
112              
113              
114              
115              
116              
117              
118             _mk_status( 'original', 'O' );
119              
120              
121              
122              
123              
124              
125              
126              
127              
128              
129              
130              
131             _mk_status( 'new', 'N' );
132              
133              
134              
135              
136              
137              
138              
139              
140              
141              
142              
143             _mk_status( 'deleted', 'D' );
144              
145             __PACKAGE__->meta->make_immutable;
146              
147             no Moose;
148              
149             1;
150              
151             __END__
152              
153             =pod
154              
155             =encoding UTF-8
156              
157             =head1 NAME
158              
159             Dist::Zilla::Tempdir::Item - A result object for things that DO() DZ::R::Tempdir;
160              
161             =head1 VERSION
162              
163             version 1.001001
164              
165             =head1 SYNOPSIS
166              
167             my $foo = Dist::Zilla::Tempdir::Item->new(
168             name => 'Path/To/File.txt',
169             file => $dzilfile,
170             );
171             $foo->set_new;
172             $foo->is_new; # true
173             $foo->is_deleted; # false
174             $foo->set_deleted;
175             $foo->is_new; # false
176             $foo->is_deleted; # true.
177              
178             Ultimately, I figured using a character with "C<eq>" every where in extending code
179             was a way to extra bugs that were hard to detect. Going via all the Object-Oriented niceness
180             you'll probably incur* a small performance penalty, but things going B<Bang> when you
181             make a typo or add invisible white-space is a Good Thing.
182              
183             * albeit immeasurably insignificant in size, especially for something that will only take
184             15 seconds of run-time every once in a while, not to mention the overhead is drowned by the
185             fact we're doing file-system IO and running many of the files through a complete hashing
186             algorithm to test for modification.
187              
188             =head1 ATTRIBUTES
189              
190             =head2 status
191              
192             isa => Str,
193             is => rw,
194              
195             The internal status character. You can mangle this yourself if you want, and for compatibility with older versions
196             of this dist, you may even have to, but try not to, if it breaks, something something something pieces.
197              
198             Using the is_* and set_* accessors is a I<much> smarter idea.
199              
200             At present, the characters M, O, N and D have defined meanings, but this could change. ( Its not even unforeseeable expanding it to
201             be 2 characters to represent different parts of state, I probably will not do that, but do not pretend I will not ;) )
202              
203             =head2 file
204              
205             isa => Dist::Zilla::Role::File,
206             required => 1,
207             is => rw
208              
209             This is the Dist::Zilla::File::* item which we refer to. For items that C<is_deleted>, C<file> is likely to be the file before it got deleted.
210              
211             For C<is_new> and C<is_original> files, the item is the file itself, and for C<is_modified>, its the modified version of the file.
212              
213             =head1 METHODS
214              
215             =head2 name
216              
217             Proxy for C<< $item->file->name >>
218              
219             This is the path to the file relative to the dist root.
220              
221             =head2 is_modified
222              
223             returns if the file is modified or not.
224              
225             =head2 set_modified
226              
227             sets the state to 'modified'
228              
229             =head2 is_original
230              
231             returns if the file is the original file or not.
232              
233             =head2 set_original
234              
235             sets the state to 'original'
236              
237             =head2 is_new
238              
239             returns if the file is new or not ( that is, if it wasn't in the dist prior to executing
240             the given code ).
241              
242             =head2 set_new
243              
244             sets the state to 'new'
245              
246             =head2 is_deleted
247              
248             returns if the file is deleted or not ( that is, if it were deleted during the execution phase )
249              
250             =head2 set_deleted
251              
252             sets the state to 'deleted'
253              
254             =head1 AUTHOR
255              
256             Kent Fredric <kentnl@cpan.org>
257              
258             =head1 COPYRIGHT AND LICENSE
259              
260             This software is copyright (c) 2014 by Kent Fredric.
261              
262             This is free software; you can redistribute it and/or modify it under
263             the same terms as the Perl 5 programming language system itself.
264              
265             =cut