tiny_bus 1.0.0 → 1.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/tiny_bus.rb +20 -17
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: efb4754b66b36fa51ecd847ef7a0a92b8ef884c662e50d51cb3a1353d5fe98dc
4
- data.tar.gz: dc6c393864d50f47131e7efc4dd2ec90781b5552ef68a452b4d0a92998dceb88
3
+ metadata.gz: d4d536f5d26c59f828baf005884e8d6be51596b61c151a2c77e129c2a1f80305
4
+ data.tar.gz: 885f08feed148a3808dfde24a38871544e01ac82fe27114ef8000e7fe0f8a630
5
5
  SHA512:
6
- metadata.gz: 4275c81f97f72fca9aa1d33a8cdff08afc70392539b3d98096a7878118c69f7eabd5fbb9e336c76a3adf357130ec492ce78715aed3edfe6cb83d83e605ec4e68
7
- data.tar.gz: 99dc348ba1e2d1281da1bd84976a6b54bb14792633f93a36e4bf507cc7fe5e1a48faae79a2d24fd6e9fd15c88c5dbcf1533b6c72e264ffb41a751e88ae432580
6
+ metadata.gz: 8ead2a2d5889921a3544a8ffc91fe93929bb3a52efe3edd1de14c0046533641f7a170f1de32b065cdeb19984d007904a6ef391a9b58fcb61839baf557868a33e
7
+ data.tar.gz: 453bd1af26f50d84155a45ba454f66fbdf1cd83b6418cd0543120c3a012a0f91738d6109faea9f983656ed4bdd2ebf0992de95ca449fc3fcbeb0b154a008bd6c
data/lib/tiny_bus.rb CHANGED
@@ -6,23 +6,23 @@ require 'tiny_log'
6
6
  # This class implements a very simpler PubSub system where:
7
7
  # - subscribers can subscribe via the #sub method
8
8
  # - subscribers can unsubscribe via the #unsub method
9
- # - msgs can enter the MsgBus via the #msg method
9
+ # - msgs can enter the TinyBus via the #msg method
10
10
  #
11
- # The messages that come into this MsgBus are assumed to be Hash-like, in the
11
+ # The messages that come into this TinyBus are assumed to be Hash-like, in the
12
12
  # sense that they have a 'topic' key that can be accessed using Hash-like key
13
13
  # access syntax, and that the 'topic' key will serve as the method of
14
14
  # distribution.
15
15
  #
16
16
  # Usage:
17
- # mb = MsgBus.new
18
- # mb.sub('news', <some object that responds to #msg)
19
- # mb.msg({'topic' => 'news', 'details' => 'Historic happenings!}) # goes to 'news' subscribers
20
- # mb.msg({'topic' => 'whatever', 'details' => 'Historic happenings!}) # goes to dead letter output, or raises exception, depending on the configuration
17
+ # t = TinyBus.new
18
+ # t.sub('news', <some object that responds to #msg)
19
+ # t.msg({'topic' => 'news', 'details' => 'Historic happenings!}) # goes to 'news' subscribers
20
+ # t.msg({'topic' => 'whatever', 'details' => 'Historic happenings!}) # goes to dead letter output, or raises exception, depending on the configuration
21
21
  #
22
22
  # Initialization options:
23
- # MsgBus.new(log: <some object that responds to #puts>) # will send a copy of all successful messages to the log
24
- # MsgBus.new(dead: <some object that responds to #puts>) # will send a copy of all unsuccessful messages to the dead object
25
- # MsgBus.new(raise_on_dead: true) # strict mode for undeliverable messages, defaults to false
23
+ # TinyBus.new(log: <some object that responds to #puts>) # will send a copy of all successful messages to the log
24
+ # TinyBus.new(dead: <some object that responds to #puts>) # will send a copy of all unsuccessful messages to the dead object
25
+ # TinyBus.new(raise_on_dead: true) # strict mode for undeliverable messages, defaults to false
26
26
  class TinyBus
27
27
  # log:
28
28
  # if specified it should be a valid filename
@@ -45,11 +45,11 @@ class TinyBus
45
45
  # adds a subscriber to a topic
46
46
  #
47
47
  # topics can be any string that doesn't start with a dot (.) - dot topics are
48
- # reserved for internal MsgBus usage, such as:
48
+ # reserved for internal TinyBus usage, such as:
49
49
  # - .log
50
50
  def sub(topic, subber)
51
- raise SubscriptionToDotTopicError.new("Cannot subscribe to dot topic `#{topic}', because these are reserved for internal use") if topic.start_with?('.')
52
- raise SubscriberDoesNotMsg.new("The specified subscriber type `#{subber.class.inspect}' does not respond to #msg") unless subber.respond_to?(:msg)
51
+ raise TinyBus::SubscriptionToDotTopicError.new("Cannot subscribe to dot topic `#{topic}', because these are reserved for internal use") if topic.start_with?('.')
52
+ raise TinyBus::SubscriberDoesNotMsg.new("The specified subscriber type `#{subber.class.inspect}' does not respond to #msg") unless subber.respond_to?(:msg)
53
53
 
54
54
  @subs[topic] ||= Set.new
55
55
  @subs[topic] << subber
@@ -84,7 +84,7 @@ class TinyBus
84
84
  @log.puts annotated
85
85
  else
86
86
  if @raise_on_dead
87
- raise DeadMsgException.new("Could not deliver message to topic `#{t}'")
87
+ raise TinyBus::DeadMsgError.new("Could not deliver message to topic `#{t}'")
88
88
  else
89
89
  @stats['.dead'] += 1
90
90
  @dead.puts annotated
@@ -92,13 +92,16 @@ class TinyBus
92
92
  end
93
93
  end
94
94
 
95
+ # helpful for debugging, gives you a count of the number of messages sent to
96
+ # each topic, including the .dead topic, which is where messages go where
97
+ # there are no subscribes for a given topic
95
98
  def to_s
96
99
  <<~DEBUG
97
- MsgBus stats: #{@subs.keys.length > 0 ? "\n " + @stats.keys.sort.map{|t| "#{t.rjust(12)}: #{@stats[t]}" }.join("\n ") : '<NONE>'}
100
+ TinyBus stats: #{@subs.keys.length > 0 ? "\n " + @stats.keys.sort.map{|t| "#{t.rjust(12)}: #{@stats[t]}" }.join("\n ") : '<NONE>'}
98
101
  DEBUG
99
102
  end
100
103
  end
101
104
 
102
- class DeadMsgError < RuntimeError; end
103
- class SubscriptionToDotTopicError < RuntimeError; end
104
- class SubscriberDoesNotMsg < RuntimeError; end
105
+ class TinyBus::DeadMsgError < RuntimeError; end
106
+ class TinyBus::SubscriptionToDotTopicError < RuntimeError; end
107
+ class TinyBus::SubscriberDoesNotMsg < RuntimeError; end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny_bus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt