File Coverage

blib/lib/EventStore/Tiny/Event.pm
Criterion Covered Total %
statement 31 31 100.0
branch 4 4 100.0
condition n/a
subroutine 8 8 100.0
pod 2 3 66.6
total 45 46 97.8


line stmt bran cond sub pod time code
1             package EventStore::Tiny::Event;
2              
3 8     8   49232 use strict;
  8         23  
  8         209  
4 8     8   35 use warnings;
  8         11  
  8         203  
5              
6 8     8   3323 use UUID::Tiny qw(create_uuid_as_string);
  8         136201  
  8         609  
7 8     8   67 use Time::HiRes qw(time);
  8         24  
  8         58  
8              
9             use Class::Tiny {
10 2         40 uuid => sub {create_uuid_as_string},
11 298         1346 timestamp => sub {time},
12 1         11 name => sub {die "name is required.\n"},
13 1         39 transformation => sub {sub {}},
14 8     8   2392 };
  8         2670  
  8         211  
15              
16             sub BUILD {
17 299     299 0 59370 my $self = shift;
18              
19             # Set non-lazy
20 299         3951 $self->name;
21 298         4074 $self->timestamp;
22              
23             # Return nothing (will be ignored anyway)
24 298         456 return;
25             }
26              
27             # Lets transformation work on state by side-effect
28             sub apply_to {
29 31     31 1 5475 my ($self, $state, $logger) = @_;
30              
31             # Apply the transformation by side effect
32 31         419 $self->transformation->($state);
33              
34             # Log this event, if logger present
35 31 100       195 $logger->($self) if defined $logger;
36              
37             # Returned the same state just in case
38 31         95 return $state;
39             }
40              
41             # Return a one-line summary of this event
42             sub summary {
43 19     19 1 1572 my $self = shift;
44 19 100       234 my $decimals = $self->timestamp =~ /(\.\d+)$/ ? $1 : '';
45 19         379 my @time_parts = localtime $self->timestamp;
46 19         737 return sprintf '[%s (%4d-%02d-%02dT%02d:%02d:%02d%s)]',
47             $self->name,
48             $time_parts[5] + 1900, # Year
49             @time_parts[4, 3, 2, 1, 0], # Rest of time representation
50             $decimals; # Possibly empty
51             }
52              
53             1;
54              
55             =pod
56              
57             =encoding utf-8
58              
59             =head1 NAME
60              
61             EventStore::Tiny::Event
62              
63             =head1 REFERENCE
64              
65             EventStore::Tiny::Event implements the following attributes and methods.
66              
67             =head2 ATTRIBUTES
68              
69             All these attributes can be manipulated by setters/getters with the attribute's name or can be set on construction:
70              
71             my $event = EventStore::Tiny::Event->new(name => "Foo");
72              
73             =head3 uuid
74              
75             This event's UUID. By default a new UUID is created.
76              
77             =head3 timestamp
78              
79             This event's timestamp. By default a new timestamp of the creation time is set.
80              
81             =head3 name
82              
83             This event's name. Setting this attribute on construction is required.
84              
85             =head3 transformation
86              
87             This event's state transformation function, represented by a subref. By default it does nothing, so it should be set as a reasonable subref changing the given state argument (as a hashref) based on the given data (as a hashref) by side-effect.
88              
89             =head2 METHODS
90              
91             =head3 apply_to
92              
93             $event->apply_to(\%state, $logger);
94              
95             Applies this event's L to the given state (by side-effect). If a C<$logger> as a subref is given, it is used to log this application.
96              
97             =head3 summary
98              
99             say $event->summary;
100              
101             Returns a one-line summarized stringification of this event.
102              
103             =head1 SEE ALSO
104              
105             L
106              
107             =head1 COPYRIGHT AND LICENSE
108              
109             Copyright (c) 2018 Mirko Westermeier (mail: mirko@westermeier.de)
110              
111             Released under the MIT License (see LICENSE.txt for details).
112              
113             =cut