File Coverage

blib/lib/EventStore/Tiny/DataEvent.pm
Criterion Covered Total %
statement 32 32 100.0
branch 2 2 100.0
condition n/a
subroutine 7 7 100.0
pod 3 3 100.0
total 44 44 100.0


line stmt bran cond sub pod time code
1             package EventStore::Tiny::DataEvent;
2 7     7   3320 use parent 'EventStore::Tiny::Event';
  7         1433  
  7         32  
3              
4 7     7   397 use strict;
  7         11  
  7         118  
5 7     7   28 use warnings;
  7         10  
  7         239  
6              
7             use Class::Tiny {
8 1         25 data => sub {{}},
9 7     7   30 };
  7         14  
  7         53  
10              
11             sub new_from_template {
12 247     247 1 431 my ($class, $event, $data) = @_;
13              
14             # "clone"
15 247         3093 return EventStore::Tiny::DataEvent->new(
16             name => $event->name,
17             transformation => $event->transformation,
18             data => $data,
19             );
20             }
21              
22             # Lets transformation work on state by side-effect
23             sub apply_to {
24 380     380 1 4161 my ($self, $state, $logger) = @_;
25              
26             # Apply the transformation by side effect
27 380         4786 $self->transformation->($state, $self->data);
28              
29             # Log this event, if logger present
30 380 100       7199 $logger->($self) if defined $logger;
31              
32             # Returned the same state just in case
33 380         17213 return $state;
34             }
35              
36             # Return a one-line summary of this event
37             sub summary {
38 14     14 1 4038 my $self = shift;
39              
40             # Prepare data summary
41             my $data_summary = join ', ' => map {
42 14         228 my $d = $self->data->{$_};
43 14         70 $d =~ s/\s+/ /g; # Summarize in-between whitespace
44 14         34 $d =~ s/^\s+//; # Get rid of leading whitespace
45 14         27 $d =~ s/\s+$//; # Get rid of whitespace in the end
46 14         23 $d =~ s/['"]+//g; # Get rid of quotes
47 14         40 $d =~ s/^(.{17}).{3,}/$1.../; # Shorten
48 14         51 "$_: '$d'" # Quoted, shortened key-value pair
49 14         20 } sort keys %{$self->data};
  14         219  
50              
51             # Retrieve event summary (without data) and inject data summary
52 14         44 my $summary = $self->SUPER::summary;
53 14         190 $summary =~ s/\)\]$/) | $data_summary]/;
54              
55             # Done
56 14         68 return $summary;
57             }
58              
59             1;
60              
61             =pod
62              
63             =encoding utf-8
64              
65             =head1 NAME
66              
67             EventStore::Tiny::DataEvent
68              
69             =head1 REFERENCE
70              
71             EventStore::Tiny::DataEvent extends EventStore::Tiny::Event and implements the following additional attributes and methods.
72              
73             =head2 data
74              
75             my $ev = EventStore::Tiny::DataEvent->new(data => {id => 42});
76              
77             Sets concrete data for this event which will be used during application.
78              
79             =head2 new_from_template
80              
81             my $concrete = EventStore::Tiny::DataEvent->new_from_template(
82             $event, {id => 17}
83             );
84              
85             Creates a new data event based on another event (usually representing an L event type which was registered before using L). The additional argument sets the new event's L attribute.
86              
87             =head3 apply_to
88              
89             $event->apply_to(\%state, $logger);
90              
91             Applies this event's L to the given state (by side-effect) and its L. If a C<$logger> as a subref is given, it is used to log this application.
92              
93             =head3 summary
94              
95             say $event->summary;
96              
97             Extended version of L from the parent. It features a simple L summary.
98              
99             =head1 SEE ALSO
100              
101             L
102              
103             =head1 COPYRIGHT AND LICENSE
104              
105             Copyright (c) 2018 Mirko Westermeier (mail: mirko@westermeier.de)
106              
107             Released under the MIT License (see LICENSE.txt for details).
108              
109             =cut