File Coverage

blib/lib/Devel/Events/Handler/Log/Memory.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package Devel::Events::Handler::Log::Memory;
4 2     2   37209 use Moose;
  0            
  0            
5              
6             with qw/Devel::Events::Handler/;
7              
8             use Devel::Events::Match;
9              
10             use MooseX::AttributeHelpers;
11              
12             has matcher => (
13             isa => "Devel::Events::Match",
14             is => "rw",
15             default => sub { Devel::Events::Match->new },
16             handles => sub {
17             my ( $attr, $meta ) = @_;
18            
19             my %mapping;
20              
21             foreach my $method ( $meta->get_method_list ) {
22             next if $method =~ /^ (?: compile_cond | match ) $/x;
23             next if __PACKAGE__->can($method);
24              
25             $mapping{$method} ||= sub {
26             my ( $self, @args ) = @_;
27             unshift @args, "match" if @args == 1;
28             $self->matcher->$method( events => scalar($self->events), @args );
29             }
30             }
31              
32             return %mapping;
33             }
34             );
35              
36             has events => (
37             metaclass => 'Collection::Array',
38             isa => "ArrayRef",
39             is => "ro",
40             default => sub { [] },
41             auto_deref => 1,
42             provides => {
43             push => 'add_event',
44             clear => 'clear',
45             },
46             );
47              
48             sub new_event {
49             my ( $self, @event ) = @_;
50             $self->add_event(\@event);
51             }
52              
53             sub replay {
54             my ( $self, $handler ) = @_;
55             $handler->new_event( @$_ ) for $self->events;
56             }
57              
58             __PACKAGE__;
59              
60             __END__
61              
62             =pod
63              
64             =head1 NAME
65              
66             Devel::Events::Handler::Log::Memory - An optional base role for event generators.
67              
68             =head1 SYNOPSIS
69              
70             use Devel::Events::Handler::Log::Memory;
71              
72             my $log = Devel::Events::Handler::Log::Memory->new();
73              
74             Some::Geneator->new( handler => $log );
75              
76             =head1 DESCRIPTION
77              
78             This convenience role provides a basic C<send_event> method, useful for
79             implementing generators.
80              
81             =head1 ATTRIBUTES
82              
83             =over 4
84              
85             =item events
86              
87             The list of events.
88              
89             Auto derefs.
90              
91             =item matcher
92              
93             The L<Devel::Events::Match> instance used for event matching.
94              
95             =back
96              
97             =head1 METHODS
98              
99             =over 4
100              
101             =item clear
102              
103             Remove all events from the log.
104              
105             Provided by L<MooseX::AttributeHelpers>.
106              
107             =item first $cond
108              
109             =item first %args
110              
111             Return the first event that matches a certain condition.
112              
113             Delegates to L<Devel::Events::Match>.
114              
115             =item grep $cond
116              
117             =item grep %args
118              
119             Return the list of events that match a certain condition.
120              
121             Delegates to L<Devel::Events::Match>.
122              
123             =item limit from => $cond, to => $cond, %args
124              
125             Return events between two events. If if C<from> or C<to> is omitted then it
126             returns all the events up to or from the other filter.
127              
128             Delegates to L<Devel::Events::Match>.
129              
130             =item chunk $marker
131              
132             =item chunk %args
133              
134             Cuts the event log into chunks. When C<$marker> matches a new chunk is opened.
135              
136             Delegates to L<Devel::Events::Match>.
137              
138             =item new_event @event
139              
140             Log the event to the C<events> list by calling C<add_event>.
141              
142             =item add_event \@event
143              
144             Provided by L<MooseX::AttributeHelpers>.
145              
146             =item replay $handler
147              
148             Replay all the events in the log to $handler.
149              
150             Useful if C<$handler> does heavy analysis that you want to delay.
151              
152             There isn't much to it:
153              
154             $handler->new_event(@$_) for $self->events;
155              
156             So obviously you can replay subsets of events manually.
157              
158             =back
159              
160             =head1 CAVEATS
161              
162             If any references are present in the event data then they will be preserved
163             till the log is clear. This may cause leaks.
164              
165             To overcome this problem use L<Devel::Events::Filter::Stringify>. It will not
166             allow overloading unless asked to, so it's safe to use without side effects.
167              
168             =head1 TODO
169              
170             Add an option to always hash all the event data for convenience.
171              
172             Make C<grep> and C<limit> into exportable functions, too.
173              
174             =cut
175              
176