u-observers 0.4.0 → 0.5.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: d9c81561b00ca412faa80abc349e8a87251f4d84ce81da5fcd95ca1117e2bd2e
4
- data.tar.gz: da923ace22c60cac08f710dccac65367b0bb568d9b4a99fbbe8bc164c8824b12
3
+ metadata.gz: 2268ebc8924bf446ca73f3b1fbdb0cb4a8fba9f60eb01173e68fa7486747b1ed
4
+ data.tar.gz: 329fad95d53450a550c9fc964fec4bb3732f5a63d684835f0ef6d766df839d15
5
5
  SHA512:
6
- metadata.gz: 69eedf04951d55a0889412d50e62e226fdfe2561a3a3a52df087f0091ec15ec696fe002fe906a00fdf15119455c4c7f7f1a54a464c83e56c3ec5d6ff143ed1f4
7
- data.tar.gz: 805003dac964746271f6b7789527e608fbd2caf77dd8f9eb735b8d30f42159e9d577f0a8bb0b3b60199fc764b40c0ae7a1edd005deb2d01f7e9567d49e18c4fd
6
+ metadata.gz: d3a2f1895221f171ca6499bca6284a9d349e77b9d6661b41a0b15c662d9f400af86d7d0b77e4331ca9a985b6db2d6628415be5422944cb09aba32d676aa537eb
7
+ data.tar.gz: 82daa067cd2cf85c3b00c0aca546131e38c1ee365336d5f01cb868b81d7b91be806ecb606c61ad5e56d2364f9341f8636d093f02d6b6f424ddccc0702b11f7e7
@@ -8,15 +8,15 @@ module Micro
8
8
 
9
9
  module ClassMethods
10
10
  def notify_observers!(with:)
11
- proc { |object| with.each { |evt_or_act| object.observers.call(evt_or_act) } }
11
+ proc { |object| with.each { |evt_or_act| object.observers.notify(evt_or_act) } }
12
12
  end
13
13
 
14
14
  def notify_observers(*events)
15
15
  notify_observers!(with: EventsOrActions[events])
16
16
  end
17
17
 
18
- def call_observers(action: :call)
19
- notify_observers!(with: EventsOrActions[action])
18
+ def call_observers(options = Utils::EMPTY_HASH)
19
+ notify_observers!(with: EventsOrActions.fetch_actions(options))
20
20
  end
21
21
  end
22
22
 
@@ -4,15 +4,21 @@ module Micro
4
4
  module Observers
5
5
 
6
6
  module EventsOrActions
7
- DEFAULT = [:call]
7
+ DEFAULTS = [:call]
8
8
 
9
- def self.[](values)
10
- vals = Utils.compact_array(values)
9
+ def self.[](value)
10
+ values = Utils.compact_array(value)
11
11
 
12
- vals.empty? ? DEFAULT : vals
12
+ values.empty? ? DEFAULTS : values
13
13
  end
14
14
 
15
- private_constant :DEFAULT
15
+ def self.fetch_actions(hash)
16
+ return self[hash.fetch(:actions) { hash.fetch(:action) }] if hash.is_a?(Hash)
17
+
18
+ raise ArgumentError, 'expected a hash with the key :action or :actions'
19
+ end
20
+
21
+ private_constant :DEFAULTS
16
22
  end
17
23
 
18
24
  end
@@ -4,8 +4,6 @@ module Micro
4
4
  module Observers
5
5
 
6
6
  class Manager
7
- EMPTY_HASH = {}.freeze
8
-
9
7
  EqualTo = -> (observer) do
10
8
  -> item { item[0] == :observer && item[1] == observer }
11
9
  end
@@ -24,7 +22,7 @@ module Micro
24
22
  @list.any?(&EqualTo[observer])
25
23
  end
26
24
 
27
- def attach(observer, options = EMPTY_HASH)
25
+ def attach(observer, options = Utils::EMPTY_HASH)
28
26
  if options[:allow_duplication] || !included?(observer)
29
27
  @list << [:observer, observer, options[:data]]
30
28
  end
@@ -32,7 +30,7 @@ module Micro
32
30
  self
33
31
  end
34
32
 
35
- def on(options=EMPTY_HASH)
33
+ def on(options = Utils::EMPTY_HASH)
36
34
  event, callable, with = options[:event], options[:call], options[:with]
37
35
 
38
36
  return self unless event.is_a?(Symbol) && callable.respond_to?(:call)
@@ -48,14 +46,14 @@ module Micro
48
46
  self
49
47
  end
50
48
 
51
- def notify(event)
52
- notify!(event)
49
+ def notify(*events)
50
+ EventsOrActions[events].each { |act| notify!(act) }
53
51
 
54
52
  self
55
53
  end
56
54
 
57
- def call(*actions)
58
- EventsOrActions[actions].each { |act| notify!(act) }
55
+ def call(options = Utils::EMPTY_HASH)
56
+ EventsOrActions.fetch_actions(options).each { |act| notify!(act) }
59
57
 
60
58
  self
61
59
  end
@@ -80,7 +78,7 @@ module Micro
80
78
  end
81
79
  end
82
80
 
83
- private_constant :EMPTY_HASH, :EqualTo
81
+ private_constant :EqualTo
84
82
  end
85
83
 
86
84
  end
@@ -4,6 +4,8 @@ module Micro
4
4
  module Observers
5
5
 
6
6
  module Utils
7
+ EMPTY_HASH = {}.freeze
8
+
7
9
  def self.compact_array(value)
8
10
  Array(value).flatten.tap(&:compact!)
9
11
  end
@@ -1,5 +1,5 @@
1
1
  module Micro
2
2
  module Observers
3
- VERSION = '0.4.0'
3
+ VERSION = '0.5.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.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura