sandthorn 0.1.1 → 0.2.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: 570d52af1a092035a72a6db14afb0c43ec61ae48
4
- data.tar.gz: 39f92e0f586d6af40ec7d1b0c9eacdf7c08827ae
3
+ metadata.gz: 6b32292f067fc1a54b2a64d44a42c88c852489b8
4
+ data.tar.gz: 3da32e8fbd184194b7f99d0977742ab6cccc8f74
5
5
  SHA512:
6
- metadata.gz: 95e7d6c19f1c50f01a4a63d0fec9378205956c29b9fb2be22fded7e5fb9c219dde8edfae25b5a3c7f620b01610157b7deb6f2531b79932b66912f7605105a776
7
- data.tar.gz: b8e4d2e12afb2ea861b6e1136b9f3d263ed5fac7cd700a50969a3ecc556e3599e2200dbde479d99dda264fea753039719509d0f60a76489e98fee771ae7505b2
6
+ metadata.gz: 69576400bb2d0667765130a000cf41b2eba699066b9eb9bc2172af37efd7351742fb646762f172d4c778cf9b057583c3a612dbb77f7462c70aca894d386a1081
7
+ data.tar.gz: eba722ba78c620c688b4259962b1da42896ff13f65f10b7efd23399c4d55a2a3f2686494de213bd5f4c7ab04d9a8b6b621b25b6c6e9097911c97d85f97044bf8
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sandthorn (0.1.1)
4
+ sandthorn (0.2.0)
5
5
  dirty_hashy
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -23,10 +23,9 @@ _Example:_
23
23
  # The one available right now
24
24
  require 'sandthorn'
25
25
  require 'sandthorn_driver_sequel'
26
- require 'sandthorn/aggregate_root_dirty_hashy'
27
26
 
28
27
  class Ship
29
- include Sandthorn::AggregateRoot::DirtyHashy
28
+ include Sandthorn::AggregateRoot
30
29
  attr_reader :name
31
30
 
32
31
  def initialize name: nil, shipping_company: nil
@@ -65,7 +64,7 @@ ship.rename! new_name: "Vasa"
65
64
  ship.save
66
65
 
67
66
  new_ship = Ship.find ship.id
68
- puts ship.name
67
+ puts new_ship.name
69
68
 
70
69
  # For more info look at the specs.
71
70
 
data/lib/sandthorn.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "sandthorn/version"
2
2
  require "sandthorn/errors"
3
+ require "sandthorn/aggregate_root"
3
4
  require 'yaml'
4
5
  require 'securerandom'
5
6
 
@@ -0,0 +1,11 @@
1
+ require 'sandthorn/aggregate_root_marshal'
2
+
3
+ module Sandthorn
4
+ module AggregateRoot
5
+ include Sandthorn::AggregateRoot::Marshal
6
+
7
+ def self.included(base)
8
+ base.extend(Sandthorn::AggregateRoot::Base::ClassMethods)
9
+ end
10
+ end
11
+ end
@@ -137,8 +137,8 @@ module Sandthorn
137
137
  aggregate.send :clear_aggregate_events
138
138
  aggregate.send :set_orginating_aggregate_version!, current_aggregate_version
139
139
  aggregate.send :set_current_aggregate_version!, current_aggregate_version
140
- aggregate.send :set_instance_variables!, attributes
141
140
  aggregate.send :aggregate_initialize
141
+ aggregate.send :set_instance_variables!, attributes
142
142
  aggregate
143
143
  end
144
144
  end
@@ -6,10 +6,6 @@ module Sandthorn
6
6
  module DirtyHashy
7
7
  include Sandthorn::AggregateRoot::Base
8
8
 
9
- def self.included(base)
10
- base.extend(Sandthorn::AggregateRoot::Base::ClassMethods)
11
- end
12
-
13
9
  def aggregate_initialize *args
14
10
  @hashy = ::DirtyHashy.new
15
11
  end
@@ -0,0 +1,54 @@
1
+ require "sandthorn/aggregate_root_base"
2
+
3
+ module Sandthorn
4
+ module AggregateRoot
5
+ module Marshal
6
+ include Sandthorn::AggregateRoot::Base
7
+
8
+ def aggregate_initialize *args
9
+ @aggregate_attribute_deltas = []
10
+ @aggregate_stored_instance_variables = {}
11
+ end
12
+
13
+ def set_instance_variables! attribute
14
+ super attribute
15
+ init_vars = extract_relevant_aggregate_instance_variables
16
+ init_vars.each {|attribute_name| @aggregate_stored_instance_variables[attribute_name] = ::Marshal.dump(instance_variable_get(attribute_name)) }
17
+ end
18
+
19
+ def get_delta
20
+ deltas = extract_relevant_aggregate_instance_variables
21
+ deltas.each { |d| delta_attribute(d)}
22
+
23
+ result = @aggregate_attribute_deltas
24
+ clear_aggregate_deltas
25
+ result
26
+ end
27
+
28
+ private
29
+
30
+ def delta_attribute attribute_name
31
+ old_dump = @aggregate_stored_instance_variables[attribute_name]
32
+ new_dump = ::Marshal.dump(instance_variable_get(attribute_name))
33
+ unless old_dump == new_dump
34
+ store_attribute_deltas attribute_name, new_dump, old_dump
35
+ store_aggregate_instance_variable attribute_name, new_dump
36
+ end
37
+ end
38
+
39
+ def store_attribute_deltas attribute_name, new_dump, old_dump
40
+ new_value_to_store = ::Marshal.load(new_dump)
41
+ old_value_to_store = old_dump ? ::Marshal.load(old_dump) : nil
42
+ @aggregate_attribute_deltas << { :attribute_name => attribute_name.to_s.delete("@"), :old_value => old_value_to_store, :new_value => new_value_to_store}
43
+ end
44
+
45
+ def store_aggregate_instance_variable attribute_name, new_dump
46
+ @aggregate_stored_instance_variables[attribute_name] = new_dump
47
+ end
48
+
49
+ def clear_aggregate_deltas
50
+ @aggregate_attribute_deltas = []
51
+ end
52
+ end
53
+ end
54
+ end
@@ -1,3 +1,3 @@
1
1
  module Sandthorn
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,10 +1,9 @@
1
1
  require 'spec_helper'
2
- require 'sandthorn/aggregate_root_dirty_hashy'
3
2
 
4
3
 
5
4
 
6
5
  class PersonTest
7
- include Sandthorn::AggregateRoot::DirtyHashy
6
+ include Sandthorn::AggregateRoot
8
7
  attr_reader :name
9
8
  attr_reader :age
10
9
  attr_reader :relationship_status
@@ -1,10 +1,9 @@
1
1
  require 'spec_helper'
2
- require 'sandthorn/aggregate_root_dirty_hashy'
3
2
 
4
3
  module Sandthorn
5
4
  module AggregateRoot
6
5
  class DirtyClass
7
- include Sandthorn::AggregateRoot::DirtyHashy
6
+ include Sandthorn::AggregateRoot
8
7
  attr_reader :name, :age
9
8
  attr :sex
10
9
  attr_writer :writer
@@ -25,12 +24,14 @@ module Sandthorn
25
24
  def change_sex value
26
25
  unless sex == value
27
26
  @sex = value
27
+ commit
28
28
  end
29
29
  end
30
30
 
31
31
  def change_writer value
32
32
  unless writer == value
33
33
  @writer = value
34
+ commit
34
35
  end
35
36
  end
36
37
 
@@ -143,10 +144,21 @@ module Sandthorn
143
144
  describe "event data" do
144
145
 
145
146
  let(:dirty_obejct) {
146
- o = DirtyClass.new :name => "old_value"
147
- o
147
+ o = DirtyClass.new :name => "old_value", :sex => "hen"
148
+ o.save
148
149
  }
149
150
 
151
+ let(:dirty_obejct_after_find) { DirtyClass.find dirty_obejct.id }
152
+
153
+ context "after find" do
154
+
155
+ it "should set the old_value on the event" do
156
+ dirty_obejct_after_find.change_name "new_name"
157
+ expect(dirty_obejct_after_find.aggregate_events.last[:event_args][:attribute_deltas].first[:old_value]).to eql "old_value"
158
+ end
159
+
160
+ end
161
+
150
162
  context "old_value should be set" do
