u-observers 2.2.1 → 2.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: d7ebc064f3e8384d9322c640d3d52b1bf70bf6d5159ae67bf3cd34bdf67482eb
4
- data.tar.gz: 398d1ab0fc5852a42416c5bc16c377c4a55d760e1e327e2149b58b7db23cae73
3
+ metadata.gz: 55ea9f6a206dba9526be1d7777969b26311f725652b3020344db33563a5835c8
4
+ data.tar.gz: d8a6e1bf2e73d15fadbd71e82aa17e0c511fb11d97a7839ba165c50589324b66
5
5
  SHA512:
6
- metadata.gz: 0d805d69fb5cb7d08d1ef5aec34742d08845d2d544cec1262ec63d99788adb5053ed87558957849f1b9843b772929e9490f1ae243ee3c568d87b94e1b432a8c5
7
- data.tar.gz: 932bfa8189f162fea8160779d521168fdebe253d36414c0b05fbccf2a8c661dc94e127fb5a942523e0ae415142d6090cd8a361124f60f550b7d2166c87106c0e
6
+ metadata.gz: c9b907879c02a2071b6c194af9f5c1a6e50a3bd17655beaed0f9dfb7858c892f657c9a57666d320d23d0055a48625d168d625fbae19ca7a560722b8c1b475d31
7
+ data.tar.gz: 4a551b0a43d15b5968d843d2a3b737ae245e177da3ac0c595f9265be887ff3b2b1205a3544c368973cae993869a9083fbffdc2cf64dab9b18c5d6d17eae5282b
data/README.md CHANGED
@@ -45,6 +45,10 @@ Because of this issue, I decided to create a gem that encapsulates the pattern w
45
45
  - [Defining observers that execute only once](#defining-observers-that-execute-only-once)
46
46
  - [`observers.attach(*args, perform_once: true)`](#observersattachargs-perform_once-true)
47
47
  - [`observers.once(event:, call:, ...)`](#observersonceevent-call-)
48
+ - [Defining observers using blocks](#defining-observers-using-blocks)
49
+ - [order.observers.on()](#orderobserverson)
50
+ - [order.observers.on()](#orderobserverson-1)
51
+ - [Replacing a block by a `lambda`/`proc`](#replacing-a-block-by-a-lambdaproc)
48
52
  - [Detaching observers](#detaching-observers)
49
53
  - [ActiveRecord and ActiveModel integrations](#activerecord-and-activemodel-integrations)
50
54
  - [notify_observers_on()](#notify_observers_on)
@@ -67,7 +71,7 @@ gem 'u-observers'
67
71
  | u-observers | branch | ruby | activerecord |
68
72
  | ----------- | ------- | -------- | ------------- |
69
73
  | unreleased | main | >= 2.2.0 | >= 3.2, < 6.1 |
70
- | 2.2.0 | v2.x | >= 2.2.0 | >= 3.2, < 6.1 |
74
+ | 2.3.0 | v2.x | >= 2.2.0 | >= 3.2, < 6.1 |
71
75
  | 1.0.0 | v1.x | >= 2.2.0 | >= 3.2, < 6.1 |
72
76
 
73
77
  > **Note**: The ActiveRecord isn't a dependency, but you could add a module to enable some static methods that were designed to be used with its [callbacks](https://guides.rubyonrails.org/active_record_callbacks.html).
@@ -375,6 +379,83 @@ order.cancel! # Nothing will happen because there aren't observers.
375
379
 
376
380
  [⬆️ &nbsp; Back to Top](#table-of-contents-)
377
381
 
382
+ ### Defining observers using blocks
383
+
384
+ The methods `#on()` and `#once()` can receive the event name (a `symbol`) and a block to define observers.
385
+
386
+ #### order.observers.on()
387
+
388
+ ```ruby
389
+ class Order
390
+ include Micro::Observers
391
+
392
+ def cancel!
393
+ observers.notify!(:canceled)
394
+ end
395
+ end
396
+
397
+ order = Order.new
398
+ order.observers.on(:canceled) do |event|
399
+ puts "The order #(#{event.subject.object_id}) has been canceled."
400
+ end
401
+
402
+ order.observers.some? # true
403
+
404
+ order.cancel! # The order #(70301497466060) has been canceled.
405
+
406
+ order.observers.some? # true
407
+ ```
408
+
409
+ #### order.observers.on()
410
+
411
+ ```ruby
412
+ class Order
413
+ include Micro::Observers
414
+
415
+ def cancel!
416
+ observers.notify!(:canceled)
417
+ end
418
+ end
419
+
420
+ order = Order.new
421
+ order.observers.once(:canceled) do |event|
422
+ puts "The order #(#{event.subject.object_id}) has been canceled."
423
+ end
424
+
425
+ order.observers.some? # true
426
+
427
+ order.cancel! # The order #(70301497466060) has been canceled.
428
+
429
+ order.observers.some? # false
430
+ ```
431
+
432
+ #### Replacing a block by a `lambda`/`proc`
433
+
434
+ Ruby allows you to replace any block with a `lambda`/`proc`. e.g.
435
+
436
+ ```ruby
437
+ class Order
438
+ include Micro::Observers
439
+
440
+ def cancel!
441
+ observers.notify!(:canceled)
442
+ end
443
+ end
444
+
445
+ NotifyAfterCancel = -> event { puts "The order #(#{event.subject.object_id}) has been canceled." }
446
+
447
+ order = Order.new
448
+ order.observers.once(:canceled, &NotifyAfterCancel)
449
+
450
+ order.observers.some? # true
451
+ order.cancel! # The order #(70301497466060) has been canceled.
452
+
453
+ order.observers.some? # false
454
+ order.cancel! # Nothing will happen because there aren't observers.
455
+ ```
456
+
457
+ [⬆️ &nbsp; Back to Top](#table-of-contents-)
458
+
378
459
  ### Detaching observers
379
460
 
380
461
  As shown in the first example, you can use the `observers.detach()` to remove observers.
@@ -44,6 +44,10 @@ Por causa desse problema, decidi criar uma gem que encapsula o padrão sem alter
44
44
  - [Definindo observers que executam apenas uma vez](#definindo-observers-que-executam-apenas-uma-vez)
45
45
  - [`observers.attach(*args, perform_once: true)`](#observersattachargs-perform_once-true)
46
46
  - [`observers.once(event:, call:, ...)`](#observersonceevent-call-)
47
+ - [Definindo observers com blocos](#definindo-observers-com-blocos)
48
+ - [order.observers.on()](#orderobserverson)
49
+ - [order.observers.on()](#orderobserverson-1)
50
+ - [Substituindo um bloco por um `lambda`/`proc`](#substituindo-um-bloco-por-um-lambdaproc)
47
51
  - [Desanexando observers](#desanexando-observers)
48
52
  - [Integrações ActiveRecord e ActiveModel](#integrações-activerecord-e-activemodel)
49
53
  - [notify_observers_on()](#notify_observers_on)
@@ -66,7 +70,7 @@ gem 'u-observers'
66
70
  | u-observers | branch | ruby | activerecord |
67
71
  | ----------- | ------- | -------- | ------------- |
68
72
  | unreleased | main | >= 2.2.0 | >= 3.2, < 6.1 |
69
- | 2.2.1 | v2.x | >= 2.2.0 | >= 3.2, < 6.1 |
73
+ | 2.3.0 | v2.x | >= 2.2.0 | >= 3.2, < 6.1 |
70
74
  | 1.0.0 | v1.x | >= 2.2.0 | >= 3.2, < 6.1 |
71
75
 
72
76
  > **Nota**: O ActiveRecord não é uma dependência, mas você pode adicionar um módulo para habilitar alguns métodos estáticos que foram projetados para serem usados ​​com seus [callbacks](https://guides.rubyonrails.org/active_record_callbacks.html).
@@ -373,7 +377,84 @@ order.observers.some? # false
373
377
  order.cancel! # Nothing will happen because there aren't observers.
374
378
  ```
375
379
 
376
- [⬆️ &nbsp; Back to Top](#table-of-contents-)
380
+ [⬆️ Voltar para o índice](#índice-)
381
+
382
+ ### Definindo observers com blocos
383
+
384
+ Os métodos `#on()` e `#once()` podem receber um evento (a `symbol`) e um bloco para definir observers.
385
+
386
+ #### order.observers.on()
387
+
388
+ ```ruby
389
+ class Order
390
+ include Micro::Observers
391
+
392
+ def cancel!
393
+ observers.notify!(:canceled)
394
+ end
395
+ end
396
+
397
+ order = Order.new
398
+ order.observers.on(:canceled) do |event|
399
+ puts "The order #(#{event.subject.object_id}) has been canceled."
400
+ end
401
+
402
+ order.observers.some? # true
403
+
404
+ order.cancel! # The order #(70301497466060) has been canceled.
405
+
406
+ order.observers.some? # true
407
+ ```
408
+
409
+ #### order.observers.on()
410
+
411
+ ```ruby
412
+ class Order
413
+ include Micro::Observers
414
+
415
+ def cancel!
416
+ observers.notify!(:canceled)
417
+ end
418
+ end
419
+
420
+ order = Order.new
421
+ order.observers.once(:canceled) do |event|
422
+ puts "The order #(#{event.subject.object_id}) has been canceled."
423
+ end
424
+
425
+ order.observers.some? # true
426
+
427
+ order.cancel! # The order #(70301497466060) has been canceled.
428
+
429
+ order.observers.some? # false
430
+ ```
431
+
432
+ #### Substituindo um bloco por um `lambda`/`proc`
433
+
434
+ Ruby permite que você substitua qualquer bloco com um `lambda`/`proc`. Exemplo:
435
+
436
+ ```ruby
437
+ class Order
438
+ include Micro::Observers
439
+
440
+ def cancel!
441
+ observers.notify!(:canceled)
442
+ end
443
+ end
444
+
445
+ NotifyAfterCancel = -> event { puts "The order #(#{event.subject.object_id}) has been canceled." }
446
+
447
+ order = Order.new
448
+ order.observers.once(:canceled, &NotifyAfterCancel)
449
+
450
+ order.observers.some? # true
451
+ order.cancel! # The order #(70301497466060) has been canceled.
452
+
453
+ order.observers.some? # false
454
+ order.cancel! # Nothing will happen because there aren't observers.
455
+ ```
456
+
457
+ [⬆️ Voltar para o índice](#índice-)
377
458
 
378
459
  ### Desanexando observers
379
460
 
@@ -409,7 +490,7 @@ order.observers.some? # false
409
490
  order.observers.count # 0
410
491
  ```
411
492
 
412
- [⬆️ &nbsp; Back to Top](#table-of-contents-)
493
+ [⬆️ Voltar para o índice](#índice-)
413
494
 
414
495
  ### Integrações ActiveRecord e ActiveModel
415
496
 
@@ -42,13 +42,20 @@ module Micro
42
42
  def attach(*args); @subscribers.attach(args) and self; end
43
43
  def detach(*args); @subscribers.detach(args) and self; end
44
44
 
45
- def on(options = Utils::EMPTY_HASH); @subscribers.on(options) and self; end
46
- def once(options = Utils::EMPTY_HASH); @subscribers.once(options) and self; end
45
+ CallableOptions = -> (arg, block) do
46
+ arg.is_a?(Symbol) && block ? { event: arg, call: block } : arg
47
+ end
48
+
49
+ def on(arg = Utils::EMPTY_HASH, &block)
50
+ @subscribers.on(CallableOptions[arg, block]) and self
51
+ end
47
52
 
48
- def off(*args)
49
- @subscribers.off(args) and self
53
+ def once(arg = Utils::EMPTY_HASH, &block)
54
+ @subscribers.once(CallableOptions[arg, block]) and self
50
55
  end
51
56
 
57
+ def off(*args); @subscribers.off(args) and self; end
58
+
52
59
  def notify(*events, data: nil)
53
60
  broadcast_if_subject_changed(Event::Names.fetch(events), data)
54
61
  end
@@ -93,7 +100,7 @@ module Micro
93
100
  self
94
101
  end
95
102
 
96
- private_constant :INVALID_BOOLEAN_MSG, :CALL_EVENT
103
+ private_constant :INVALID_BOOLEAN_MSG, :CALL_EVENT; :CallableOptions
97
104
  end
98
105
 
99
106
  end
@@ -1,5 +1,5 @@
1
1
  module Micro
2
2
  module Observers
3
- VERSION = '2.2.1'
3
+ VERSION = '2.3.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: 2.2.1
4
+ version: 2.3.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-11-18 00:00:00.000000000 Z
11
+ date: 2020-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler