synapse-mongo 0.2.0 → 0.3.1

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.
@@ -3,6 +3,7 @@ require 'mongo'
3
3
  require 'synapse'
4
4
 
5
5
  require 'synapse/mongo/version'
6
+ require 'synapse/configuration/mongo'
6
7
 
7
8
  module Synapse
8
9
  module Common
@@ -0,0 +1,10 @@
1
+ require 'synapse/configuration/event_store/mongo/event_store'
2
+
3
+ module Synapse
4
+ module Configuration
5
+ class ContainerBuilder
6
+ # Creates and configures a Mongo event store
7
+ builder :mongo_event_store, MongoEventStoreDefinitionBuilder
8
+ end # ContainerBuilder
9
+ end # Configuration
10
+ end
@@ -0,0 +1,96 @@
1
+ module Synapse
2
+ module Configuration
3
+ # Definition builder used to create a Mongo-backed event store
4
+ #
5
+ # @example The minimum possible effort to build an event store
6
+ # mongo_event_store do
7
+ # use_client Mongo::MongoClient.new
8
+ # end
9
+ #
10
+ # @example Mix it up a bit
11
+ # mongo_event_store :alt_event_store do
12
+ # use_per_event_strategy
13
+ #
14
+ # use_serializer :alt_serializer
15
+ # use_upcaster_chain :alt_upcaster_chain
16
+ # use_template :alt_template
17
+ # end
18
+ class MongoEventStoreDefinitionBuilder < DefinitionBuilder
19
+ # Convenience method that creates an event store template with the given Mongo client
20
+ #
21
+ # @param [Mongo::MongoClient] client
22
+ # @return [undefined]
23
+ def use_client(client)
24
+ @template = EventStore::Mongo::Template.new client
25
+ end
26
+
27
+ # Changes the type of storage strategy to document-per-commit
28
+ #
29
+ # @see EventStore::Mongo::DocumentPerCommitStrategy
30
+ # @return [undefined]
31
+ def use_per_commit_strategy
32
+ @storage_strategy_type = EventStore::Mongo::DocumentPerCommitStrategy
33
+ end
34
+
35
+ # Changes the type of storage strategy to document-per-event
36
+ #
37
+ # @see EventStore::Mongo::DocumentPerEventStrategy
38
+ # @return [undefined]
39
+ def use_per_event_strategy
40
+ @storage_strategy_type = EventStore::Mongo::DocumentPerEventStrategy
41
+ end
42
+
43
+ # Changes the serializer to use with this event store
44
+ #
45
+ # @see Serialization::Serializer
46
+ # @param [Symbol] serializer
47
+ # @return [undefined]
48
+ def use_serializer(serializer)
49
+ @serializer = serializer
50
+ end
51
+
52
+ # Changes the Mongo template to use with this event store
53
+ #
54
+ # @see EventStore::Mongo::Template
55
+ # @param [Symbol] template
56
+ # @return [undefined]
57
+ def use_template(template)
58
+ @template = template
59
+ end
60
+
61
+ # Changes the upcaster chain to use with this event store
62
+ #
63
+ # @see Upcasting::UpcasterChain
64
+ # @param [Symbol] upcaster_chain
65
+ # @return [undefined]
66
+ def use_upcaster_chain(upcaster_chain)
67
+ @upcaster_chain = upcaster_chain
68
+ end
69
+
70
+ protected
71
+
72
+ # @return [undefined]
73
+ def populate_defaults
74
+ identified_by :event_store
75
+
76
+ use_serializer :serializer
77
+ use_per_commit_strategy
78
+ use_template :mongo_event_store_template
79
+ use_upcaster_chain :upcaster_chain
80
+
81
+ use_factory do
82
+ serializer = resolve @serializer
83
+ template = resolve @template
84
+ upcaster_chain = resolve @upcaster_chain
85
+
86
+ strategy = @storage_strategy_type.new template, serializer, upcaster_chain
87
+
88
+ event_store = EventStore::Mongo::MongoEventStore.new template, strategy
89
+ event_store.ensure_indexes
90
+
91
+ event_store
92
+ end
93
+ end
94
+ end # MongoEventStoreDefinitionBuilder
95
+ end # Configuration
96
+ end
@@ -0,0 +1,2 @@
1
+ require 'synapse/configuration/event_store/mongo'
2
+ require 'synapse/configuration/process_manager/mongo'
@@ -0,0 +1,10 @@
1
+ require 'synapse/configuration/process_manager/mongo/process_repository'
2
+
3
+ module Synapse
4
+ module Configuration
5
+ class ContainerBuilder
6
+ # Creates and configures a Mongo process repository
7
+ builder :mongo_process_repository, MongoProcessRepositoryDefinitionBuilder
8
+ end # ContainerBuilder
9
+ end # Configuration
10
+ end
@@ -0,0 +1,77 @@
1
+ module Synapse
2
+ module Configuration
3
+ # Definition builder used to create a Mongo-backed process repository
4
+ #
5
+ # @example The minimum possible effort to build a process repository
6
+ # mongo_process_repository do
7
+ # use_client Mongo::MongoClient.new
8
+ # end
9
+ #
10
+ # @example Mix it up a bit
11
+ # mongo_process_repository :alt_process_repository do
12
+ # use_resource_injector :alt_resource_injector
13
+ # use_serializer :alt_serializer
14
+ # use_template :alt_template
15
+ # end
16
+ class MongoProcessRepositoryDefinitionBuilder < DefinitionBuilder
17
+ # Convenience method that creates a process repository template with the given Mongo client
18
+ #
19
+ # @param [Mongo::MongoClient] client
20
+ # @return [undefined]
21
+ def use_client(client)
22
+ @template = ProcessManager::Mongo::Template.new client
23
+ end
24
+
25
+ # Changes the serializer to use with this process repository
26
+ #
27
+ # @see Serialization::Serializer
28
+ # @param [Symbol] serializer
29
+ # @return [undefined]
30
+ def use_serializer(serializer)
31
+ @serializer = serializer
32
+ end
33
+
34
+ # Changes the Mongo template to use with this process repository
35
+ #
36
+ # @see ProcessManager::Mongo::Template
37
+ # @param [Symbol] template
38
+ # @return [undefined]
39
+ def use_template(template)
40
+ @template = template
41
+ end
42
+
43
+ # Changes the resource injector to use with this process repository
44
+ #
45
+ # @see ProcessManager::ResourceInjector
46
+ # @param [Symbol] resource_injector
47
+ # @return [undefined]
48
+ def use_resource_injector(resource_injector)
49
+ @resource_injector = resource_injector
50
+ end
51
+
52
+ protected
53
+
54
+ # @return [undefined]
55
+ def populate_defaults
56
+ identified_by :process_repository
57
+
58
+ use_resource_injector :resource_injector
59
+ use_serializer :serializer
60
+ use_template :mongo_process_repository_template
61
+
62
+ use_factory do
63
+ serializer = resolve @serializer
64
+ template = resolve @template
65
+ resource_injector = resolve @resource_injector, true
66
+
67
+ process_repository = ProcessManager::Mongo::MongoProcessRepository.new serializer, template
68
+ if resource_injector
69
+ process_factory.resource_injector = resource_injector
70
+ end
71
+
72
+ process_repository
73
+ end
74
+ end
75
+ end # MongoProcessRepositoryDefinitionBuilder
76
+ end # Configuration
77
+ end
@@ -75,9 +75,7 @@ module Synapse
75
75
  def load_last_snapshot(type_identifier, aggregate_id)
