u-observers 0.2.0 → 0.3.0

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
  SHA256:
3
- metadata.gz: a74f927670b4695760167dbbc2c1758cedbe112233b22d938cbcd79083cd8526
4
- data.tar.gz: b268b6f4b5d9c92469c20c8ba891bc12146fa52b09e6a44ccb664c6155d9b45f
3
+ metadata.gz: 0fe383e800461aeb658a9b17f2d4799b194b7e70fdf5a545dbe07f69cd9d2642
4
+ data.tar.gz: a42fdd9868c43855006b763faa86b0377ae2204f6e9c20893edfbadf49792a60
5
5
  SHA512:
6
- metadata.gz: b1830ebe8c59bd1f7fe66588d2b6b5b8839b487af1fe3c75a876eecc28773996e86dbd61a65af44c28c1b29bbe1a9497a9a234801d50ec19b53878b43f85bc89
7
- data.tar.gz: 4d7d0b6289ed0045e85e4edbacd6482d11c5577932f215cab19f0885c7049a20ad2015ceffaea21e1d6a7459727dd64f370cd4555e45b97d78b2c6b1a5e31575
6
+ metadata.gz: af8ce345aec2b28cef954f6de09ce835d1e4236e115877b04c92227121a85ebedd1b1cf422e3cc1645f2b7ad818cfdf81d9b05bcdb41007bca4fd9115d900fc0
7
+ data.tar.gz: 45ade4eefd27ae3b8f0aed723681b5c1931cd11c34f78f17a21e59aa745a67de27b32272f4c01377950420f4dd0743c84258b9415cf24dea891d253f9ca1fe4b
@@ -8,7 +8,7 @@ module Micro
8
8
  def call_observers(with: :call)
9
9
  proc do |object|
10
10
  Array(with)
11
- .each { |action| object.observers.call(action: action) }
11
+ .each { |action| object.observers.call(action) }
12
12
  end
13
13
  end
14
14
 
@@ -6,39 +6,58 @@ module Micro
6
6
  class Manager
7
7
  EMPTY_HASH = {}.freeze
8
8
 
9
+ SameObserver = -> (observer) do
10
+ -> item { item[0] == :observer && item[1] == observer }
11
+ end
12
+
9
13
  def self.for(subject)
10
14
  new(subject)
11
15
  end
12
16
 
13
17
  def initialize(subject, list = nil)
14
18
  @subject = subject
19
+
15
20
  @list = (list.kind_of?(Array) ? list : []).flatten.tap(&:compact!)
16
21
  end
17
22
 
23
+ def included?(observer)
24
+ @list.any?(&SameObserver[observer])
25
+ end
26
+
18
27
  def attach(observer, options = EMPTY_HASH)
19
- if options[:allow_duplication] || @list.none? { |obs, _data| obs == observer }
20
- @list << [observer, options[:data]]
28
+ if options[:allow_duplication] || !included?(observer)
29
+ @list << [:observer, observer, options[:data]]
21
30
  end
22
31
 
23
32
  self
24
33
  end
25
34
 
35
+ def on(options=EMPTY_HASH)
36
+ action, callable, with = options[:action], options[:call], options[:with]
37
+
38
+ return self unless action.is_a?(Symbol) && callable.respond_to?(:call)
39
+
40
+ arg = with.is_a?(Proc) ? with.call(@subject) : subject
41
+
42
+ @list << [:caller, action, [callable, arg]]
43
+ end
44
+
26
45
  def detach(observer)
27
- @list.delete_if { |obs, _data| obs == observer }
46
+ @list.delete_if(&SameObserver[observer])
28
47
 
29
48
  self
30
49
  end
31
50
 
32
- def call(action: :call)
33
- @list.each do |observer, data|
34
- next unless observer.respond_to?(action)
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)
35
57
 
36
- handler = observer.method(action)
58
+ return handler.call(@subject) if handler.arity == 1
37
59
 
38
- case handler.arity
39
- when 2 then handler.call(@subject, data)
40
- when 1 then handler.call(@subject)
41
- else raise NotImplementedError
60
+ handler.call(@subject, data)
42
61
  end
43
62
  end
44
63
 
@@ -1,5 +1,5 @@
1
1
  module Micro
2
2
  module Observers
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: u-observers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura