blib/lib/POE/Component/DirWatch/Object/Untouched.pm untested

filter: Statementstmt 0.0% 0 / 34 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 45 CC 2 SCAR 11.5
CC
2
Cov
0%
_stat_check
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 3.1
1 package POE::Component::DirWatch::Object::Untouched;
2 use strict;
3 use warnings;
4 use Moose;
5 use Array::Compare;
6 use POE;
7  
8 our $VERSION = "0.02";
9  
10 extends 'POE::Component::DirWatch::Object';
11  
12 has 'stat_interval'=> (is => 'rw', isa => 'Num', required => 1, default => 1);
13 has 'cmp' => (is => 'rw', isa => 'Object', required => 1,
14 default => sub{ Array::Compare->new } );
15  
16 #--------#---------#---------#---------#---------#---------#---------#---------#
17  
18 #Remind me of stat:
19 # 7 size total size of file, in bytes
20 # 8 atime last access time in seconds since the epoch
21 # 9 mtime last modify time in seconds since the epoch
22 # 10 ctime inode change time in seconds since the epoch (*)
23  
24 before '_start' => sub{
25 my ($self, $kernel) = @_[OBJECT, KERNEL];
26 $kernel->state('stat_check', $self, '_stat_check');
27 };
28
29 override '_dispatch' => sub{
30 my ($self, $kernel, @params) = @_[OBJECT, KERNEL, ARG0, ARG1];
31 return unless $self->filter->(@params);
32  
33 $kernel->delay(stat_check => $self->stat_interval,
34 \@params, [ ( stat($_->[1]) )[7..10] ]
35 )
36 };
37
38 sub _stat_check{
39 my ($self, $kernel, $params, $stats) = @_[OBJECT, KERNEL, ARG0, ARG1];
40 $kernel->yield(callback => $params)
41 if $self->cmp->compare($stats, [ ( stat($params->[1]) )[7..10] ]);
42 }
43  
44  
45 1;
46  
47 __END__;
48  
49 #--------#---------#---------#---------#---------#---------#---------#---------#
50  
51  
52 =head1 NAME
53  
54 POE::Component::DirWatch::Object::Untouched
55  
56 =head1 SYNOPSIS
57  
58 use POE::Component::DirWatch::Object::Untouched;
59  
60 #$watcher is a PoCo::DW:Object::Untouched
61 my $watcher = POE::Component::DirWatch::Object::Untouched->new
62 (
63 alias => 'dirwatch',
64 directory => '/some_dir',
65 filter => sub { $_[0] =~ /\.gz$/ && -f $_[1] },
66 callback => \&some_sub,
67 interval => 5,
68 stat_interval => 2, #pick up files if they are untouched after 2 seconds
69 );
70  
71 $poe_kernel->run;
72  
73 =head1 DESCRIPTION
74  
75 POE::Component::DirWatch::Object::Untouched extends DirWatch::Object in order to
76 exclude files that appear to be in use or are actively being changed.
77  
78 =head1 Accessors
79  
80 =head2 stat_interval
81  
82 Read-Write. An integer value that specifies how many seconds to wait in between the
83 call to dispatch and the actual dispatch. The interval here serves as a dead period
84 in between when the initial stat readings are made and the second reading is made
85 (the one that determines whether there was any change or not). Note that the
86 C<interval> in C<POE::Component::DirWatch::Object> will be delayed by this length.
87 See C<_stat_check> for details.
88  
89 =head2 cmp
90  
91 An Array::Compare object
92  
93 =head1 Extended methods
94  
95 =head2 _start
96  
97 C<after '_start'> the kernel is called and a new 'stat_check' event is added.
98  
99 =head2 _dispatch
100  
101 C<override '_dispatch'> to delay and delegate the dispatching to _stat_check.
102 Filtering still happens at this stage.
103  
104 =head1 New Methods
105  
106 =head2 _stat_check
107  
108 Schedule a callback event for every file whose contents have not changed since the
109 C<poll> event. After all callbacks are scheduled, set an alarm for the next poll.
110  
111 ARG0 should be the proper params for C<callback> and ARG1 the original C<stat()>
112 reading we are comparing against.
113  
114 =head2 meta
115  
116 Keeping tests happy.
117  
118 =head1 SEE ALSO
119  
120 L<POE::Component::DirWatch::Object>, L<Moose>
121  
122 =head1 AUTHOR
123  
124 Guillermo Roditi, <groditi@cpan.org>
125  
126 =head1 BUGS
127  
128 If a file is created and deleted between polls it will never be seen. Also if a file
129 is edited more than once in between polls it will never be picked up.
130  
131 Please report any bugs or feature requests to
132 C<bug-poe-component-dirwatch-object at rt.cpan.org>, or through the web interface at
133 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=POE-Component-DirWatch-Object>.
134 I will be notified, and then you'll automatically be notified of progress on
135 your bug as I make changes.
136  
137 =head1 SUPPORT
138  
139 You can find documentation for this module with the perldoc command.
140  
141 perldoc POE::Component::DirWatch::Object::Untouched
142  
143 You can also look for information at:
144  
145 =over 4
146  
147 =item * AnnoCPAN: Annotated CPAN documentation
148  
149 L<http://annocpan.org/dist/POE-Component-DirWatch-Object>
150  
151 =item * CPAN Ratings
152  
153 L<http://cpanratings.perl.org/d/POE-Component-DirWatch-Object>
154  
155 =item * RT: CPAN's request tracker
156  
157 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=POE-Component-DirWatch-Object>
158  
159 =item * Search CPAN
160  
161 L<http://search.cpan.org/dist/POE-Component-DirWatch-Object>
162  
163 =back
164  
165 =head1 ACKNOWLEDGEMENTS
166  
167 People who answered way too many questions from an inquisitive idiot:
168  
169 =over 4
170  
171 =item #PoE & #Moose
172  
173 =item Matt S Trout <mst@shadowcatsystems.co.uk>
174  
175 =item Rocco Caputo
176  
177 =back
178  
179 =head1 COPYRIGHT
180  
181 Copyright 2006 Guillermo Roditi. All Rights Reserved. This is
182 free software; you may redistribute it and/or modify it under the same
183 terms as Perl itself.
184  
185 =cut
186