76
76
  cursor = @storage_strategy.fetch_last_snapshot type_identifier, aggregate_id
77
77
 
78
- unless cursor.has_next?
79
- return
80
- end
78
+ return unless cursor.has_next?
81
79
 
82
80
  first = cursor.next_document
83
81
  @storage_strategy.extract_events first, aggregate_id
@@ -1,5 +1,5 @@
1
1
  module Synapse
2
2
  module Mongo
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.1'
4
4
  end
5
5
  end
@@ -5,7 +5,7 @@ module Synapse
5
5
  module Mongo
6
6
 
7
7
  class BaseTemplateTest < Test::Unit::TestCase
8
- def test_database
8
+ should 'connect and authenticate to database only once' do
9
9
  client = Object.new
10
10
  database = Object.new
11
11
 
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+
3
+ module Synapse
4
+ module Configuration
5
+ class MongoEventStoreDefinitionBuilderTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @container = Container.new
9
+ @builder = ContainerBuilder.new @container
10
+ end
11
+
12
+ should 'build with sensible defaults' do
13
+ @builder.converter_factory
14
+ @builder.serializer
15
+ @builder.upcaster_chain
16
+
17
+ @builder.mongo_event_store do
18
+ use_client ::Mongo::MongoClient.new
19
+ end
20
+
21
+ event_store = @container.resolve :event_store
22
+
23
+ assert_instance_of EventStore::Mongo::MongoEventStore, event_store
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+
3
+ module Synapse
4
+ module Configuration
5
+ class MongoProcessRepositoryDefinitionBuilderTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @container = Container.new
9
+ @builder = ContainerBuilder.new @container
10
+ end
11
+
12
+ should 'build with sensible defaults' do
13
+ @builder.converter_factory
14
+ @builder.serializer
15
+
16
+ @builder.mongo_process_repository do
17
+ use_client ::Mongo::MongoClient.new
18
+ end
19
+
20
+ repository = @container.resolve :process_repository
21
+
22
+ assert_instance_of ProcessManager::Mongo::MongoProcessRepository, repository
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -5,7 +5,7 @@ module Synapse
5
5
  module Mongo
