synapse-core 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +7 -0
  2. data/lib/synapse.rb +1 -1
  3. data/lib/synapse/command.rb +1 -1
  4. data/lib/synapse/command/command_handler.rb +1 -1
  5. data/lib/synapse/command/mapping.rb +71 -0
  6. data/lib/synapse/command/message.rb +0 -16
  7. data/lib/synapse/command/simple_command_bus.rb +5 -2
  8. data/lib/synapse/common/message.rb +16 -0
  9. data/lib/synapse/configuration.rb +1 -18
  10. data/lib/synapse/configuration/component/command_bus.rb +7 -24
  11. data/lib/synapse/configuration/component/command_bus/simple_command_bus.rb +31 -7
  12. data/lib/synapse/configuration/component/event_bus.rb +3 -8
  13. data/lib/synapse/configuration/component/event_sourcing.rb +11 -8
  14. data/lib/synapse/configuration/component/event_sourcing/aggregate_snapshot_taker.rb +48 -0
  15. data/lib/synapse/configuration/component/event_sourcing/interval_snapshot_policy.rb +33 -0
  16. data/lib/synapse/configuration/component/event_sourcing/repository.rb +36 -1
  17. data/lib/synapse/configuration/component/repository.rb +4 -8
  18. data/lib/synapse/configuration/component/serialization.rb +5 -16
  19. data/lib/synapse/configuration/component/uow.rb +3 -8
  20. data/lib/synapse/configuration/component/upcasting.rb +3 -8
  21. data/lib/synapse/configuration/container_builder.rb +29 -2
  22. data/lib/synapse/configuration/definition_builder.rb +47 -23
  23. data/lib/synapse/domain/message.rb +0 -16
  24. data/lib/synapse/event_bus.rb +1 -1
  25. data/lib/synapse/event_bus/event_listener.rb +1 -1
  26. data/lib/synapse/event_bus/mapping.rb +47 -0
  27. data/lib/synapse/event_sourcing.rb +3 -2
  28. data/lib/synapse/event_sourcing/aggregate_factory.rb +4 -3
  29. data/lib/synapse/event_sourcing/aggregate_root.rb +17 -0
  30. data/lib/synapse/event_sourcing/conflict_resolver.rb +3 -0
  31. data/lib/synapse/event_sourcing/member.rb +34 -6
  32. data/lib/synapse/event_sourcing/repository.rb +17 -0
  33. data/lib/synapse/event_sourcing/snapshot/aggregate_taker.rb +38 -0
  34. data/lib/synapse/event_sourcing/snapshot/policy.rb +27 -0
  35. data/lib/synapse/event_sourcing/snapshot/taker.rb +2 -37
  36. data/lib/synapse/event_sourcing/snapshot/unit_listener.rb +26 -0
  37. data/lib/synapse/event_sourcing/stream_decorator.rb +8 -6
  38. data/lib/synapse/event_store/errors.rb +2 -2
  39. data/lib/synapse/mapping.rb +2 -0
  40. data/lib/synapse/mapping/mapper.rb +75 -0
  41. data/lib/synapse/{wiring/wire.rb → mapping/mapping.rb} +8 -8
  42. data/lib/synapse/process_manager.rb +2 -2
  43. data/lib/synapse/process_manager/mapping/process.rb +44 -0
  44. data/lib/synapse/process_manager/{wiring → mapping}/process_manager.rb +13 -13
  45. data/lib/synapse/process_manager/process.rb +3 -3
  46. data/lib/synapse/repository/locking.rb +14 -8
  47. data/lib/synapse/upcasting/upcaster_chain.rb +2 -2
  48. data/lib/synapse/version.rb +1 -1
  49. data/test/command/{wiring_test.rb → mapping_test.rb} +11 -11
  50. data/test/configuration/component/command_bus/simple_command_bus_test.rb +30 -0
  51. data/test/configuration/component/event_bus/simple_event_bus_test.rb +2 -2
  52. data/test/configuration/component/event_sourcing/repository_test.rb +71 -0
  53. data/test/configuration/component/repository/simple_repository_test.rb +35 -0
  54. data/test/configuration/component/upcasting/upcaster_chain_test.rb +29 -0
  55. data/test/configuration/container_builder_test.rb +4 -6
  56. data/test/event_bus/{wiring_test.rb → mapping_test.rb} +6 -6
  57. data/test/event_sourcing/aggregate_factory_test.rb +5 -1
  58. data/test/event_sourcing/aggregate_root_test.rb +1 -0
  59. data/test/event_sourcing/fixtures.rb +21 -21
  60. data/test/event_sourcing/repository_test.rb +10 -0
  61. data/test/event_sourcing/snapshot/aggregate_taker_test.rb +1 -1
  62. data/test/event_sourcing/snapshot/interval_policy_test.rb +24 -0
  63. data/test/process_manager/{wiring → mapping}/fixtures.rb +7 -8
  64. data/test/process_manager/{wiring → mapping}/process_manager_test.rb +6 -6
  65. data/test/process_manager/{wiring → mapping}/process_test.rb +3 -3
  66. data/test/serialization/converter/chain_test.rb +2 -2
  67. data/test/serialization/converter/factory_test.rb +2 -2
  68. data/test/serialization/converter/identity_test.rb +1 -1
  69. data/test/serialization/converter/json_test.rb +2 -2
  70. data/test/serialization/converter/ox_test.rb +2 -2
  71. data/test/serialization/lazy_object_test.rb +1 -1
  72. data/test/serialization/message/metadata_test.rb +1 -1
  73. data/test/serialization/message/serialization_aware_message_test.rb +5 -5
  74. data/test/serialization/message/serialized_message_builder_test.rb +1 -1
  75. data/test/serialization/message/serialized_message_test.rb +5 -5
  76. data/test/serialization/message/serializer_test.rb +2 -2
  77. data/test/serialization/revision_resolver_test.rb +1 -1
  78. data/test/serialization/serialized_object_test.rb +2 -2
  79. data/test/serialization/serialized_type_test.rb +2 -2
  80. data/test/serialization/serializer/marshal_test.rb +1 -1
  81. data/test/serialization/serializer/oj_test.rb +1 -1
  82. data/test/serialization/serializer/ox_test.rb +2 -2
  83. data/test/serialization/serializer_test.rb +1 -1
  84. data/test/uow/factory_test.rb +1 -1
  85. data/test/uow/outer_commit_listener_test.rb +4 -4
  86. data/test/uow/provider_test.rb +5 -5
  87. data/test/uow/uow_test.rb +19 -17
  88. data/test/upcasting/chain_test.rb +1 -1
  89. data/test/upcasting/data_test.rb +3 -1
  90. metadata +30 -37
  91. data/lib/synapse/command/wiring.rb +0 -47
  92. data/lib/synapse/event_bus/wiring.rb +0 -20
  93. data/lib/synapse/event_sourcing/snapshot/count_stream.rb +0 -86
  94. data/lib/synapse/event_sourcing/snapshot/count_trigger.rb +0 -91
  95. data/lib/synapse/process_manager/wiring/process.rb +0 -27
  96. data/lib/synapse/wiring.rb +0 -3
  97. data/lib/synapse/wiring/message_wiring.rb +0 -76
  98. data/lib/synapse/wiring/wire_registry.rb +0 -61
  99. data/test/event_sourcing/snapshot/integration_test.rb +0 -65
  100. data/test/wiring/wire_registry_test.rb +0 -60
  101. data/test/wiring/wire_test.rb +0 -51
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: baa532559d58992e62cae2cc5b843a9485bd10af
4
+ data.tar.gz: 5d63dd14c2598d92034db8fb45d7e37958a45f98
5
+ SHA512:
6
+ metadata.gz: 5264e9b6b99b0da204d4c0dc9d28f4b1624c1ac28e6ae9e47b5f89d441a4b2a41fe2a4fcde4e25801d7f2aa676b615728881fb4656132f7ed2d5e9f42edcc7e7
7
+ data.tar.gz: fe5bc48121cacff34fb1ef50a43159902de2d5bf93b34ee5504e440e5a0dd76ea5caa66b5a441d55f154259a271feea30cbe523d8d180f67f7983d1889cdff18
data/lib/synapse.rb CHANGED
@@ -14,10 +14,10 @@ module Synapse
14
14
  autoload :Command
