File Coverage

blib/lib/EventStore/Tiny/EventStream.pm
Criterion Covered Total %
statement 43 43 100.0
branch 8 8 100.0
condition n/a
subroutine 14 14 100.0
pod 8 8 100.0
total 73 73 100.0


line stmt bran cond sub pod time code
1             package EventStore::Tiny::EventStream;
2              
3 7     7   669 use strict;
  7         9  
  7         174  
4 7     7   30 use warnings;
  7         9  
  7         273  
5              
6             use Class::Tiny {
7 8         106 events => sub {[]},
8 7     7   34 };
  7         55  
  7         80  
9              
10             sub add_event {
11 249     249 1 1268 my ($self, $event) = @_;
12              
13             # Append event to internal list
14 249         285 push @{$self->events}, $event;
  249         3048  
15              
16             # Done
17 249         1175 return $event;
18             }
19              
20             sub size {
21 80     80 1 18242 my $self = shift;
22 80         105 return scalar @{$self->events};
  80         1063  
23             }
24              
25             sub first_timestamp {
26 2     2 1 2290 my $self = shift;
27 2 100       3 return unless @{$self->events};
  2         33  
28 1         16 return $self->events->[0]->timestamp;
29             }
30              
31             sub last_timestamp {
32 66     66 1 1320 my $self = shift;
33 66 100       93 return unless @{$self->events};
  66         937  
34 53         883 return $self->events->[$self->size - 1]->timestamp;
35             }
36              
37             sub apply_to {
38 47     47 1 6319 my ($self, $state, $logger) = @_;
39              
40             # Start with empty state by default
41 47 100       116 $state = {} unless defined $state;
42              
43             # Apply all events
44 47         61 $_->apply_to($state, $logger) for @{$self->events};
  47         676  
45              
46             # Done
47 47         188 return $state;
48             }
49              
50             sub substream {
51 66     66 1 7275 my ($self, $selector) = @_;
52              
53             # Default selector: take everything
54 66 100   3   169 $selector = sub {1} unless defined $selector;
  3         5  
55              
56             # Filter events
57 66         95 my @filtered = grep {$selector->($_)} @{$self->events};
  1835         7570  
  66         915  
58              
59             # Build new sub stream
60 66         565 return EventStore::Tiny::EventStream->new(events => \@filtered);
61             }
62              
63             sub before {
64 38     38 1 9780 my ($self, $timestamp) = @_;
65              
66             # All events until the given timestamp (including)
67 38     997   161 return $self->substream(sub {$_->timestamp <= $timestamp});
  997         11812  
68             }
69              
70             sub after {
71 24     24 1 2254 my ($self, $timestamp) = @_;
72              
73             # All events after the given timestamp (excluding)
74 24     826   106 return $self->substream(sub {$_->timestamp > $timestamp});
  826         9731  
75             }
76              
77             1;
78              
79             =pod
80              
81             =encoding utf-8
82              
83             =head1 NAME
84              
85             EventStore::Tiny::EventStream
86              
87             =head1 REFERENCE
88              
89             EventStore::Tiny::Stream implements the following attributes and methods.
90              
91             =head2 events
92              
93             my $event17 = $stream->events->[16];
94              
95             Internal list representation (arrayref) of all events of this stream.
96              
97             =head2 add_event
98              
99             $stream->add_event($event);
100              
101             Adds an event to the stream.
102              
103             =head2 size
104              
105             my $event_count = $stream->size;
106              
107             Returns the number of events in this stream.
108              
109             =head2 first_timestamp
110              
111             my $start_ts = $stream->first_timestamp;
112              
113             Returns the timestamp of the first event of this stream.
114              
115             =head2 last_timestamp
116              
117             my $end_ts = $stream->last_timestamp;
118              
119             Returns the timestamp of the last event of this stream.
120              
121             =head2 apply_to
122              
123             my $state = $stream->apply_to(\%state);
124              
125             Applies the whole stream (all events one after another) to a given state (by default an empty hash). The state is changed by side-effect but is also returned.
126              
127             =head2 substream
128              
129             my $filtered = $stream->substream(sub {
130             my $event = shift;
131             return we_want($event);
132             });
133              
134             Creates a substream using a given filter. All events the given subref returns true for are selected for this substream.
135              
136             =head2 before
137              
138             my $pre_stream = $stream->before($timestamp);
139              
140             Returns a substream with all events before or at the same time of a given timestamp.
141              
142             =head2 after
143              
144             my $post_stream = $stream->after($timestamp);
145              
146             Returns a substream with all events after a given timestamp.
147              
148             =head1 SEE ALSO
149              
150             L
151              
152             =head1 COPYRIGHT AND LICENSE
153              
154             Copyright (c) 2018 Mirko Westermeier (mail: mirko@westermeier.de)
155              
156             Released under the MIT License (see LICENSE.txt for details).
157              
158             =cut