151
163
 
152
164
  it "should set the old_value on the event" do
@@ -159,12 +171,29 @@ module Sandthorn
159
171
  expect(dirty_obejct.aggregate_events.last[:event_args][:attribute_deltas].last[:attribute_name]).not_to eql "aggregate_id"
160
172
  end
161
173
 
162
- it "should not change sex attribute" do
174
+ it "should not change sex attribute if sex method is not runned" do
163
175
  dirty_obejct.change_name "new_name"
164
176
  dirty_obejct.aggregate_events.each do |event|
165
- expect(event[:event_name]).not_to eql "change_sex"
177
+ event[:event_args][:attribute_deltas].each do |attribute_delta|
178
+ expect(attribute_delta[:attribute_name]).not_to eql "sex"
179
+ end
180
+ end
181
+ end
182
+
183
+ it "should not change sex attribute if sex attribute is the same" do
184
+ dirty_obejct.change_sex "hen"
185
+ dirty_obejct.aggregate_events.each do |event|
186
+ event[:event_args][:attribute_deltas].each do |attribute_delta|
187
+ expect(attribute_delta[:attribute_name]).not_to eql "sex"
188
+ end
166
189
  end
167
190
  end
191
+
192
+ it "should set old_value and new_value on sex change" do
193
+ dirty_obejct.change_sex "shemale"
194
+ expect(dirty_obejct.aggregate_events.last[:event_args][:attribute_deltas].first[:old_value]).to eql "hen"
195
+ expect(dirty_obejct.aggregate_events.last[:event_args][:attribute_deltas].first[:new_value]).to eql "shemale"
196
+ end
168
197
  end
169
198
  end
170
199
  end
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'sandthorn/aggregate_root_dirty_hashy'
3
2
  require 'sandthorn/aggregate_root_snapshot'
4
3
  require 'date'
5
4
 
@@ -64,7 +63,7 @@ module BankAccountDepositCommmands
64
63
  end
65
64
 
66
65
  class BankAccount
67
- include Sandthorn::AggregateRoot::DirtyHashy
66
+ include Sandthorn::AggregateRoot
68
67
 
69
68
  attr_reader :balance
70
69
  attr_reader :account_number
@@ -5,7 +5,7 @@ require 'benchmark'
5
5
  module Sandthorn
6
6
  module AggregateRoot
7
7
  class TestClass
8
- include Sandthorn::AggregateRoot::DirtyHashy
8
+ include Sandthorn::AggregateRoot
9
9
  attr_reader :name
10
10
 
11
11
 
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'sandthorn/aggregate_root_dirty_hashy'
3
2
  require 'date'
4
3
 
5
4
  class Hello
@@ -16,7 +15,7 @@ class Hello
16
15
  end
17
16
 
18
17
  class IAmComplex
19
- include Sandthorn::AggregateRoot::DirtyHashy
18
+ include Sandthorn::AggregateRoot
20
19
  attr_reader :a_date
21
20
  attr_reader :hello
22
21
 
@@ -70,7 +69,6 @@ describe 'when using complex types in events' do
70
69
  it 'should detect foo_bar chaning in hello' do
71
70
  aggr = IAmComplex.aggregate_build @events
72
71
  aggr.set_foo_bar_on_hello "morgan"
73
-
74
72
  builded = IAmComplex.aggregate_build aggr.aggregate_events
75
73
  builded.hello.foo_bar.should eql "morgan"
76
74
  end
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.1.1
4
+ version: 0.2.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-02 00:00:00.000000000 Z
12
+ date: 2014-04-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -201,8 +201,10 @@ files:
201
201
  - README.md
202
202
  - Rakefile
203
203
  - lib/sandthorn.rb
204
+ - lib/sandthorn/aggregate_root.rb
204
205
  - lib/sandthorn/aggregate_root_base.rb
205
206
  - lib/sandthorn/aggregate_root_dirty_hashy.rb
207
+ - lib/sandthorn/aggregate_root_marshal.rb
206
208
  - lib/sandthorn/aggregate_root_snapshot.rb
207
209
  - lib/sandthorn/errors.rb
208
210
  - lib/sandthorn/event_inspector.rb