observers 0.7.0 → 0.9.0
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 +4 -4
- data/lib/models/key.rb +30 -15
- data/lib/models/observer.rb +11 -3
- data/lib/observers.rb +2 -2
- data/lib/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fbb9116b5b3a86d777d4aca13b5c4c04214340596e1df83adebada4dd871bc43
|
|
4
|
+
data.tar.gz: 560fbbb9480ac2968036b8024e9362c12ea68994c77a587abc2432505ceeda7f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 403fe656fda85237d897b4e82b84e9b770f352807ac5ef05a37f4c1d58784af7f43e5f22d6ab5999bdebeea831350757125f184870de45ad78330efc6fe07ace
|
|
7
|
+
data.tar.gz: '06680f408eaf15377c441fd995a7a3b05b3a41bcf6b8e3e3aa87dd06a6e65227f43a50b01c03193845d26d306e368b2ecfaec814af0cb8f4ba9a34386641af83'
|
data/lib/models/key.rb
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Observers
|
|
4
|
+
class NoObserversError < StandardError; end
|
|
5
|
+
|
|
4
6
|
class Key
|
|
5
7
|
attr_reader :observers
|
|
6
8
|
|
|
@@ -10,21 +12,19 @@ module Observers
|
|
|
10
12
|
end
|
|
11
13
|
|
|
12
14
|
def observe(object:, action:)
|
|
13
|
-
# TODO: We can observe objects directly, no need to wrap in an observer
|
|
15
|
+
# TODO: We can observe objects directly, no need to wrap in an observer, unless need to let the object's observer override the action?
|
|
14
16
|
# A future reason I can think of for keeping observer wrapper is to track whether the object has implemented certain actions/methods.
|
|
15
17
|
@observers << Observer.new(object:, action:)
|
|
16
18
|
end
|
|
17
19
|
|
|
18
20
|
# @returns: The result of the last observer with a non-nil value.
|
|
19
21
|
def trigger(action: nil, event: nil)
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
action = event.action if event && action.nil?
|
|
23
|
-
action = :handle if action.nil?
|
|
22
|
+
# TODO: Breaks some tests... should it? Yes. Should it be disabled when testing? Yes.
|
|
23
|
+
# raise NoObserversError, "No observers configured for #{@key}" if @observers.empty?
|
|
24
24
|
|
|
25
25
|
last_result = nil
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
per_observer_per_action(action:, event:) do |observer, action|
|
|
28
28
|
result = observer.trigger(action:, event:)
|
|
29
29
|
last_result = result unless result.nil?
|
|
30
30
|
yield if block_given?
|
|
@@ -33,26 +33,41 @@ module Observers
|
|
|
33
33
|
last_result
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
-
# @returns: The result of the first observer with a non-nil value.
|
|
36
|
+
# @returns: The result of the first observer and the first action with a non-nil value.
|
|
37
37
|
def take(action: nil, event: nil)
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
action = event.action if event && action.nil?
|
|
41
|
-
action = :handle if action.nil?
|
|
38
|
+
# TODO: Breaks some tests... should it? Yes. Should it be disabled when testing? Yes.
|
|
39
|
+
# raise NoObserversError, "No observers configured for #{@key}" if @observers.empty?
|
|
42
40
|
|
|
43
|
-
|
|
41
|
+
per_observer_per_action(action:, event:) do |observer, action|
|
|
44
42
|
result = observer.trigger(action:, event:)
|
|
45
43
|
yield if block_given?
|
|
46
44
|
return result unless result.nil?
|
|
47
45
|
end
|
|
48
46
|
|
|
49
|
-
|
|
47
|
+
# None of the observers returned a non-nil value.
|
|
48
|
+
nil
|
|
50
49
|
end
|
|
51
50
|
|
|
52
51
|
private
|
|
53
52
|
|
|
54
|
-
def
|
|
55
|
-
|
|
53
|
+
def per_observer_per_action(action:, event:)
|
|
54
|
+
action ||= event&.action
|
|
55
|
+
|
|
56
|
+
if action
|
|
57
|
+
@observers.each do |observer|
|
|
58
|
+
yield observer, action
|
|
59
|
+
end
|
|
60
|
+
elsif event&.actions
|
|
61
|
+
@observers.each do |observer|
|
|
62
|
+
event.actions.each do |action|
|
|
63
|
+
yield observer, action
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
else
|
|
67
|
+
@observers.each do |observer|
|
|
68
|
+
yield observer, :handle
|
|
69
|
+
end
|
|
70
|
+
end
|
|
56
71
|
end
|
|
57
72
|
end
|
|
58
73
|
end
|
data/lib/models/observer.rb
CHANGED
|
@@ -11,15 +11,23 @@ module Observers
|
|
|
11
11
|
|
|
12
12
|
def trigger(action:, event:)
|
|
13
13
|
action = @action if @action
|
|
14
|
-
|
|
14
|
+
|
|
15
|
+
return unless @object.respond_to?(action)
|
|
16
|
+
|
|
17
|
+
# Assumption that if method has params then that param is "event:".
|
|
18
|
+
if event && @object.method(action).parameters.count > 0
|
|
19
|
+
@object.send(action, **{ event: })
|
|
20
|
+
else
|
|
21
|
+
@object.send(action)
|
|
22
|
+
end
|
|
15
23
|
rescue ArgumentError => e
|
|
16
24
|
type = @object.instance_of?(Class) ? @object : @object.class
|
|
17
25
|
method_type = @object.instance_of?(Class) ? '.' : '#'
|
|
18
26
|
|
|
19
27
|
raise ArgumentError, "#{type}##{action} has an 'event:' keyword argument but no event arg was sent" if event.nil?
|
|
20
28
|
|
|
21
|
-
# Events trigger events, so the error bubbles up to
|
|
22
|
-
# "RequestEvent sent to Rain::Router#
|
|
29
|
+
# Events trigger events, so the error bubbles up to become the error message for the next rescue's error message:
|
|
30
|
+
# "RequestEvent sent to Rain::Router#route_request -> StatusEvent sent to Error404Node.render -> unknown keyword: :props"
|
|
23
31
|
raise ArgumentError, "#{event.class} sent to #{type}#{method_type}#{action} -> #{e.message}"
|
|
24
32
|
end
|
|
25
33
|
end
|
data/lib/observers.rb
CHANGED
|
@@ -4,7 +4,7 @@ require_relative 'keys'
|
|
|
4
4
|
require_relative 'models/observer'
|
|
5
5
|
|
|
6
6
|
module Observers
|
|
7
|
-
Config = Struct.new(:
|
|
7
|
+
Config = Struct.new(:empty_observers_callback)
|
|
8
8
|
|
|
9
9
|
class << self
|
|
10
10
|
def [](key)
|
|
@@ -16,7 +16,7 @@ module Observers
|
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
def config
|
|
19
|
-
@config ||= Config.new(
|
|
19
|
+
@config ||= Config.new(empty_observers_callback: nil)
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
def configure
|
data/lib/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: observers
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.9.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- maedi
|
|
@@ -21,11 +21,11 @@ files:
|
|
|
21
21
|
- lib/models/observer.rb
|
|
22
22
|
- lib/observers.rb
|
|
23
23
|
- lib/version.rb
|
|
24
|
-
homepage: https://github.com/
|
|
24
|
+
homepage: https://github.com/raindeer-rb/observers
|
|
25
25
|
licenses: []
|
|
26
26
|
metadata:
|
|
27
|
-
homepage_uri: https://github.com/
|
|
28
|
-
source_code_uri: https://github.com/
|
|
27
|
+
homepage_uri: https://github.com/raindeer-rb/observers
|
|
28
|
+
source_code_uri: https://github.com/raindeer-rb/observers/src/branch/main
|
|
29
29
|
rdoc_options: []
|
|
30
30
|
require_paths:
|
|
31
31
|
- lib
|