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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 519d2e75ae0484e19b36a27ecf644fba4fb181c0
4
- data.tar.gz: 39fcfb0b74667175c4833bb6ebf120771e8f8d99
3
+ metadata.gz: e05fef9f1259cacab26fbd2489dda9ca6b973863
4
+ data.tar.gz: cce9abb499135d77b2403cb349ffc7620737b74a
5
5
  SHA512:
6
- metadata.gz: db73cea764d6df16a4a18a6362a61f792ebb2eca51bc660028709150587130e719ada04bfecdc5a80ffc44464a6bfbf274910449e8fbf560869c34fbc68e19db
7
- data.tar.gz: 4e5a6129aecc2d9259424891800e812a3362975dada68dfcbb75ef183192ed2a7d47f405240260e8c35122246886a2a65ca71e187d08fa84f3a2a09e76fb8a4f
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("../lib", __FILE__)
3
- require "ev/version"
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'ev/version'
4
4
 
5
5
  Gem::Specification.new do |s|
6
- s.name = "ev"
6
+ s.name = 'ev'
7
7
  s.version = EV::VERSION
8
8
  s.platform = Gem::Platform::RUBY
9
- s.authors = ["Matthew Conger-Eldeen"]
10
- s.email = ["mceldeen@gmail.com"]
11
- s.homepage = "https://www.github.com/mceldeen/ev"
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 "rspec", "~>3.2"
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 = ["lib"]
21
+ s.require_paths = ['lib']
21
22
  end
@@ -1,58 +1,47 @@
1
1
  module EV
2
- class EventSource
3
- def self.new_from_events(events)
4
- allocate.tap do |root|
5
- root.initialize_from_events(events)
6
- end
7
- end
8
-
9
- def initialize_from_events(events)
10
- events.each do |event|
11
- apply_without_changes(event)
12
- end
13
- end
14
-
15
- def version
16
- @version || 0
17
- end
18
-
19
- def changes
20
- if @changes
21
- @changes.dup
22
- else
23
- []
24
- end
25
- end
26
-
27
- def initial_version
28
- version - changes.size
29
- end
30
-
31
- def dirty?
32
- changes.size > 0
33
- end
34
-
35
- def commit
36
- @changes.clear if @changes
37
- end
38
-
39
- protected
40
-
41
- def handle(event)
42
- raise NotImplementedError
43
- end
44
-
45
- def apply(event)
46
- apply_without_changes(event)
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,3 +1,3 @@
1
1
  module EV
2
- VERSION = "0.0.4"
2
+ VERSION = '0.1.0'
3
3
  end
@@ -1,113 +1,129 @@
1
1
  require 'spec_helper'
2
- require_relative '../../../lib/ev/event_source'
3
-
4
- module EV
5
- class DummyModel < EventSource
6
- def handle(event)
7
- end
8
- end
9
-
10
- describe EventSource do
11
- describe ".new_from_events" do
12
- subject { EventSource }
13
-
14
- it "does not call new" do
15
- expect(subject).to_not receive(:new)
16
- subject.new_from_events([])
17
- end
18
-
19
- describe "with events" do
20
- before do
21
- root = EventSource.new
22
- expect(root).to receive(:handle).twice
23
- expect(subject).to receive(:allocate).and_return(root)
24
- end
25
-
26
- it "calls handle for each event" do
27
- subject.new_from_events([1, 2])
28
- end
29
-
30
- it "does not show changes" do
31
- root = subject.new_from_events([1, 2])
32
- expect(root.changes).to eq([])
33
- end
34
-
35
- it "increments the verison number" do
36
- root = subject.new_from_events([1, 2])
37
- expect(root.version).to eq(2)
38
- end
39
- end
40
- end
41
-
42
- describe "#apply" do
43
- subject { EventSource.new }
44
- before do
45
- @event = {foo: :bar}
46
- expect(subject).to receive(:handle).with(@event)
47
- end
48
-
49
- it "calls handle" do
50
- subject.send(:apply, @event)
51
- end
52
-
53
- it "increments version" do
54
- expect(subject.version).to eq(0)
55
- subject.send(:apply, @event)
56
- expect(subject.version).to eq(1)
57
- end
58
-
59
- it "shows changes" do
60
- expect(subject.changes).to eq([])
61
- subject.send(:apply, @event)
62
- expect(subject.changes).to eq([@event])
63
- end
64
-
65
- it "duplicates the changes list" do
66
- subject.send(:apply, @event)
67
- subject.changes.shift
68
- expect(subject.changes.size).to eq(1)
69
- end
70
- end
71
-
72
- describe "#initial_version" do
73
- subject { DummyModel.new_from_events([1, 2])}
74
-
75
- it "works with no changes" do
76
- expect(subject.initial_version).to eq(2)
77
- end
78
-
79
- it "works with changes" do
80
- subject.send(:apply, 3)
81
- expect(subject.initial_version).to eq(2)
82
- end
83
- end
84
-
85
- describe "#dirty?" do
86
- subject { DummyModel.new_from_events([1,2]) }
87
-
88
- it "works with no changes" do
89
- expect(subject.dirty?).to eq(false)
90
- end
91
-
92
- it "works with changes" do
93
- subject.send(:apply, 3)
94
- expect(subject.dirty?).to eq(true)
95
- end
96
- end
97
-
98
- describe "#commit" do
99
- subject { DummyModel.new_from_events([1,2]) }
100
-
101
- it "works with no changes" do
102
- subject.commit
103
- expect(subject.dirty?).to eq(false)
104
- end
105
-
106
- it "works with changes" do
107
- subject.send(:apply, 3)
108
- subject.commit
109
- expect(subject.dirty?).to eq(false)
110
- end
111
- end
112
- end
113
- end
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
@@ -1,3 +1,5 @@
1
+ require_relative '../lib/ev'
2
+
1
3
  RSpec.configure do |config|
2
4
  config.expect_with :rspec do |expectations|
3
5
  expectations.include_chain_clauses_in_custom_matcher_descriptions = true
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
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: 2015-05-07 00:00:00.000000000 Z
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.4.5
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