15
15
  autoload :Domain
16
16
  autoload :EventBus
17
+ autoload :Mapping
17
18
  autoload :Repository
18
19
  autoload :Serialization
19
20
  autoload :UnitOfWork, 'synapse/uow'
20
- autoload :Wiring
21
21
  end
22
22
 
23
23
  # Optional components
@@ -29,10 +29,10 @@ require 'synapse/command/command_handler'
29
29
  require 'synapse/command/dispatch_interceptor'
30
30
  require 'synapse/command/errors'
31
31
  require 'synapse/command/interceptor_chain'
32
+ require 'synapse/command/mapping'
32
33
  require 'synapse/command/message'
33
34
  require 'synapse/command/rollback_policy'
34
35
  require 'synapse/command/simple_command_bus'
35
- require 'synapse/command/wiring'
36
36
 
37
37
  require 'synapse/command/gateway'
38
38
  require 'synapse/command/gateway/retry_scheduler'
@@ -2,7 +2,7 @@ module Synapse
2
2
  module Command
3
3
  # Mixin for an object capable of handling commands
4
4
  #
5
- # Consider using the command handler mixin that uses message wiring.
5
+ # Consider using the command handler mixin that uses the mapping DSL.
6
6
  module CommandHandler
7
7
  # Handles the given command
