ev 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ev.gemspec +9 -8
- data/lib/ev/event_source.rb +45 -56
- data/lib/ev/version.rb +1 -1
- data/spec/lib/ev/event_source_spec.rb +128 -112
- data/spec/spec_helper.rb +2 -0
- metadata +8 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e05fef9f1259cacab26fbd2489dda9ca6b973863
|
4
|
+
data.tar.gz: cce9abb499135d77b2403cb349ffc7620737b74a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af18c2770f7ef6be07d0a42ef5a556b92f495825ce4168005b15b74b5d4003cfef5144c9712e8c11765e04e972c81c2f449c4b1e8ac163ca608a1097b97be98b
|
7
|
+
data.tar.gz: 253bf73a8fa411a7f3bcdb0d9ca6efa0a8d82515084b6dc38c1daeaffb232e8edfaf42f0629e94038ce6c9a159c1e1991d9be21f8293456b5642384fa0a53ccc
|
data/ev.gemspec
CHANGED
@@ -1,21 +1,22 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
$:.push File.expand_path(
|
3
|
-
require
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'ev/version'
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
|
-
s.name =
|
6
|
+
s.name = 'ev'
|
7
7
|
s.version = EV::VERSION
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
|
-
s.authors = [
|
10
|
-
s.email = [
|
11
|
-
s.homepage =
|
9
|
+
s.authors = ['Matthew Conger-Eldeen']
|
10
|
+
s.email = ['mceldeen@gmail.com']
|
11
|
+
s.homepage = 'https://www.github.com/mceldeen/ev'
|
12
|
+
s.licenses = 'MIT'
|
12
13
|
s.summary = %q{A set of useful stuff for event sourcing.}
|
13
14
|
s.description = %q{A set of useful stuff for event sourcing.}
|
14
15
|
|
15
|
-
s.add_development_dependency
|
16
|
+
s.add_development_dependency 'rspec', '~>3.2'
|
16
17
|
|
17
18
|
s.files = `git ls-files`.split("\n")
|
18
19
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
20
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
-
s.require_paths = [
|
21
|
+
s.require_paths = ['lib']
|
21
22
|
end
|
data/lib/ev/event_source.rb
CHANGED
@@ -1,58 +1,47 @@
|
|
1
1
|
module EV
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
@changes ||= []
|
48
|
-
@changes << event
|
49
|
-
end
|
50
|
-
|
51
|
-
private
|
52
|
-
|
53
|
-
def apply_without_changes(event)
|
54
|
-
handle(event)
|
55
|
-
@version = version.next
|
56
|
-
end
|
57
|
-
end
|
2
|
+
class EventSource
|
3
|
+
def initialize(events)
|
4
|
+
events.each { |event| apply_without_changes(event) }
|
5
|
+
end
|
6
|
+
|
7
|
+
def version
|
8
|
+
@version || 0
|
9
|
+
end
|
10
|
+
|
11
|
+
def changes
|
12
|
+
return [] unless @changes
|
13
|
+
@changes.dup
|
14
|
+
end
|
15
|
+
|
16
|
+
def initial_version
|
17
|
+
version - changes.size
|
18
|
+
end
|
19
|
+
|
20
|
+
def dirty?
|
21
|
+
changes.size > 0
|
22
|
+
end
|
23
|
+
|
24
|
+
def commit
|
25
|
+
@changes.clear if @changes
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
def handle(event)
|
31
|
+
raise NotImplementedError
|
32
|
+
end
|
33
|
+
|
34
|
+
def apply(event)
|
35
|
+
apply_without_changes(event)
|
36
|
+
@changes ||= []
|
37
|
+
@changes << event
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def apply_without_changes(event)
|
43
|
+
handle(event)
|
44
|
+
@version = version.next
|
45
|
+
end
|
46
|
+
end
|
58
47
|
end
|
data/lib/ev/version.rb
CHANGED
@@ -1,113 +1,129 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
2
|
+
|
3
|
+
class EventSourceExample < EV::EventSource
|
4
|
+
def add_event(event)
|
5
|
+
apply(event)
|
6
|
+
end
|
7
|
+
|
8
|
+
protected
|
9
|
+
def handle(event)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
RSpec.describe EventSourceExample do
|
14
|
+
describe '#changes' do
|
15
|
+
let(:events) { [1, 2] }
|
16
|
+
subject { described_class.new(events) }
|
17
|
+
|
18
|
+
context 'with events before initialization' do
|
19
|
+
it 'has no changes' do
|
20
|
+
expect(subject.changes).to eq([])
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'with an events before and after initialization' do
|
25
|
+
let(:new_events) { [3, 4] }
|
26
|
+
|
27
|
+
before { new_events.each { |event| subject.add_event(event) } }
|
28
|
+
|
29
|
+
it 'has changes' do
|
30
|
+
expect(subject.changes).to eq(new_events)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '#version' do
|
36
|
+
subject { described_class.new(events) }
|
37
|
+
|
38
|
+
context 'with no events at all' do
|
39
|
+
let(:events) { [] }
|
40
|
+
|
41
|
+
it 'defaults to zero' do
|
42
|
+
expect(subject.version).to eq(0)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'with events before initialization' do
|
47
|
+
let(:events) { [1, 2] }
|
48
|
+
|
49
|
+
it { expect(subject.version).to eq(events.count) }
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'with an events before and after initialization' do
|
53
|
+
let(:events) { [1, 2] }
|
54
|
+
let(:new_events) { [3, 4] }
|
55
|
+
|
56
|
+
before { new_events.each { |event| subject.add_event(event) } }
|
57
|
+
|
58
|
+
it { expect(subject.version).to eq(new_events.count + events.count) }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe '#initial_version' do
|
63
|
+
subject { described_class.new(events) }
|
64
|
+
|
65
|
+
context 'with no events at all' do
|
66
|
+
let(:events) { [] }
|
67
|
+
|
68
|
+
it { expect(subject.initial_version).to eq(0) }
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'with events before initialization' do
|
72
|
+
let(:events) { [1, 2] }
|
73
|
+
|
74
|
+
it { expect(subject.initial_version).to eq(events.count) }
|
75
|
+
end
|
76
|
+
|
77
|
+
context 'with an events before and after initialization' do
|
78
|
+
let(:events) { [1, 2] }
|
79
|
+
let(:new_events) { [3, 4] }
|
80
|
+
|
81
|
+
before { new_events.each { |event| subject.add_event(event) } }
|
82
|
+
|
83
|
+
it { expect(subject.initial_version).to eq(events.count) }
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe '#dirty?' do
|
88
|
+
subject { described_class.new(events) }
|
89
|
+
|
90
|
+
context 'no changes' do
|
91
|
+
let(:events) { [1, 2] }
|
92
|
+
|
93
|
+
it { expect(subject.dirty?).to eq(false) }
|
94
|
+
end
|
95
|
+
|
96
|
+
context 'with an events before and after initialization' do
|
97
|
+
let(:events) { [1, 2] }
|
98
|
+
let(:new_events) { [3, 4] }
|
99
|
+
|
100
|
+
before { new_events.each { |event| subject.add_event(event) } }
|
101
|
+
|
102
|
+
it { expect(subject.dirty?).to eq(true) }
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe '#commit' do
|
107
|
+
subject { described_class.new(events) }
|
108
|
+
|
109
|
+
context 'with no changes' do
|
110
|
+
let(:events) { [1, 2] }
|
111
|
+
|
112
|
+
before { subject.commit }
|
113
|
+
|
114
|
+
it { expect(subject.changes).to eq([]) }
|
115
|
+
end
|
116
|
+
|
117
|
+
context 'with an events before and after initialization' do
|
118
|
+
let(:events) { [1, 2] }
|
119
|
+
let(:new_events) { [3, 4] }
|
120
|
+
|
121
|
+
before do
|
122
|
+
new_events.each { |event| subject.add_event(event) }
|
123
|
+
subject.commit
|
124
|
+
end
|
125
|
+
|
126
|
+
it { expect(subject.changes).to eq([]) }
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ev
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Conger-Eldeen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-08-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -44,7 +44,8 @@ files:
|
|
44
44
|
- spec/lib/ev/event_source_spec.rb
|
45
45
|
- spec/spec_helper.rb
|
46
46
|
homepage: https://www.github.com/mceldeen/ev
|
47
|
-
licenses:
|
47
|
+
licenses:
|
48
|
+
- MIT
|
48
49
|
metadata: {}
|
49
50
|
post_install_message:
|
50
51
|
rdoc_options: []
|
@@ -62,8 +63,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
63
|
version: '0'
|
63
64
|
requirements: []
|
64
65
|
rubyforge_project:
|
65
|
-
rubygems_version: 2.
|
66
|
+
rubygems_version: 2.6.11
|
66
67
|
signing_key:
|
67
68
|
specification_version: 4
|
68
69
|
summary: A set of useful stuff for event sourcing.
|
69
|
-
test_files:
|
70
|
+
test_files:
|
71
|
+
- spec/lib/ev/event_source_spec.rb
|
72
|
+
- spec/spec_helper.rb
|