sandthorn 0.10.2 → 0.10.3

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: 4666c49404c1a108066a0144e81923b36e234466
4
- data.tar.gz: 648da2f2f66afa2458a47de3e29c4f0223b5c84f
3
+ metadata.gz: 567a5ca0b81764696a4a80323cabeca004109fc6
4
+ data.tar.gz: 90db28c455cc71ae90df453771b48b15d53a453e
5
5
  SHA512:
6
- metadata.gz: f706cb2aecabdce41cc4a975c1f7e3fca114956bb7280bca3a6aa96eae62df166a854bfecb48f3ba779724ad51b5af13c64285a5299105d7b1cf7c1bacdc426f
7
- data.tar.gz: 928e4ca497a3a401b77e538bb0dfba0290c9f0897c6bf8de01c0e62ec4726365a05754eb5be7e6ea047c88bda6148c1a066ab69ff7707530ac8c430ce2504476
6
+ metadata.gz: 3499edfb4c94c63c5d2a82954e125e274109697ad0101a33c484959971f6b8d07e0c57b6573197e7758328f45460cf34debaf7e0bb9244bab05329ddb14b55ac
7
+ data.tar.gz: 83bb3743729b952d596f4b6583343fb055933ef480f137e60734753aceee7cf9acc9ce8a5e06e72a3e0894d0c665b5b3eb58c2f4a73288e7d4fe7b9f66311053
data/README.md CHANGED
@@ -234,6 +234,18 @@ class Board
234
234
  end
235
235
  ```
236
236
 
237
+ ### `Sandthorn::AggregateRoot::default_attributes`
238
+
239
+ Its possible to add a default_attributes method on an aggregate and set default values to new and already created aggregates.
240
+
241
+ The default_attributes method will be run before initialize on Class.new and before the events when an aggregate is rebuilt. This will make is possible to add default attributes to an aggregate during its hole life cycle.
242
+
243
+ ```ruby
244
+ def default_attributes
245
+ @new_array = []
246
+ end
247
+ ```
248
+
237
249
  ### `Sandthorn::AggregateRoot.commit`
238
250
 
239
251
  It is required that an event is commited to the aggregate to be stored as an event. `commit` extracts the object's delta and locally caches the state changes that has been applied to the aggregate. Commonly, commit is called when an event is applied. In [CQRS](http://martinfowler.com/bliki/CQRS.html), events are named using past tense.
@@ -52,6 +52,10 @@ module Sandthorn
52
52
  commit_with_event_name(event_name, args)
53
53
  end
54
54
 
55
+ def default_attributes
56
+ #NOOP
57
+ end
58
+
55
59
  alias :record_event :commit
56
60
 
57
61
  module ClassMethods
@@ -98,18 +102,25 @@ module Sandthorn
98
102
  aggregate_build ([transformed_snapshot_event] + transformed_events).compact
99
103
  end
100
104
 
101
- def new *args
102
- super.tap do |aggregate|
103
- aggregate.aggregate_trace @@aggregate_trace_information do |aggr|
104
- aggr.aggregate_base_initialize
105
- aggr.aggregate_initialize
106
- aggr.send :set_aggregate_id, Sandthorn.generate_aggregate_id
107
- aggr.send :commit, *args
108
- return aggr
109
- end
105
+ def new *args, &block
106
+
107
+ aggregate = allocate
108
+ aggregate.aggregate_base_initialize
109
+ aggregate.aggregate_initialize
110
+
111
+ aggregate.default_attributes
112
+ aggregate.send :initialize, *args, &block
113
+ aggregate.send :set_aggregate_id, Sandthorn.generate_aggregate_id
114
+
115
+ aggregate.aggregate_trace @@aggregate_trace_information do |aggr|
116
+ aggr.send :commit, *args
117
+ return aggr
110
118
  end
119
+
111
120
  end
112
121
 
122
+
123
+
113
124
  def aggregate_build events
114
125
  current_aggregate_version = 0
115
126
 
@@ -124,9 +135,11 @@ module Sandthorn
124
135
  attributes = build_instance_vars_from_events events
125
136
  current_aggregate_version = events.last[:aggregate_version] unless events.empty?
126
137
  aggregate.send :clear_aggregate_events
138
+ aggregate.default_attributes
127
139
  aggregate.send :set_orginating_aggregate_version!, current_aggregate_version
128
140
  aggregate.send :set_current_aggregate_version!, current_aggregate_version
129
141
  aggregate.send :aggregate_initialize
142
+
130
143
  aggregate.send :set_instance_variables!, attributes
131
144
  aggregate
132
145
  end
@@ -1,3 +1,3 @@
1
1
  module Sandthorn
2
- VERSION = "0.10.2"
2
+ VERSION = "0.10.3"
3
3
  end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ class DefaultAttributes
4
+ include Sandthorn::AggregateRoot
5
+ def initialize
6
+ end
7
+ end
8
+
9
+
10
+ describe "when the initialize-method changes" do
11
+
12
+ it "should not have an array attribute on first version of the DefaultAttributes class" do
13
+ aggregate = DefaultAttributes.new
14
+ expect(aggregate.respond_to?(:array)).to be_falsy
15
+ end
16
+
17
+ context "default_attributes" do
18
+
19
+ def add_default_attributes
20
+ DefaultAttributes.class_eval do
21
+ attr_reader :array
22
+ define_method :default_attributes, lambda { @array = [] }
23
+ end
24
+ end
25
+
26
+ it "should have set the array attribute to [] on rebuilt " do
27
+ aggregate = DefaultAttributes.new
28
+ add_default_attributes
29
+ rebuilt_aggregate = DefaultAttributes.aggregate_build(aggregate.aggregate_events)
30
+ expect(rebuilt_aggregate.array).to eql []
31
+ end
32
+
33
+ it "should have an set the array attribute to [] on new" do
34
+ add_default_attributes
35
+ aggregate = DefaultAttributes.new
36
+ expect(aggregate.array).to eql []
37
+ end
38
+ end
39
+ 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.10.2
4
+ version: 0.10.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lars Krantz
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-01-27 00:00:00.000000000 Z
13
+ date: 2016-01-28 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -207,6 +207,7 @@ files:
207
207
  - spec/bounded_context_spec.rb
208
208
  - spec/complex_aggregate_spec.rb
209
209
  - spec/db/sequel_driver.sqlite3_old
210
+ - spec/default_attributes_spec.rb
210
211
  - spec/different_driver_spec.rb
211
212
  - spec/event_inspector_spec.rb
212
213
  - spec/event_spec.rb
@@ -250,6 +251,7 @@ test_files:
250
251
  - spec/bounded_context_spec.rb
251
252
  - spec/complex_aggregate_spec.rb
252
253
  - spec/db/sequel_driver.sqlite3_old
254
+ - spec/default_attributes_spec.rb
253
255
  - spec/different_driver_spec.rb
254
256
  - spec/event_inspector_spec.rb
255
257
  - spec/event_spec.rb