sandthorn 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f99d35485b8746cbbe8ea792d915d764344a689c
4
- data.tar.gz: d3da0a1e657e55b9fdc283a4d549d1ea4c5c1a7b
3
+ metadata.gz: 10d8ea5557695337009c236ea93deaf0eef47d13
4
+ data.tar.gz: d9f2920cb7b31df316859f51ed76864a360b6ccd
5
5
  SHA512:
6
- metadata.gz: 4c9b2225374dc3626f14ef756c011438c0347e9e4787f53a3d74161e49c6875d9bcc2418e0fa4574e69b4b86d39c05c0348a52b58e78f4402028816ae81aae4b
7
- data.tar.gz: 7c56c84f45d5c352e669e874a050fd62557a7c764e459d546c429f6cd544f9dcd7a28be55e2d0f116b49b90c265ee2c23fb03034b4b34a3dce8f19aa87ea3d8d
6
+ metadata.gz: bf97fec50cc1c40ff231a1907a4e7e6a134338766329e7eadb69220047b06a3307a18fa1c703952ac497477d78a10480cc80504735d8239102fda77b7f1c895c
7
+ data.tar.gz: dd51c3758eb294f6f766f7b5fa5d7fc5fe31facdf76bc0eeaa95d44e5c55aae0d898cce785653cbda7104e733d7e201f46c50da430edba179cea9c2ab6c4bda7
@@ -1,18 +1,11 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sandthorn (0.3.0)
5
- dirty_hashy
4
+ sandthorn (0.4.0)
6
5
 
7
6
  GEM
8
7
  remote: https://rubygems.org/
9
8
  specs:
10
- activesupport (4.0.4)
11
- i18n (~> 0.6, >= 0.6.9)
12
- minitest (~> 4.2)
13
- multi_json (~> 1.3)
14
- thread_safe (~> 0.1)
15
- tzinfo (~> 0.3.37)
16
9
  autotest-standalone (4.5.11)
17
10
  awesome_print (1.2.0)
18
11
  coderay (1.1.0)
@@ -23,14 +16,10 @@ GEM
23
16
  term-ansicolor
24
17
  thor
25
18
  diff-lcs (1.2.5)
26
- dirty_hashy (0.2.1)
27
- activesupport
28
19
  docile (1.1.3)
29
20
  gem-release (0.7.1)
30
- i18n (0.6.9)
31
21
  method_source (0.8.2)
32
22
  mime-types (2.2)
33
- minitest (4.7.5)
34
23
  multi_json (1.9.2)
35
24
  pg (0.17.1)
36
25
  pry (0.9.12.6)
@@ -65,9 +54,7 @@ GEM
65
54
  term-ansicolor (1.3.0)
66
55
  tins (~> 1.0)
67
56
  thor (0.19.1)
68
- thread_safe (0.3.3)
69
57
  tins (1.1.0)
70
- tzinfo (0.3.39)
71
58
  yard (0.8.7.4)
72
59
 
73
60
  PLATFORMS
@@ -95,50 +95,52 @@ module Sandthorn
95
95
  end
96
96
 
97
97
  def aggregate_build events
98
- first_event = events.first()
99
98
  current_aggregate_version = 0
100
- if first_event[:event_name] == "aggregate_set_from_snapshot"
101
- aggregate = first_event[:event_args][0]
99
+ if first_event_snapshot?(events)
100
+ aggregate = start_build_from_snapshot events
102
101
  current_aggregate_version = aggregate.aggregate_originating_version
103
102
  events.shift
104
103
  else
105
- new_args = events.first()[:event_args][:method_args]
106
-
107
- if new_args.nil?
108
- aggregate = new
109
- else
110
- aggregate = new *new_args
111
- end
112
- aggregate.send :aggregate_clear_current_event_version!
104
+ aggregate = start_build_from_new events
113
105
  end
114
-
115
- attributes = {}
116
- events.each do |event|
106
+ attributes = build_instance_vars_from_events events
107
+ current_aggregate_version = events.last[:aggregate_version] unless events.empty?
108
+ aggregate.send :clear_aggregate_events
109
+ aggregate.send :set_orginating_aggregate_version!, current_aggregate_version
110
+ aggregate.send :set_current_aggregate_version!, current_aggregate_version
111
+ aggregate.send :aggregate_initialize
112
+ aggregate.send :set_instance_variables!, attributes
113
+ aggregate
114
+ end
115
+ private
116
+ def build_instance_vars_from_events events
117
+ events.inject({}) do |instance_vars, event |
117
118
  event_args = event[:event_args]
118
119
  event_name = event[:event_name]
