u-observers 0.3.0 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0fe383e800461aeb658a9b17f2d4799b194b7e70fdf5a545dbe07f69cd9d2642
4
- data.tar.gz: a42fdd9868c43855006b763faa86b0377ae2204f6e9c20893edfbadf49792a60
3
+ metadata.gz: d9c81561b00ca412faa80abc349e8a87251f4d84ce81da5fcd95ca1117e2bd2e
4
+ data.tar.gz: da923ace22c60cac08f710dccac65367b0bb568d9b4a99fbbe8bc164c8824b12
5
5
  SHA512:
6
- metadata.gz: af8ce345aec2b28cef954f6de09ce835d1e4236e115877b04c92227121a85ebedd1b1cf422e3cc1645f2b7ad818cfdf81d9b05bcdb41007bca4fd9115d900fc0
7
- data.tar.gz: 45ade4eefd27ae3b8f0aed723681b5c1931cd11c34f78f17a21e59aa745a67de27b32272f4c01377950420f4dd0743c84258b9415cf24dea891d253f9ca1fe4b
6
+ metadata.gz: 69eedf04951d55a0889412d50e62e226fdfe2561a3a3a52df087f0091ec15ec696fe002fe906a00fdf15119455c4c7f7f1a54a464c83e56c3ec5d6ff143ed1f4
7
+ data.tar.gz: 805003dac964746271f6b7789527e608fbd2caf77dd8f9eb735b8d30f42159e9d577f0a8bb0b3b60199fc764b40c0ae7a1edd005deb2d01f7e9567d49e18c4fd
@@ -2,23 +2,27 @@ require 'micro/observers/version'
2
2
 
3
3
  module Micro
4
4
  module Observers
5
+ require 'micro/observers/utils'
6
+ require 'micro/observers/events_or_actions'
5
7
  require 'micro/observers/manager'
6
8
 
7
9
  module ClassMethods
8
- def call_observers(with: :call)
9
- proc do |object|
10
- Array(with)
11
- .each { |action| object.observers.call(action) }
12
- end
10
+ def notify_observers!(with:)
11
+ proc { |object| with.each { |evt_or_act| object.observers.call(evt_or_act) } }
13
12
  end
14
13
 
15
- def notify_observers(with: :call)
16
- call_observers(with: with)
14
+ def notify_observers(*events)
15
+ notify_observers!(with: EventsOrActions[events])
16
+ end
17
+
18
+ def call_observers(action: :call)
19
+ notify_observers!(with: EventsOrActions[action])
17
20
  end
18
21
  end
19
22
 
20
23
  def self.included(base)
21
24
  base.extend(ClassMethods)
25
+ base.send(:private_class_method, :notify_observers!)
22
26
  end
23
27
 
24
28
  def observers
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Micro
4
+ module Observers
5
+
6
+ module EventsOrActions
7
+ DEFAULT = [:call]
8
+
9
+ def self.[](values)
10
+ vals = Utils.compact_array(values)
11
+
12
+ vals.empty? ? DEFAULT : vals
13
+ end
14
+
15
+ private_constant :DEFAULT
16
+ end
17
+
18
+ end
19
+ end
@@ -6,7 +6,7 @@ module Micro
6
6
  class Manager
7
7
  EMPTY_HASH = {}.freeze
8
8
 
9
- SameObserver = -> (observer) do
9
+ EqualTo = -> (observer) do
10
10
  -> item { item[0] == :observer && item[1] == observer }
11
11
  end
12
12
 
@@ -17,11 +17,11 @@ module Micro
17
17
  def initialize(subject, list = nil)
18
18
  @subject = subject
19
19
 
20
- @list = (list.kind_of?(Array) ? list : []).flatten.tap(&:compact!)
20
+ @list = Utils.compact_array(list.kind_of?(Array) ? list : [])
21
21
  end
22
22
 
23
23
  def included?(observer)
24
- @list.any?(&SameObserver[observer])
24
+ @list.any?(&EqualTo[observer])
25
25
  end
26
26
 
27
27
  def attach(observer, options = EMPTY_HASH)
@@ -33,40 +33,54 @@ module Micro
33
33
  end
34
34
 
35
35
  def on(options=EMPTY_HASH)
36
- action, callable, with = options[:action], options[:call], options[:with]
36
+ event, callable, with = options[:event], options[:call], options[:with]
37
37
 
38
- return self unless action.is_a?(Symbol) && callable.respond_to?(:call)
38
+ return self unless event.is_a?(Symbol) && callable.respond_to?(:call)
39
39
 
40
- arg = with.is_a?(Proc) ? with.call(@subject) : subject
40
+ arg = with.is_a?(Proc) ? with.call(@subject) : (arg || subject)
41
41
 
42
- @list << [:caller, action, [callable, arg]]
42
+ @list << [:callable, event, [callable, arg]]
43
43
  end
44
44
 
45
45
  def detach(observer)
46
- @list.delete_if(&SameObserver[observer])
46
+ @list.delete_if(&EqualTo[observer])
47
47
 
48
48
  self
49
49
  end
50
50
 
51
- def call(action = :call)
52
- @list.each do |type, observer, data|
53
- if type == :caller && observer == action
54
- data[0].call(data[1])
55
- elsif type == :observer && observer.respond_to?(action)
56
- handler = observer.method(action)
51
+ def notify(event)
52
+ notify!(event)
57
53
 
58
- return handler.call(@subject) if handler.arity == 1
54
+ self
55
+ end
59
56
 
60
- handler.call(@subject, data)
61
- end
62
- end
57
+ def call(*actions)
58
+ EventsOrActions[actions].each { |act| notify!(act) }
63
59
 
64
60
  self
65
61
  end
66
62
 
67
- alias notify call
63
+ private
64
+
65
+ def notify!(evt_or_act)
66
+ @list.each do |strategy, observer, data|
67
+ call!(observer, strategy, data, with: evt_or_act)
68
+ end
69
+ end
70
+
71
+ def call!(observer, strategy, data, with:)
72
+ return data[0].call(data[1]) if strategy == :callable && observer == with
73
+
74
+ if strategy == :observer && observer.respond_to?(with)
75
+ handler = observer.method(with)
76
+
77
+ return handler.call(@subject) if handler.arity == 1
78
+
79
+ handler.call(@subject, data)
80
+ end
81
+ end
68
82
 
69
- private_constant :EMPTY_HASH
83
+ private_constant :EMPTY_HASH, :EqualTo
70
84
  end
71
85
 
72
86
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Micro
4
+ module Observers
5
+
6
+ module Utils
7
+ def self.compact_array(value)
8
+ Array(value).flatten.tap(&:compact!)
9
+ end
10
+ end
11
+
12
+ end
13
+ end
@@ -1,5 +1,5 @@
1
1
  module Micro
2
2
  module Observers
3
- VERSION = '0.3.0'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: u-observers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-09-25 00:00:00.000000000 Z
11
+ date: 2020-09-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Simple and powerful implementation of the observer pattern.
14
14
  email:
@@ -27,7 +27,9 @@ files:
27
27
  - bin/console
28
28
  - bin/setup
29
29
  - lib/micro/observers.rb
30
+ - lib/micro/observers/events_or_actions.rb
30
31
  - lib/micro/observers/manager.rb
32
+ - lib/micro/observers/utils.rb
31
33
  - lib/micro/observers/version.rb
32
34
  - lib/u-observers.rb
33
35
  - u-observers.gemspec