dispatio 1.2.0 → 1.3.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 +13 -0
  3. data/README.md +32 -1
  4. data/lib/dispatio.rb +24 -9
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f40f67d97bc67210b29b6509feaf066a36ad196e2580926dd2190b0bdfc201f0
4
- data.tar.gz: 3412b4cddd334b45d1969270ba75a7670482b995a44663484693564aedfb3009
3
+ metadata.gz: dcd71077a687e29a3ad7bfafc139e64a02c228de8c0a9fa5d815d1e6cb6807d4
4
+ data.tar.gz: ffc9cae009b1d706d0c463cf8206173382dc0b7a3afd9c022711ecbada200613
5
5
  SHA512:
6
- metadata.gz: 1bdd84b304a216c2010e4c5e07539f5896d242775bf3a4b7484dc31a6cb48eec808a3ecb3d4ffe119e3844af3a2c77b1e85b06ac81d508ad54b5d42a46a9c71d
7
- data.tar.gz: bcb954aedf93e66e4a04bc21f11c61f8ac771f98af92ba7ae2a28fc2ce137d11b2ef0602b2aeeb7d503b19be40d7575458edd88b6b81e0eb276f05f5faf61ebb
6
+ metadata.gz: 0e56deb551614afeb997c6748a3a13c83c6d3be0baff7a7acc111aa363ecfab300fd74cc50f738918ec299d242b8d7a29bb6c8b204288e59bf010c49385cce7c
7
+ data.tar.gz: d442514097e44dfd9bf1bb82eac1bbc7c1550626d7fbcaa6714800d6dd4701708cbaa43bff50e2711d798aa1b0a99e9e97c0881d694346aa75d540fa85d0df31
data/CHANGELOG.md CHANGED
@@ -2,6 +2,19 @@
2
2
  # dispatio GHANGELOG.md
3
3
 
4
4
 
5
+ ## dispatio 1.3.0 released 2026-10-02
6
+
7
+ * Introduce make_table() default: value option
8
+ * Introduce Dispatio::Table.names
9
+ * Introduce Dispatio::Table.include?(name)
10
+
11
+
12
+ ## dispatio 1.2.1 released 2026-09-13
13
+
14
+ * Accept implicit `_suffix`
15
+ * Fix suffix:
16
+
17
+
5
18
  ## dispatio 1.2.0 released 2026-09-13
6
19
 
7
20
  * Introduce prefix: or suffix:
data/README.md CHANGED
@@ -19,7 +19,9 @@ class MyClass
19
19
 
20
20
  def post(type, msg)
21
21
 
22
- (@ptable ||= Dispatio.make_table(self, suffix: '_post').call(type, msg)
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)
23
25
  end
24
26
 
25
27
  protected
@@ -133,6 +135,35 @@ Bar.consume('bar', 'world') # ==> [ Bar, :vbar, 'world' ]
133
135
  Bar.consume(:nada, 'nemo') # ==> NoMethodError, 'no :validate_nada method'
134
136
  ```
135
137
 
138
+ ## the default: v option
139
+
140
+ ```ruby
141
+ class Bonk; class << self
142
+
143
+ def consume(name, data)
144
+
145
+ dtable.dispatch(name, data)
146
+ end
147
+
148
+ protected
149
+
150
+ def validate_foo(data); [ self, :vfoo, data ]; end
151
+
152
+ def dtable
153
+
154
+ @dtable ||=
155
+ Dispatio.make_table(self, prefix: 'validate_', default: :x)
156
+ end
157
+ end; end
158
+
159
+ Bonk.consume('foo', { text: 'blah blah' })
160
+ # => [ Bonk, :vfoo, { text: 'blah blah' } ]
161
+ Bonk.consume(:foo, { text: 'bullsh*t' })
162
+ # => [ Bonk, :vfoo, { text: 'bullsh*t' } ]
163
+ Bonk.consume(:bar, { text: 'prank' })
164
+ # => :x
165
+ ```
166
+
136
167
 
137
168
  ## LICENSE
138
169
 
data/lib/dispatio.rb CHANGED
@@ -1,15 +1,17 @@
1
1
 
2
2
  module Dispatio
3
3
 
4
- VERSION = '1.2.0'.freeze
4
+ VERSION = '1.3.0'.freeze
5
5
 
6
6
  class << self
7
7
 
8
8
  def make_table(point, prefix_or_opts)
9
9
 
10
10
  opts =
11
- prefix_or_opts.is_a?(String) ? { prefix: prefix_or_opts } :
12
- prefix_or_opts
11
+ case poo = prefix_or_opts
12
+ when /\A_/ then { suffix: poo }
13
+ when String then { prefix: poo }
14
+ else poo; end
13
15
 
14
16
  fail(
15
17
  ArgumentError,
@@ -29,9 +31,11 @@ module Dispatio
29
31
  .methods
30
32
  .inject({}) { |h, m|
31
33
  if px = opts[:prefix]
32
- h[m.to_s[px.size..-1]] = point.method(m) if m.to_s.start_with?(px)
34
+ h[m.to_s[px.length..-1]] = point.method(m) \
35
+ if m.to_s.start_with?(px)
33
36
  else; sx = opts[:suffix]
34
- h[m.to_s[0, sx.size-2]] = point.method(m) if m.to_s.end_with?(sx)
37
+ h[m.to_s[0..-(sx.length + 1)]] = point.method(m) \
38
+ if m.to_s.end_with?(sx)
35
39
  end
36
40
  h }
37
41
  ).freeze
@@ -55,16 +59,27 @@ module Dispatio
55
59
 
56
60
  m = self[name]
57
61
 
62
+ return m.call(*args, **opts, &block) if m
63
+
64
+ return @opts[:default] if @opts.has_key?(:default)
65
+
58
66
  fail(
59
67
  NoMethodError,
60
68
  @opts[:prefix] ?
61
69
  "no :#{@opts[:prefix]}#{name} method" :
62
- "no :#{name}#{@opts[:suffix]} method"
63
- ) unless m
64
-
65
- m.call(*args, **opts, &block)
70
+ "no :#{name}#{@opts[:suffix]} method")
66
71
  end
67
72
  alias dispatch call
73
+
74
+ def names
75
+
76
+ @table.keys
77
+ end
78
+
79
+ def include?(name)
80
+
81
+ @table.keys.include?(name.to_s)
82
+ end
68
83
  end
69
84
  end
70
85
 
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.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Mettraux