8
8
  #
@@ -0,0 +1,71 @@
1
+ module Synapse
2
+ module Command
3
+ # Mixin for a command handler that uses the mapping DSL
4
+ #
5
+ # @example
6
+ # class OrderBookCommandHandler
7
+ # include MappingCommandHandler
8
+ #
9
+ # map_command CreateOrderbook do |command|
10
+ # # ...
11
+ # end
12
+ #
13
+ # map_command PlaceBuyOrder, :to => :on_buy_order
14
+ # map_command PlaceSellOrder, :to => :on_sell_order
15
+ # end
16
+ module MappingCommandHandler
17
+ extend ActiveSupport::Concern
18
+ include CommandHandler
19
+
20
+ included do
21
+ # @return [Mapping::Mapper]
22
+ class_attribute :command_mapper
23
+ self.command_mapper = Mapping::Mapper.new false
24
+ end
25
+
26
+ module ClassMethods
27
+ # @see Mapper#map
28
+ # @param [Class] type
29
+ # @param [Object...] args
30
+ # @param [Proc] block
31
+ # @return [undefined]
32
+ def map_command(type, *args, &block)
33
+ command_mapper.map type, *args, &block
34
+ end
35
+ end
36
+
37
+ # @param [CommandMessage] command
38
+ # @param [UnitOfWork] current_unit Current unit of work
39
+ # @return [Object] The result of handling the given command
40
+ def handle(command, current_unit)
41
+ mapping = command_mapper.mapping_for command.payload_type
42
+
43
+ unless mapping
44
+ raise ArgumentError, 'Not capable of handling [%s] commands' % command.payload_type
45
+ end
46
+
47
+ mapping.invoke self, command.payload
48
+ end
49
+
50
+ # Subscribes this handler to the given command bus for any types that have been mapped
51
+ #
52
+ # @param [CommandBus] command_bus
53
+ # @return [undefined]
54
+ def subscribe(command_bus)
55
+ command_mapper.each_type do |type|
56
+ command_bus.subscribe type, self
57
+ end
58
+ end
59
+
60
+ # Unsubscribes this handler from the given command bus for any types that have been mapped
61
+ #
62
+ # @param [CommandBus] command_bus
63
+ # @return [undefined]
64
+ def unsubscribe(command_bus)
65
+ command_mapper.each_type do |type|
66
+ command_bus.unsubscribe type, self
67
+ end
68
+ end
69
+ end # MappingCommandHandler
70
+ end # Command
71
+ end
@@ -6,22 +6,6 @@ module Synapse
6
6
  def self.builder
7
7
  CommandMessageBuilder
8
8
  end
9
-
10
- # Creates a command message using the given command object
11
- #
12
- # If the given object is an command message, it will be returned unchanged.
13
- #
14
- # @param [Object] command
15
- # @return [CommandMessage]
16
- def self.as_message(command)
17
- unless command.is_a? CommandMessage
18
- command = self.build do |builder|
19
- builder.payload = command
20
- end
21
- end
22
-
23
- command
24
- end
25
9
  end
