Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 50 lines (37 sloc) 0.778 kb
bd23c1e @masak added Hex::AggregateRoot and Hex::AggregateRoots::Game
masak authored
1 package Hex::AggregateRoot;
2
3 use Moose;
4
5 has 'id' => (
6 is => 'ro',
7 isa => 'Str'
8 );
9
10 has 'version' => (
11 is => 'ro',
12 isa => 'Int'
13 );
14
15 has 'changes' => (
16 is => 'rw',
17 isa => 'ArrayRef[Event]',
18 reader => 'get_uncommitted_changes',
19 );
20
f254a80 @masak [Hex::AggregateRoot] don't use a clearer here
masak authored
21 sub mark_changes_as_committed {
22 my ($self) = @_;
23
24 @{ $self->changes() } = ();
25 }
26
bd23c1e @masak added Hex::AggregateRoot and Hex::AggregateRoots::Game
masak authored
27 sub apply_event {
28 my ($self, $event, $add) = @_;
29
30 my $handler = $self->lookup()->{ref $event}
31 or return; # original code fails here...
5efdeda @masak routing things through
masak authored
32 $handler->($self);
bd23c1e @masak added Hex::AggregateRoot and Hex::AggregateRoots::Game
masak authored
33
34 $add //= 1;
35 if ($add) {
36 push @{$self->changes()}, $event;
37 }
38 }
39
40 sub load_from_history {
41 my ($self, $events_ref) = @_;
42
43 my @events = @{$events_ref};
44 for my $event (@events) {
45 $self->apply_event($event, 0);
46 }
47 }
48
49 1;
Something went wrong with that request. Please try again.