dispatio 1.1.0 → 1.2.0

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 +5 -0
  3. data/README.md +42 -0
  4. data/lib/dispatio.rb +36 -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: f40f67d97bc67210b29b6509feaf066a36ad196e2580926dd2190b0bdfc201f0
4
+ data.tar.gz: 3412b4cddd334b45d1969270ba75a7670482b995a44663484693564aedfb3009
5
5
  SHA512:
6
- metadata.gz: 82ced686161c8adf45614813ad55425f473efbd8af2ebb1438739ab2570d76e0815fda45056b49aee9cede82139a15afa9ab9f85b4495e3c83bb4618a77b606c
7
- data.tar.gz: ebf88bb77dcfeecbf073ab56d04543bfe5185b4a938082ab30fb7b3149d9f1fa49b73c0e95773e45cf33cd1a932d8ecd22c4b164f9448a322081d0723f46e042
6
+ metadata.gz: 1bdd84b304a216c2010e4c5e07539f5896d242775bf3a4b7484dc31a6cb48eec808a3ecb3d4ffe119e3844af3a2c77b1e85b06ac81d508ad54b5d42a46a9c71d
7
+ data.tar.gz: bcb954aedf93e66e4a04bc21f11c61f8ac771f98af92ba7ae2a28fc2ce137d11b2ef0602b2aeeb7d503b19be40d7575458edd88b6b81e0eb276f05f5faf61ebb
data/CHANGELOG.md CHANGED
@@ -2,6 +2,11 @@
2
2
  # dispatio GHANGELOG.md
3
3
 
4
4
 
5
+ ## dispatio 1.2.0 released 2026-09-13
6
+
7
+ * Introduce prefix: or suffix:
8
+
9
+
5
10
  ## dispatio 1.1.0 released 2026-09-12
6
11
 
7
12
  * Focus on #call, alias #dispatch to it
data/README.md CHANGED
@@ -5,6 +5,42 @@
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, suffix: '_post').call(type, msg)
23
+ end
24
+
25
+ protected
26
+
27
+ def consume_this(msg)
28
+ # ...
29
+ end
30
+ def consume_that(msg)
31
+ # ...
32
+ end
33
+ def this_post(msg)
34
+ # ...
35
+ end
36
+ def that_post(msg)
37
+ # ...
38
+ end
39
+ end
40
+ ```
41
+
42
+ 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.
43
+
8
44
 
9
45
  ## against an instance
10
46
 
@@ -20,6 +56,7 @@ class Foo
20
56
  #dtable.(name, data) #
21
57
  #dtable[name].call(data) # or
22
58
  #dtable[name].(data) #
59
+ #dtable[name][data] #
23
60
  #
24
61
  # just use the one you like
25
62
  end
@@ -35,6 +72,8 @@ class Foo
35
72
  end
36
73
 
37
74
  def dtable; @dtable ||= Dispatio.make_table(self, 'validate_'); end
75
+ # or
76
+ #def dtable; @dtable ||= Dispatio.make_table(self, prefix: 'validate_'); end
38
77
  end
39
78
 
40
79
  foo = Foo.new
@@ -64,6 +103,7 @@ class Bar
64
103
  #dtable.(name, data) #
65
104
  #dtable[name].call(data) # or
66
105
  #dtable[name].(data) #
106
+ #dtable[name][data] #
67
107
  #
68
108
  # just use the one you like
69
109
  end
@@ -79,6 +119,8 @@ class Bar
79
119
  end
80
120
 
81
121
  def dtable; @dtable ||= Dispatio.make_table(self, 'validate_'); end
122
+ # or
123
+ #def dtable; @dtable ||= Dispatio.make_table(self, prefix: 'validate_'); end
82
124
  end
83
125
  end
84
126
 
data/lib/dispatio.rb CHANGED
@@ -1,20 +1,38 @@
1
1
 
2
2
  module Dispatio
3
3
 
4
- VERSION = '1.1.0'.freeze
4
+ VERSION = '1.2.0'.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
+ prefix_or_opts.is_a?(String) ? { prefix: prefix_or_opts } :
12
+ prefix_or_opts
13
+
14
+ fail(
15
+ ArgumentError,
16
+ "2nd arg to Dispatio.make_table must be a string or option hash"
17
+ ) unless opts.is_a?(Hash)
18
+
19
+ optklas = [ opts[:prefix], opts[:suffix] ].compact.map(&:class).uniq
20
+ #
21
+ fail(
22
+ ArgumentError,
23
+ "missing or invalid prefix: or suffix: option"
24
+ ) unless optklas == [ String ]
9
25
 
10
26
  Dispatio::Table.new(
11
- prefix,
27
+ opts,
12
28
  point
13
29
  .methods
14
- .select { |m|
15
- m.to_s.start_with?(prefix) }
16
30
  .inject({}) { |h, m|
17
- h[m.to_s[prefix.length..-1]] = point.method(m)
31
+ if px = opts[:prefix]
32
+ h[m.to_s[px.size..-1]] = point.method(m) if m.to_s.start_with?(px)
33
+ else; sx = opts[:suffix]
34
+ h[m.to_s[0, sx.size-2]] = point.method(m) if m.to_s.end_with?(sx)
35
+ end
18
36
  h }
19
37
  ).freeze
20
38
  end
@@ -22,9 +40,9 @@ module Dispatio
22
40
 
23
41
  class Table
24
42
 
25
- def initialize(prefix, table)
43
+ def initialize(opts, table)
26
44
 
27
- @prefix = prefix
45
+ @opts = opts
28
46
  @table = table.freeze
29
47
  end
30
48
 
@@ -35,9 +53,16 @@ module Dispatio
35
53
 
36
54
  def call(name, *args, **opts, &block)
37
55
 
38
- ( self[name] ||
39
- fail(NoMethodError.new("no :#{@prefix}#{name} method"))
40
- ).call(*args, **opts, &block)
56
+ m = self[name]
57
+
58
+ fail(
59
+ NoMethodError,
60
+ @opts[:prefix] ?
61
+ "no :#{@opts[:prefix]}#{name} method" :
62
+ "no :#{name}#{@opts[:suffix]} method"
63
+ ) unless m
64
+
65
+ m.call(*args, **opts, &block)
41
66
  end
42
67
  alias dispatch call
43
68
  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.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Mettraux