mediate 0.1.0 → 0.1.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 06e45660926271c9435a4c2dd1634974b3873091f12160b907833cab8c9b7abf
4
- data.tar.gz: 8b1fe7523830f57f8e2499bf8c3575ffc0eef7716478a554f683947a8958dd95
3
+ metadata.gz: 3d836d3d2c7c76a49fe26644d4fb93ce98f84ae6b77cb1a47ab0db43c3ccf3d2
4
+ data.tar.gz: 87b88af20dd82d6cadb44261d47864fb032f5c27648f1e50092c6ad2572f6495
5
5
  SHA512:
6
- metadata.gz: 6f29b77858c6361cbdc54ccc79656d470dd2feb274fe341746fa66d9fc33e34a7c1906bc8df859dfbb0dbf68fa73fc5f1b7819a2bb889565c91388abfbd8d090
7
- data.tar.gz: 20c32a19443169eb74e41524b8ae9a342a023a2f54a306d182c3419988dbc5479a739c5866012d29a83e1cb1aeace714f6677efaab7c6368d90082fa0655c879
6
+ metadata.gz: a8c012bc18b9939dcea6264760d45516359569fa865e725e488cb00ead4a22ab977ce1c6f6561ed256031b0c06824b998cbf090fe09fc2654a4273f5e6310584
7
+ data.tar.gz: 769e0b47ce6458170e422aefb56ff89bfde68adef9fdfe1b5ca65ae47ce2f138aa2b0407664c1910942fc81b5eac6af112d34d0a83513810c43e3b8b0f372eb3
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mediate (0.1.0)
4
+ mediate (0.1.2)
5
5
  concurrent-ruby (~> 1.1)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -96,7 +96,7 @@ Note that only one handler can be registered for a particular request class; att
96
96
 
97
97
  #### Implicit handler declaration
98
98
 
99
- For simple handlers, you can skip the explicit `RequestHandler` declaration above and instead pass a block to `Request.handle_with`.
99
+ For simple handlers, you can skip the explicit `RequestHandler` declaration above and instead pass a lambda to `Request.handle_with`.
100
100
 
101
101
  ```ruby
102
102
  class Ping < Mediate::Request
@@ -107,14 +107,14 @@ class Ping < Mediate::Request
107
107
  super()
108
108
  end
109
109
  # This will have the same behavior as the PingHandler declaration above.
110
- handle_with { |request| "Received: #{request.message}" }
110
+ handle_with ->(request) { "Received: #{request.message}" }
111
111
  end
112
112
 
113
113
  response = Mediate.dispatch(Ping.new('hello'))
114
114
  puts response # 'Received: hello'
115
115
  ```
116
116
 
