dispatio 1.1.0 → 1.2.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 (5) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +11 -0
  3. data/README.md +44 -0
  4. data/lib/dispatio.rb +40 -11
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a460ddbac24649ff60c70b11b27d766a2fb5a748d682ced095e9933604200a96
4
- data.tar.gz: 6449b86f7f5269661ed558b37bc3752d91f24f3b4d875ddb561a59f1975ba862
3
+ metadata.gz: 1291fe6d6ec352d090a78afae44e98656c14026566448a039065f7b58cd9cdf2
4
+ data.tar.gz: 4e70a7cfd9125425d432c70d5eeb3df8829163ce19496378db877b9e8e7edbd2
5
5
  SHA512:
6
- metadata.gz: 82ced686161c8adf45614813ad55425f473efbd8af2ebb1438739ab2570d76e0815fda45056b49aee9cede82139a15afa9ab9f85b4495e3c83bb4618a77b606c
7
- data.tar.gz: ebf88bb77dcfeecbf073ab56d04543bfe5185b4a938082ab30fb7b3149d9f1fa49b73c0e95773e45cf33cd1a932d8ecd22c4b164f9448a322081d0723f46e042
6
+ metadata.gz: '08a3866d4918d6124b60399e0657e3e307647635a0105c9ff2ed54cb679eee236854e7e68ef6bee18939997bf7700cbe50fec87120dbe022b9f6c314da57a81f'
7
+ data.tar.gz: eb83b8f0fb6a6609f44e0f21333645269617c55dd952d43e85c6ae3a6800b020db65d606d629f4efc97e4d770472204a7c81d4a5f7e8bba29fc58b52c9c098f8
data/CHANGELOG.md CHANGED
@@ -2,6 +2,17 @@
2
2
  # dispatio GHANGELOG.md
3
3
 
4
4
 
5
+ ## dispatio 1.2.1 released 2026-09-13
6
+
7
+ * Accept implicit `_suffix`
8
+ * Fix suffix:
9
+
10
+
11
+ ## dispatio 1.2.0 released 2026-09-13
12
+
13
+ * Introduce prefix: or suffix:
14
+
15
+
5
16
  ## dispatio 1.1.0 released 2026-09-12
6
17
 
7
18
  * Focus on #call, alias #dispatch to it
data/README.md CHANGED
@@ -5,6 +5,44 @@
5
5
 
6
6
  A stupid dispatch table tool.
7
7
 
8
+ ```ruby
9
+ require 'dispatio'
10
+
11
+ class MyClass
12
+
13
+ def consume(type, msg)
14
+
15
+ (@ctable ||= Dispatio.make_table(self, 'consume_').call(type, msg)
16
+ # or
17
+ #(@ctable ||= Dispatio.make_table(self, prefix: 'consume_').call(type, msg)
18
+ end
19
+
20
+ def post(type, msg)
21
+
22
+ (@ptable ||= Dispatio.make_table(self, '_post').call(type, msg)
23
+ # or, more explicitely
24
+ #(@ptable ||= Dispatio.make_table(self, suffix: '_post').call(type, msg)
25
+ end
26
+
27
+ protected
28
+
29
+ def consume_this(msg)
30
+ # ...
31
+ end
32
+ def consume_that(msg)
33
+ # ...
34
+ end
35
+ def this_post(msg)
36
+ # ...
37
+ end
38
+ def that_post(msg)
39
+ # ...
40
+ end
41
+ end
42
+ ```
43
+
44
+ The `Dispatio.make_table(klass, prefix_or_opts)` creates a dispatch table by enumerating the matching (prefix or suffix) methods. One can then `call` (or `dispatch`) the table to call the corresponding method.
45
+
8
46
 
9
47
  ## against an instance
10
48
 
@@ -20,6 +58,7 @@ class Foo
20
58
  #dtable.(name, data) #
21
59
  #dtable[name].call(data) # or
22
60
  #dtable[name].(data) #
61
+ #dtable[name][data] #
23
62
  #