119
-
120
- next if event_name == "aggregate_set_from_snapshot"
121
- next if event_name == "instance_extended_as_aggregate"
122
-
123
120
  attribute_deltas = event_args[:attribute_deltas]
124
-
125
- unless event[:aggregate_version].nil?
126
- current_aggregate_version = event[:aggregate_version]
127
- end
128
-
129
121
  unless attribute_deltas.nil?
130
122
  deltas = attribute_deltas.each_with_object({}) do |delta, acc|
131
123
  acc[delta[:attribute_name]] = delta[:new_value]
132
124
  end
133
-
134
- attributes.merge! deltas
125
+ instance_vars.merge! deltas
135
126
  end
127
+ instance_vars
136
128
  end
137
- aggregate.send :clear_aggregate_events
138
- aggregate.send :set_orginating_aggregate_version!, current_aggregate_version
139
- aggregate.send :set_current_aggregate_version!, current_aggregate_version
140
- aggregate.send :aggregate_initialize
141
- aggregate.send :set_instance_variables!, attributes
129
+ end
130
+ def first_event_snapshot? events
131
+ events.first[:event_name].to_sym == :aggregate_set_from_snapshot
132
+ end
133
+ def start_build_from_snapshot events
134
+ snapshot = events.first[:event_args][0]
135
+ end
136
+ def start_build_from_new events
137
+ new_args = events.first[:event_args][:method_args]
138
+ if new_args.nil?
139
+ aggregate = new
140
+ else
141
+ aggregate = new *new_args
142
+ end
143
+ aggregate.send :aggregate_clear_current_event_version!
142
144
  aggregate
143
145
  end
144
146
  end
@@ -2,12 +2,14 @@ module Sandthorn
2
2
  module AggregateRootSnapshot
3
3
  attr_reader :aggregate_snapshot
4
4
 
5
- def aggregate_snapshot!
6
-
7
- if @aggregate_events.count > 0
8
- raise "Can't take snapshot on object with unsaved events"
9
- end
5
+ def snapshot
6
+ aggregate_snapshot!
7
+ save_snapshot
8
+ self
9
+ end
10
10
 