117
- Behind the scenes, this defines a `Ping::Handler` class that calls the given block in its `handle` method. For testing purposes, you can get an instance of this handler class by calling `Mediate::Request.create_implicit_handler` (see [Testing implicit request handlers](#testing-implicit-request-handlers)).
117
+ Behind the scenes, this defines a `Ping::Handler` class that calls the given lambda in its `handle` method. For testing purposes, you can get an instance of this handler class by calling `Mediate::Request.create_implicit_handler` (see [Testing implicit request handlers](#testing-implicit-request-handlers)).
118
118
 
119
119
  #### Request polymorphism
120
120
 
@@ -129,7 +129,7 @@ Unless we registered a handler for `SubPing` explicitly.
129
129
 
130
130
  ```ruby
131
131
  class SubPing < Ping
132
- handle_with { |request| "Received from SubPing: #{request.message}" }
132
+ handle_with ->(request) { "Received from SubPing: #{request.message}" }
133
133
  end
134
134
  puts Mediate.dispatch(SubPing.new('howdy')) # 'Received from SubPing: howdy'
135
135
  ```
@@ -252,11 +252,11 @@ Special consideration is only required when testing paths that invoke methods on
252
252
 
253
253
  #### Testing implicit request handlers
254
254
 
255
- How can you test a request handler defined using `handle_with` and a block like the following?
255
+ How can you test a request handler defined using `handle_with` and a lambda like the following?
256
256
 
257
257
  ```ruby
258
258
  class ExampleRequest < Mediate::Request
259
- handle_with do |request|
259
+ handle_with lambda do |request|
260
260
  # ....
261
261
  end
262
262
  end
@@ -51,7 +51,7 @@ module Mediate
51
51
  end
52
52
 
53
53
  #
54
- # Sends a notification to all register handlers for the given notification's type.
54
+ # Sends a notification to all registered handlers for the given notification's type.
55
55
  #
56
56
  # @param [Mediate::Notification] notification
57
57
  #
@@ -9,27 +9,25 @@ module Mediate
9
9
  IMPLICIT_HANDLER_CLASS_NAME = "Handler"
10
10
 
11
11
  #
12
- # Registers a handler for this Request type using the given block as the handle method.
12
+ # Registers a handler for this Request type using the given lambda as the handle method.
13
13
  #
14
+ # @param [Lambda] lmbda the block that will handle the request
14
15
  # @param [Mediate::Mediator] mediator the instance to register the handler on
15
- # @param [Proc] &proc the block that will handle the request
16
16
  #
17
- # @raises [ArgumentError] if no block is given
17
+ # @raises [ArgumentError] if no lambda is given
18
+ # @raises [RequestHandlerAlreadyExistsError] if handler already defined for this class
18
19
  #
19
- # @example When a request of this type is dispatched, the handle_with block will run
20
+ # @example When a request of this type is dispatched, the handle_with lambda will run
20
21
  # class MyRequest < Mediate::Request
21
- # handle_with do |request|
22
+ # handle_with lambda do |request|
22
23
  # ## do something with request...
23
24
  # end
24
25
  # end
25
- def self.handle_with(mediator = Mediate.mediator, &proc)
26
- raise ArgumentError, "expected block to be passed to #handle_with." unless proc
26
+ def self.handle_with(lmbda, mediator = Mediate.mediator)
27
+ raise ArgumentError, "expected lambda to be passed to #handle_with." if lmbda.nil?
27
28
 
28
- if implicit_handler_defined?
29
- raise "#{name}::#{IMPLICIT_HANDLER_CLASS_NAME} is already defined. Cannot create implicit handler."
30
- end
31
-
32
- handler_class = define_handler(proc)
29
+ handler_class = define_handler(lmbda)
30
+ undefine_implicit_handler # remove any previous definition (this will do nothing if it doesn't exist)
33
31
  const_set(IMPLICIT_HANDLER_CLASS_NAME, handler_class)
34
32
  mediator.register_request_handler(handler_class, self)
35
33
  end
@@ -57,17 +55,14 @@ module Mediate
57
55
  remove_const(IMPLICIT_HANDLER_CLASS_NAME)
58
56
  end
59
57
 
60
- def self.define_handler(proc)
58
+ def self.define_handler(lmbda)
61
59
  Class.new(RequestHandler) do
62
- @@handle_proc = proc # rubocop:disable Style/ClassVars
63
- def handle(request)
64
- @@handle_proc.call(request)
65
- end
60
+ define_method(:handle, lmbda)
66
61
  end
67
62
  end
68
63
 
69
64
  def self.implicit_handler_defined?
70
- const_defined?(IMPLICIT_HANDLER_CLASS_NAME)
65
+ const_defined?(IMPLICIT_HANDLER_CLASS_NAME, false) # do not check ancestors
71
66
  end
72
67
 
73
68
  private_constant :IMPLICIT_HANDLER_CLASS_NAME
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mediate
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/mediate.rb CHANGED
@@ -33,7 +33,7 @@ module Mediate
33
33
  end
34
34
 
35
35
  #
36
- # Sends a notification to all register handlers for the given notification's type.
36
+ # Sends a notification to all registered handlers for the given notification's type.
37
37
  #
38
38
  # @param [Mediate::Notification] notification
39
39
  #
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mediate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Ferguson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-02 00:00:00.000000000 Z
11
+ date: 2022-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby