replay 0.1.0 → 0.1.1

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: 1c2e196bd8e54aa363a8ad762f41797aa99c989e
4
- data.tar.gz: cda39f5a62f384de3ba509f291407a54035b007b
3
+ metadata.gz: 084d0ba8e80a2b93566f0ba8e780feedccf92d28
4
+ data.tar.gz: 7cf7c4e15f76ee0659cb0be60cac9a21d0fc2d6b
5
5
  SHA512:
6
- metadata.gz: af511f51f8dbec4f12fc284bebf4232786081e6832089d1bd7209f7ab88b001e02a40bb85a7182acb562c19715c037c797c0e5be4d54b4bfd51228e9bb853c94
7
- data.tar.gz: 25ff65cea036543f63088c7ff299acbd1128cbbfd70c74aa6fbfefa4fa4c9312a0b034ac687cecff2e4ef041c063aac18ec5956d897d9f2d63da3c71c8351a57
6
+ metadata.gz: e1561eba9d3b6469a38243a840e22dd850500a057f3b272f8ea67964680436867f40c57a892e07cf31aa44cdd519660d3f273f4bd657e0e304635120bec65d64
7
+ data.tar.gz: ebd32484a7c18fa8c7dbbd77c49b3564bfaede2ee2c785c839e0a1af00d1dac5ee876ea57cc1f6659c6438f28013e1353aecd2b9abf80afe422389a7a9ab43eb
@@ -13,6 +13,7 @@ module Replay
13
13
  class UndefinedKeyError < ReplayError; end
14
14
  class UnhandledEventError < ReplayError; end
15
15
  class UnknownEventError < ReplayError; end
16
+ class InvalidRouterError < ReplayError; end
16
17
  class InvalidStorageError < ReplayError;
17
18
  def initialize(*args)
18
19
  klass = args.shift
@@ -33,6 +34,7 @@ require 'replay/event_decorator'
33
34
  require 'replay/event_declarations'
34
35
  require 'replay/publisher'
35
36
  require 'replay/subscription_manager'
37
+ require 'replay/subscriptions'
36
38
  require 'replay/backends'
37
39
  require 'replay/repository'
38
40
  require 'replay/repository/identity_map'
@@ -2,16 +2,27 @@ module Replay
2
2
  module Observer
3
3
 
4
4
  def self.included(base)
5
- class << base
6
- def observe(event_type, &block)
7
- @observer_blocks ||= Hash.new
8
- @observer_blocks[Replay::Inflector.underscore(event_type.to_s)] = block
9
- end
5
+ base.extend(ClassMethods)
6
+ base.instance_variable_set(:@router, Replay::Router::DefaultRouter)
7
+ end
8
+
9
+ module ClassMethods
10
+ def router(rtr)
11
+ raise Replay::InvalidRouterError.new("Router does not implement add_observer") unless rtr.respond_to?(:add_observer)
12
+ @router = rtr
13
+ end
14
+
15
+ def observe(event_type, &block)
16
+ raise InvalidRouterError.new("No router defined!") unless @router
17
+ @observer_blocks ||= Hash.new
18
+ @observer_blocks[Replay::Inflector.underscore(event_type.to_s)] = block
19
+
20
+ @router.add_observer self, event_type
21
+ end
10
22
 
11
- def published(stream_id, event)
12
- blk = @observer_blocks[Replay::Inflector.underscore(event.class.to_s)]
13
- blk.call(stream_id, event, binding) if blk
14
- end
23
+ def published(stream_id, event)
24
+ blk = @observer_blocks[Replay::Inflector.underscore(event.class.to_s)]
25
+ blk.call(stream_id, event, binding) if blk
15
26
  end
16
27
  end
17
28
  end
@@ -1,21 +1,12 @@
1
1
  module Replay
2
2
  module Publisher
3
3
  def self.included(base)
4
- include_essentials base
5
- end
6
-
7
- def self.include_essentials(base)
8
4
  base.instance_variable_set :@application_blocks, {}
9
5
  base.extend ClassMethods
10
6
  base.extend(Replay::Events)
11
- end
12
-
13
- def subscription_manager
14
- @subscription_manager ||= Replay::SubscriptionManager.new(Replay.logger)
15
- end
16
-
17
- def add_subscriber(subscriber)
18
- subscription_manager.add_subscriber(subscriber)
7
+ base.class_exec do
8
+ include Replay::Subscriptions
9
+ end
19
10
  end
20
11
 
21
12
  def apply(events, raise_unhandled = true)
@@ -1,5 +1,24 @@
1
1
  module Replay
2
2
  module Router
3
+ def self.included(base)
4
+ base.class_exec do
5
+ include Replay::Subscriptions
6
+ extend ClassMethods if base.include?(Singleton)
7
+ end
8
+ end
3
9
 
10
+ def add_observer(observer, *events)
11
+ add_subscriber observer
12
+ end
13
+
14
+ module ClassMethods
15
+ def add_observer(observer, *events)
16
+ instance.add_subscriber(observer)
17
+ end
18
+
19
+ def published(stream, event)
20
+ instance.published(stream, event)
21
+ end
22
+ end
4
23
  end
5
24
  end
@@ -2,18 +2,7 @@ module Replay
2
2
  module Router
3
3
  class DefaultRouter
4
4
  include Singleton
5
-
6
- def initialize
7
- @subscription_manager = Replay::SubscriptionManager.new
8
- end
9
-
10
- def add_observer(observer, *events)
11
- @subscription_manager.add_subscriber(observer)
12
- end
13
-
14
- def published(stream_id, event)
15
- @subscription_manager.notify_subscribers(stream_id, event)
16
- end
5
+ include Replay::Router
17
6
  end
18
7
  end
19
8
  end
@@ -0,0 +1,15 @@
1
+ module Replay
2
+ module Subscriptions
3
+ def subscription_manager
4
+ @subscription_manager ||= Replay::SubscriptionManager.new(Replay.logger)
5
+ end
6
+
7
+ def add_subscriber(subscriber)
8
+ subscription_manager.add_subscriber(subscriber)
9
+ end
10
+
11
+ def published(stream_id, event)
12
+ @subscription_manager.notify_subscribers(stream_id, event)
13
+ end
14
+ end
15
+ end
@@ -37,6 +37,16 @@ Replay::SubscriptionManager.class_exec do
37
37
  end
38
38
  end
39
39
 
40
+ Replay::Router.module_exec do
41
+ def inspect
42
+ self.class.to_s
43
+ end
44
+
45
+ def observed_by?(object)
46
+ @subscription_manager.has_subscriber?(object)
47
+ end
48
+ end
49
+
40
50
  Replay::Publisher::ClassMethods.module_exec do
41
51
  def self.extended(base)
42
52
  @publishers ||= []
@@ -1,3 +1,3 @@
1
1
  module Replay
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -21,6 +21,19 @@ class ObserverTest
21
21
  end
22
22
  end
23
23
 
24
+ class NonstandardRouter
25
+ include Singleton
26
+ include Replay::Router
27
+ end
28
+
29
+ class RoutedObserverTest
30
+ include Replay::Observer
31
+ router NonstandardRouter.instance
32
+
33
+ observe ObservedEvent do |e|
34
+ end
35
+ end
36
+
24
37
  describe Replay::Observer do
25
38
  before do
26
39
  ObserverTest.reset
@@ -34,4 +47,12 @@ describe Replay::Observer do
34
47
  ObserverTest.published('123', UnobservedEvent.new)
35
48
  ObserverTest.wont_be :observed?
36
49
  end
50
+
51
+ it "links to DefaultRouter by default" do
52
+ Replay::Router::DefaultRouter.instance.must_be :observed_by?,ObserverTest
53
+ end
54
+
55
+ it "links to a substitute router when instructed" do
56
+ NonstandardRouter.instance.must_be :observed_by?, RoutedObserverTest
57
+ end
37
58
  end
@@ -1,12 +1,4 @@
1
1
  require 'test_helper'
2
-
3
- module Router
4
- module Test
5
- def has_observer?(object)
6
- @subscription_manager.has_subscriber?(object)
7
- end
8
- end
9
- end
10
2
  class TypedEvent
11
3
 
12
4
  end
@@ -20,8 +12,6 @@ class Observer
20
12
  end
21
13
  end
22
14
 
23
- Replay::Router::DefaultRouter.send(:include, Router::Test)
24
-
25
15
  describe Replay::Router::DefaultRouter do
26
16
  before do
27
17
  @router = Replay::Router::DefaultRouter.instance
@@ -29,7 +19,7 @@ describe Replay::Router::DefaultRouter do
29
19
  describe "adding observers" do
30
20
  it "tracks the observing object" do
31
21
  @router.add_observer(Observer)
32
- assert(@router.has_observer?(Observer), "router does not track observer")
22
+ @router.must_be :observed_by?, Observer
33
23
  end
34
24
  end
35
25
  describe "event publishing" do
@@ -1,6 +1,4 @@
1
1
  gem 'minitest'
2
- gem 'replay'
3
- gem 'byebug'
4
2
 
5
3
  require 'minitest/autorun'
6
4
  require 'minitest/spec'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: replay
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - karmajunkie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-14 00:00:00.000000000 Z
11
+ date: 2014-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -96,6 +96,7 @@ files:
96
96
  - lib/replay/router/default_router.rb
97
97
  - lib/replay/rspec.rb
98
98
  - lib/replay/subscription_manager.rb
99
+ - lib/replay/subscriptions.rb
99
100
  - lib/replay/test.rb
100
101
  - lib/replay/test/test_event_stream.rb
101
102
  - lib/replay/version.rb