11
+ def aggregate_snapshot!
12
+ raise Errors::SnapshotError.new "Can't take snapshot on object with unsaved events" if @aggregate_events.count > 0
11
13
  @aggregate_snapshot = {
12
14
  :event_name => "aggregate_set_from_snapshot",
13
15
  :event_args => [self],
@@ -16,23 +18,10 @@ module Sandthorn
16
18
  end
17
19
 
18
20
  def save_snapshot
19
- raise "No snapshot has been created!" unless @aggregate_snapshot
20
- @aggregate_snapshot[:event_data] = Sandthorn.serialize @aggregate_snapshot[:event_args]
21
+ raise Errors::SnapshotError.new "No snapshot has been created!" unless aggregate_snapshot
22
+ @aggregate_snapshot[:event_data] = Sandthorn.serialize aggregate_snapshot[:event_args]
21
23
  @aggregate_snapshot[:event_args] = nil
22
- Sandthorn.save_snapshot @aggregate_snapshot, @aggregate_id, self.class.name
23
- @aggregate_snapshot = nil
24
- end
25
- private
26
- def aggregate_create_event_when_extended
27
- self.aggregate_snapshot!
28
- vars = extract_relevant_aggregate_instance_variables
29
- vars.each do |var_name|
30
- value = instance_variable_get var_name
31
- dump = Marshal.dump(value)
32
- store_aggregate_instance_variable var_name, dump
33
- end
34
- #@aggregate_snapshot[:event_data] = Sandthorn.serialize @aggregate_snapshot[:event_args]
35
- store_aggregate_event "instance_extended_as_aggregate", @aggregate_snapshot[:event_args]
24
+ Sandthorn.save_snapshot aggregate_snapshot, aggregate_id, self.class.name
36
25
  @aggregate_snapshot = nil
37
26
  end
38
27
  end
@@ -4,5 +4,6 @@ module Sandthorn
4
4
  class AggregateNotFound < Error; end
5
5
  class ConcurrencyError < Error; end
6
6
  class ConfigurationError < Error; end
7
+ class SnapshotError < Error; end
7
8
  end
8
9
  end
@@ -1,3 +1,3 @@
1
1
  module Sandthorn
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
+ spec.required_ruby_version = '>= 2.0'
20
21
 
21
22
  spec.add_development_dependency "bundler", "~> 1.3"
22
23
  spec.add_development_dependency "rake"
@@ -29,7 +30,4 @@ Gem::Specification.new do |spec|
29
30
  spec.add_development_dependency "sqlite3"
30
31
  spec.add_development_dependency "coveralls"
31
32
  spec.add_development_dependency "sandthorn_driver_sequel", "~> 1.1"
32
-
33
- spec.add_runtime_dependency "dirty_hashy"
34
-
35
33
  end
@@ -211,10 +211,24 @@ describe 'when generating state on an aggregate root' do
211
211
 
212
212
  end
213
213
 
214
+ describe Sandthorn::AggregateRootSnapshot do
215
+ let(:subject) { a_test_account.save.extend Sandthorn::AggregateRootSnapshot }
216
+ context "when using :snapshot - method" do
217
+ it "should return self" do
218
+ expect(subject.snapshot).to eql subject
219
+ end
220
+ it "should raise SnapshotError if aggregate has unsaved events" do
221
+ subject.paid_with_visa_card_event 2000, ""
222
+ expect{subject.snapshot}.to raise_error Sandthorn::Errors::SnapshotError
223
+ end
224
+ end
225
+ end
226
+
227
+
214
228
  describe 'when saving to repository' do
215
229
  let(:account) {a_test_account.extend Sandthorn::AggregateRootSnapshot}
216
230
  it 'should raise an error if trying to save before creating a snapshot' do
217
- lambda {account.save_snapshot}.should raise_error (RuntimeError)
231
+ lambda {account.save_snapshot}.should raise_error (Sandthorn::Errors::SnapshotError)
218
232
  end
219
233
  it 'should not raise an error if snapshot was created' do
220
234
  account.save
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'sandthorn/aggregate_root_dirty_hashy'
3
2
  require 'benchmark'
4
3
 
5
4
  module Sandthorn
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sandthorn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lars Krantz
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-04-08 00:00:00.000000000 Z
12
+ date: 2014-04-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -165,20 +165,6 @@ dependencies:
165
165
  - - "~>"
166
166
  - !ruby/object:Gem::Version
167
167
  version: '1.1'
168
- - !ruby/object:Gem::Dependency
169
- name: dirty_hashy
170
- requirement: !ruby/object:Gem::Requirement
171
- requirements:
172
- - - ">="
173
- - !ruby/object:Gem::Version
174
- version: '0'
175
- type: :runtime
176
- prerelease: false
177
- version_requirements: !ruby/object:Gem::Requirement
178
- requirements:
179
- - - ">="
180
- - !ruby/object:Gem::Version
181
- version: '0'
182
168
  description: Event sourcing gem
183
169
  email:
184
170
  - lars.krantz@alaz.se
@@ -203,7 +189,6 @@ files:
203
189
  - lib/sandthorn.rb
204
190
  - lib/sandthorn/aggregate_root.rb
205
191
  - lib/sandthorn/aggregate_root_base.rb
206
- - lib/sandthorn/aggregate_root_dirty_hashy.rb
207
192
  - lib/sandthorn/aggregate_root_marshal.rb
208
193
  - lib/sandthorn/aggregate_root_snapshot.rb
209
194
  - lib/sandthorn/errors.rb
@@ -233,7 +218,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
233
218
  requirements:
234
219
  - - ">="
235
220
  - !ruby/object:Gem::Version
236
- version: '0'
221
+ version: '2.0'
237
222
  required_rubygems_version: !ruby/object:Gem::Requirement
238
223
  requirements:
239
224
  - - ">="
@@ -1,38 +0,0 @@
1
- require "dirty_hashy"
2
- require "sandthorn/aggregate_root_base"
3
-
4
- module Sandthorn
5
- module AggregateRoot
6
- module DirtyHashy
7
- include Sandthorn::AggregateRoot::Base
8
-
9
- def aggregate_initialize *args
10
- @hashy = ::DirtyHashy.new
11
- end
12
-
13
- def set_instance_variables! attribute
14
- super attribute
15
-
16
- extract_relevant_aggregate_instance_variables.each do |var|
17
- next if var.to_s == "@hashy"
18
- @hashy[var.to_s.delete("@")] = self.instance_variable_get("#{var}")
19
- end
20
- @hashy.clean_up!
21
- end
22
-
23
- def get_delta
24
- extract_relevant_aggregate_instance_variables.each do |var|
25
- next if var.to_s == "@hashy"
26
- @hashy[var.to_s.delete("@")] = self.instance_variable_get("#{var}")
27
- end
28
- aggregate_attribute_deltas = []
29
- @hashy.changes.each do |attribute|
30
- aggregate_attribute_deltas << { :attribute_name => attribute[0], :old_value => attribute[1][0], :new_value => attribute[1][1]}
31
- end
32
- @hashy.clean_up!
33
- aggregate_attribute_deltas
34
- end
35
-
36
- end
37
- end
38
- end