26
10
 
27
11
  # Message builder capable of producing CommandMessage instances
@@ -15,12 +15,15 @@ module Synapse
15
15
  # @param [UnitOfWorkFactory] unit_factory
16
16
  # @return [undefined]
17
17
  def initialize(unit_factory)
18
+ @unit_factory = unit_factory
19
+
18
20
  @handlers = Hash.new
19
21
  @filters = Array.new
20
22
  @interceptors = Array.new
21
- @logger = Logging.logger[self.class]
23
+
22
24
  @rollback_policy = RollbackOnAnyExceptionPolicy.new
23
- @unit_factory = unit_factory
25
+
26
+ @logger = Logging.logger[self.class]
24
27
  end
25
28
 
26
29
  # @api public
@@ -66,6 +66,22 @@ module Synapse
66
66
  builder.build
67
67
  end
68
68
 
69
+ # Wraps an object into a message as its payload
70
+ #
71
+ # If the given object is an message, it will be returned unchanged.
72
+ #
73
+ # @param [Object] object
74
+ # @return [Message]
75
+ def self.as_message(object)
76
+ unless object.is_a? Message
77
+ object = self.build do |builder|
78
+ builder.payload = object
79
+ end
80
+ end
81
+
82
+ object
83
+ end
84
+
69
85
  # Yields a message builder that can be used to produce a message
70
86
  #
71
87
  # @see MessageBuilder#build
@@ -6,27 +6,10 @@ require 'synapse/configuration/dependent'
6
6
  require 'synapse/configuration/ext'
7
7
 
8
8
  require 'synapse/configuration/component/command_bus'
9
- # Has to be loaded before asynchronous command bus definition builder
10
- require 'synapse/configuration/component/command_bus/simple_command_bus'
11
- require 'synapse/configuration/component/command_bus/async_command_bus'
12
-
13
9
  require 'synapse/configuration/component/event_bus'
14
- require 'synapse/configuration/component/event_bus/simple_event_bus'
15
-
10
+ # Has to be loaded before event sourcing
16
11
  require 'synapse/configuration/component/repository'
17
- # Has to be loaded before event sourcing or simple repository definition builders
18
- require 'synapse/configuration/component/repository/locking_repository'
19
- require 'synapse/configuration/component/repository/simple_repository'
20
-
21
12
  require 'synapse/configuration/component/event_sourcing'
22
- require 'synapse/configuration/component/event_sourcing/repository'
23
-
24
13
  require 'synapse/configuration/component/serialization'
25
- require 'synapse/configuration/component/serialization/converter_factory'
26
- require 'synapse/configuration/component/serialization/serializer'
27
-
28
14
  require 'synapse/configuration/component/uow'
29
- require 'synapse/configuration/component/uow/unit_factory'
30
-
31
15
  require 'synapse/configuration/component/upcasting'
32
- require 'synapse/configuration/component/upcasting/upcaster_chain'
@@ -1,35 +1,18 @@
1
+ require 'synapse/configuration/component/command_bus/simple_command_bus'
2
+ require 'synapse/configuration/component/command_bus/async_command_bus'
3
+ require 'synapse/configuration/component/command_bus/gateway'
4
+
1
5
  module Synapse
2
6
  module Configuration
3
7
  class ContainerBuilder
4
8
  # Creates and configures an asynchronous command bus
5
- #
6
- # @see AsynchronousCommandBusDefinitionBuilder
7
- # @param [Symbol] identifier
8
- # @param [Proc] block
9
- # @return [undefined]
10
- def async_command_bus(identifier = nil, &block)
11
- with_definition_builder AsynchronousCommandBusDefinitionBuilder, identifier, &block
12
- end
9
+ builder :async_command_bus, AsynchronousCommandBusDefinitionBuilder
13
10
 
14
11
  # Creates and configures a simple command bus
15
- #
16
- # @see SimpleCommandBusDefinitionBuilder
17
- # @param [Symbol] identifier
18
- # @param [Proc] block
19
- # @return [undefined]
20
- def simple_command_bus(identifier = nil, &block)
21
- with_definition_builder SimpleCommandBusDefinitionBuilder, identifier, &block
22
- end
12
+ builder :simple_command_bus, SimpleCommandBusDefinitionBuilder
23
13
 
24
14
  # Creates and configures a command gateway
25
- #
26
- # @see CommandGatewayBusDefinitionBuilder
27
- # @param [Symbol] identifier
28
- # @param [Proc] block
29
- # @return [undefined]
30
- def gateway(identifier = nil, &block)
31
- with_definition_builder CommandGatewayBusDefinitionBuilder, identifier, &block
32
- end
15
+ builder :gateway, CommandGatewayDefinitionBuilder
33
16
  end # ContainerBuilder
34
17
  end # Configuration
35
18
  end
@@ -5,12 +5,12 @@ module Synapse
5
5
  # @example The minimum possible effort to build a command bus
6
6
  # simple_command_bus
7
7
  #
8
- # @example Create a command bus with an alternate identifier and handler tag
8
+ # @example Create a command bus with an alternate identifier and tags
9
9
  # simple_command_bus :alt_command_bus do
10
10
  # use_handler_tag :alt_command_handler
11
+ # use_filter_tag :alt_command_filter
12
+ # use_interceptor_tag :alt_dispatch_interceptor
11
13
  # end
12
- #
13
- # @todo Support for interceptors and filters
14
14
  class SimpleCommandBusDefinitionBuilder < DefinitionBuilder
15
15
  # Changes the tag to use to automatically subscribe command handlers
16
16
  #
@@ -23,6 +23,22 @@ module Synapse
23
23
  @handler_tag = handler_tag
24
24
  end
25
25
 
26
+ # Changes the tag to use to automatically register command filters
27
+ #
28
+ # @param [Symbol] filter_tag
29
+ # @return [undefined]
30
+ def use_filter_tag(filter_tag)
31
+ @filter_tag = filter_tag
32
+ end
33
+
34
+ # Changes the tag to use to automatically register command filters
35
+ #
36
+ # @param [Symbol] interceptor_tag
37
+ # @return [undefined]
38
+ def use_interceptor_tag(interceptor_tag)
39
+ @interceptor_tag = interceptor_tag
40
+ end
41
+
26
42
  # Changes the rollback policy to use for the command bus
27
43
  #
28
44
  # By default, the command bus will always rollback on an exception
@@ -48,21 +64,29 @@ module Synapse
48
64
  identified_by :command_bus
49
65
 
50
66
  use_handler_tag :command_handler
67
+ use_filter_tag :command_filter
68
+ use_interceptor_tag :dispatch_interceptor
69
+
51
70
  use_unit_factory :unit_factory
52
71
 
53
72
  use_factory do
54
73
  unit_factory = resolve @unit_factory
55
74
 
56
75
  command_bus = create_command_bus unit_factory
57
-
58
- if @rollback_policy
59
- command_bus.rollback_policy = resolve @rollback_policy
60
- end
76
+ command_bus.rollback_policy = resolve @rollback_policy, true
61
77
 
62
78
  with_tagged @handler_tag do |handler|
63
79
  handler.subscribe command_bus
64
80
  end
65
81
 
82
+ with_tagged @filter_tag do |filter|
83
+ command_bus.filters.push filter
84
+ end
85
+
86
+ with_tagged @interceptor_tag do |interceptor|
87
+ command_bus.interceptors.push interceptor
88
+ end
89
+
66
90
  command_bus
67
91
  end
68
92
  end
@@ -1,15 +1,10 @@
1
+ require 'synapse/configuration/component/event_bus/simple_event_bus'
2
+
1
3
  module Synapse
2
4
  module Configuration
3
5
  class ContainerBuilder
4
6
  # Creates and configures a simple event bus
5
- #
6
- # @see SimpleEventBusDefinitionBuilder
7
- # @param [Symbol] identifier
8
- # @param [Proc] block
9
- # @return [undefined]
10
- def simple_event_bus(identifier = nil, &block)
11
- with_definition_builder SimpleEventBusDefinitionBuilder, identifier, &block
12
- end
7
+ builder :simple_event_bus, SimpleEventBusDefinitionBuilder
13
8
  end # ContainerBuilder
14
9
  end # Configuration
15
10
  end
@@ -1,15 +1,18 @@
1
+ require 'synapse/configuration/component/event_sourcing/aggregate_snapshot_taker'
2
+ require 'synapse/configuration/component/event_sourcing/interval_snapshot_policy'
3
+ require 'synapse/configuration/component/event_sourcing/repository'
4
+
1
5
  module Synapse
2
6
  module Configuration
3
7
  class ContainerBuilder
4
8
  # Creates and configures an event sourcing repository
5
- #
6
- # @see EventSourcingRepositoryDefinitionBuilder
7
- # @param [Symbol] identifier
8
- # @param [Proc] block
9
- # @return [undefined]
10
- def es_repository(identifier = nil, &block)
11
- with_definition_builder EventSourcingRepositoryDefinitionBuilder, identifier, &block
12
- end
9
+ builder :es_repository, EventSourcingRepositoryDefinitionBuilder
10
+
11
+ # Creates and configures an aggregate snapshot taker
12
+ builder :aggregate_snapshot_taker, AggregateSnapshotTakerDefinitionBuilder
13
+
14
+ # Creates and configures an interval-based snapshot policy
15
+ builder :interval_snapshot_policy, IntervalSnapshotPolicyDefinitionBuilder
13
16
  end # ContainerBuilder
14
17
  end # Configuration
15
18
  end
@@ -0,0 +1,48 @@
1
+ module Synapse
2
+ module Configuration
3
+ # Definition builder used to create aggregate snapshot takers
4
+ #
5
+ # @example The minimum possible effort to build an aggregate snapshot taker
6
+ # aggregate_snapshot_taker
7
+ #
8
+ # @example Build an aggregate snapshot taker using an alternate event store and factory tag
9
+ # aggregate_snapshot_taker :alt_snapshot_taker do
10
+ # use_aggregate_factory_tag :alt_factory_tag
11
+ # use_event_store :alt_event_store
12
+ # end
13
+ class AggregateSnapshotTakerDefinitionBuilder < DefinitionBuilder
14
+ # @param [Symbol] aggregate_factory_tag
15
+ # @return [undefined]
16
+ def use_aggregate_factory_tag(aggregate_factory_tag)
17
+ @aggregate_factory_tag = aggregate_factory_tag
18
+ end
19
+
20
+ # @param [Symbol] event_store
21
+ # @return [undefined]
22
+ def use_event_store(event_store)
23
+ @event_store = event_store
24
+ end
25
+
26
+ protected
27
+
28
+ # @return [undefined]
29
+ def populate_defaults
30
+ identified_by :snapshot_taker
31
+
32
+ use_aggregate_factory_tag :aggregate_factory
33
+ use_event_store :event_store
34
+
35
+ use_factory do
36
+ event_store = resolve @event_store
37
+ snapshot_taker = EventSourcing::AggregateSnapshotTaker.new event_store
38
+
39
+ with_tagged @aggregate_factory_tag do |factory|
40
+ snapshot_taker.register_factory factory
41
+ end
42
+
43
+ snapshot_taker
44
+ end
45
+ end
46
+ end # AggregateSnapshotTakerDefinitionBuilder
47
+ end # Configuration
48
+ end
@@ -0,0 +1,33 @@
1
+ module Synapse
2
+ module Configuration
3
+ # Definition builder used to create an interval-based snapshot policy
4
+ #
5
+ # @example The minimum possible effort to build a snapshot policy
6
+ # interval_snapshot_policy
7
+ #
8
+ # @example Build an aggregate snapshot taker using an alternate threshold
9
+ # interval_snapshot_policy :alt_snapshot_policy do
10
+ # use_threshold 50
11
+ # end
12
+ class IntervalSnapshotPolicyDefinitionBuilder < DefinitionBuilder
13
+ # @param [Integer] threshold
14
+ # @return [undefined]
15
+ def use_threshold(threshold)
16
+ @threshold = threshold
17
+ end
18
+
19
+ protected
20
+
21
+ # @return [undefined]
22
+ def populate_defaults
23
+ identified_by :snapshot_policy
24
+
25
+ use_threshold 30
26
+
27
+ use_factory do
28
+ EventSourcing::AggregateSnapshotTaker.new @threshold
29
+ end
30
+ end
31
+ end # IntervalSnapshotPolicyDefinitionBuilder
32
+ end # Configuration
33
+ end