6
6
 
7
7
  class MongoEventStoreTest < Test::Unit::TestCase
8
- def test_integration
8
+ should 'integrate with different storage strategies' do
9
9
  client = ::Mongo::MongoClient.new
10
10
  template = Template.new client
11
11
  template.event_collection.drop
@@ -16,7 +16,7 @@ module Synapse
16
16
  @repository = MongoProcessRepository.new serializer, template
17
17
  end
18
18
 
19
- def test_add
19
+ should 'persist processes to the database' do
20
20
  process = Process.new
21
21
 
22
22
  @repository.add process
@@ -26,7 +26,7 @@ module Synapse
26
26
  assert_equal 0, process.correlations.additions.count
27
27
  end
28
28
 
29
- def test_commit
29
+ should 'commit changes to processes to the database' do
30
30
  correlation = Correlation.new :order_id, '123'
31
31
 
32
32
  process = Process.new
@@ -45,7 +45,7 @@ module Synapse
45
45
  assert_nil @repository.load process.id
46
46
  end
47
47
 
48
- def test_find
48
+ should 'support finding processes by their correlation keys' do
49
49
  correlation_a = Correlation.new :order_id, '123'
50
50
  correlation_b = Correlation.new :order_id, '456'
51
51
 
@@ -3,7 +3,7 @@ require 'test_helper'
3
3
  module Synapse
4
4
  module Serialization
5
5
  class OrderedHashToHashConverterTest < Test::Unit::TestCase
6
- def test_convert
6
+ should 'convert a BSON ordered hash to a normal hash' do
7
7
  converter = OrderedHashToHashConverter.new
8
8
 
9
9
  assert_equal BSON::OrderedHash, converter.source_type
@@ -6,9 +6,12 @@ end
6
6
 
7
7
  require 'pp'
8
8
  require 'test/unit'
9
+ require 'shoulda/context'
9
10
  require 'rr'
10
11
  require 'synapse-mongo'
11
12
 
12
- class Test::Unit::TestCase
13
- include RR::Adapters::TestUnit
13
+ # I guess RR broke
14
+ # http://stackoverflow.com/questions/3657972
15
+ unless defined? Test::Unit::AssertionFailedError
16
+ Test::Unit::AssertionFailedError = ActiveSupport::TestCase::Assertion
14
17
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synapse-mongo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,24 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-14 00:00:00.000000000 Z
12
+ date: 2013-06-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: synapse-core
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ! '>='
19
+ - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 0.4.0
21
+ version: '0.5'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - ! '>='
27
+ - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: 0.4.0
29
+ version: '0.5'
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: mongo
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -51,6 +51,11 @@ extra_rdoc_files: []
51
51
  files:
52
52
  - lib/synapse-mongo.rb
53
53
  - lib/synapse/mongo/version.rb
54
+ - lib/synapse/configuration/process_manager/mongo/process_repository.rb
55
+ - lib/synapse/configuration/process_manager/mongo.rb
56
+ - lib/synapse/configuration/event_store/mongo/event_store.rb
57
+ - lib/synapse/configuration/event_store/mongo.rb
58
+ - lib/synapse/configuration/mongo.rb
54
59
  - lib/synapse/process_manager/mongo/process_repository.rb
55
60
  - lib/synapse/process_manager/mongo/process_document.rb
56
61
  - lib/synapse/process_manager/mongo/template.rb
@@ -65,6 +70,8 @@ files:
65
70
  - lib/synapse/common/mongo/base_template.rb
66
71
  - lib/synapse/serialization/converter/bson.rb
67
72
  - test/test_helper.rb
73
+ - test/configuration/mongo/process_manager_test.rb
74
+ - test/configuration/mongo/event_store_test.rb
68
75
  - test/process_manager/mongo/repository_test.rb
69
76
  - test/event_store/mongo/event_store_test.rb
70
77
  - test/common/mongo/base_template_test.rb