24
63
  # just use the one you like
25
64
  end
@@ -35,6 +74,8 @@ class Foo
35
74
  end
36
75
 
37
76
  def dtable; @dtable ||= Dispatio.make_table(self, 'validate_'); end
77
+ # or
78
+ #def dtable; @dtable ||= Dispatio.make_table(self, prefix: 'validate_'); end
38
79
  end
39
80
 
40
81
  foo = Foo.new
@@ -64,6 +105,7 @@ class Bar
64
105
  #dtable.(name, data) #
65
106
  #dtable[name].call(data) # or
66
107
  #dtable[name].(data) #
108
+ #dtable[name][data] #
67
109
  #
68
110
  # just use the one you like
69
111
  end
@@ -79,6 +121,8 @@ class Bar
79
121
  end
80
122
 
81
123
  def dtable; @dtable ||= Dispatio.make_table(self, 'validate_'); end
124
+ # or
125
+ #def dtable; @dtable ||= Dispatio.make_table(self, prefix: 'validate_'); end
82
126
  end
83
127
  end
84
128
 
data/lib/dispatio.rb CHANGED
@@ -1,20 +1,42 @@
1
1
 
2
2
  module Dispatio
3
3
 
4
- VERSION = '1.1.0'.freeze
4
+ VERSION = '1.2.1'.freeze
5
5
 
6
6
  class << self
7
7
 
8
- def make_table(point, prefix)
8
+ def make_table(point, prefix_or_opts)
9
+
10
+ opts =
11
+ case poo = prefix_or_opts
12
+ when /\A_/ then { suffix: poo }
13
+ when String then { prefix: poo }
14
+ else poo; end
15
+
16
+ fail(
17
+ ArgumentError,
18
+ "2nd arg to Dispatio.make_table must be a string or option hash"
19
+ ) unless opts.is_a?(Hash)
20
+
21
+ optklas = [ opts[:prefix], opts[:suffix] ].compact.map(&:class).uniq
22
+ #
23
+ fail(
24
+ ArgumentError,
25
+ "missing or invalid prefix: or suffix: option"
26
+ ) unless optklas == [ String ]
9
27
 
10
28
  Dispatio::Table.new(
11
- prefix,
29
+ opts,
12
30
  point
13
31
  .methods
14
- .select { |m|
15
- m.to_s.start_with?(prefix) }
16
32
  .inject({}) { |h, m|
17
- h[m.to_s[prefix.length..-1]] = point.method(m)
33
+ if px = opts[:prefix]
34
+ h[m.to_s[px.length..-1]] = point.method(m) \
35
+ if m.to_s.start_with?(px)
36
+ else; sx = opts[:suffix]
37
+ h[m.to_s[0..-(sx.length + 1)]] = point.method(m) \
38
+ if m.to_s.end_with?(sx)
39
+ end
18
40
  h }
19
41
  ).freeze
20
42
  end
@@ -22,9 +44,9 @@ module Dispatio
22
44
 
23
45
  class Table
24
46
 
25
- def initialize(prefix, table)
47
+ def initialize(opts, table)
26
48
 
27
- @prefix = prefix
49
+ @opts = opts
28
50
  @table = table.freeze
29
51
  end
30
52
 
@@ -35,9 +57,16 @@ module Dispatio
35
57
 
36
58
  def call(name, *args, **opts, &block)
37
59
 
38
- ( self[name] ||
39
- fail(NoMethodError.new("no :#{@prefix}#{name} method"))
40
- ).call(*args, **opts, &block)
60
+ m = self[name]
61
+
62
+ fail(
63
+ NoMethodError,
64
+ @opts[:prefix] ?
65
+ "no :#{@opts[:prefix]}#{name} method" :
66
+ "no :#{name}#{@opts[:suffix]} method"
67
+ ) unless m
68
+
69
+ m.call(*args, **opts, &block)
41
70
  end
42
71
  alias dispatch call
43
72
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dispatio
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Mettraux