dispatio 1.0.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +70 -3
- data/lib/dispatio.rb +43 -12
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f40f67d97bc67210b29b6509feaf066a36ad196e2580926dd2190b0bdfc201f0
|
|
4
|
+
data.tar.gz: 3412b4cddd334b45d1969270ba75a7670482b995a44663484693564aedfb3009
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1bdd84b304a216c2010e4c5e07539f5896d242775bf3a4b7484dc31a6cb48eec808a3ecb3d4ffe119e3844af3a2c77b1e85b06ac81d508ad54b5d42a46a9c71d
|
|
7
|
+
data.tar.gz: bcb954aedf93e66e4a04bc21f11c61f8ac771f98af92ba7ae2a28fc2ce137d11b2ef0602b2aeeb7d503b19be40d7575458edd88b6b81e0eb276f05f5faf61ebb
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,16 @@
|
|
|
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
|
+
|
|
10
|
+
## dispatio 1.1.0 released 2026-09-12
|
|
11
|
+
|
|
12
|
+
* Focus on #call, alias #dispatch to it
|
|
13
|
+
|
|
14
|
+
|
|
5
15
|
## dispatio 1.0.0 released 2026-09-11
|
|
6
16
|
|
|
7
17
|
* First release
|
data/README.md
CHANGED
|
@@ -1,16 +1,64 @@
|
|
|
1
1
|
|
|
2
2
|
# dispatio
|
|
3
3
|
|
|
4
|
+
[](http://badge.fury.io/rb/dispatio)
|
|
5
|
+
|
|
4
6
|
A stupid dispatch table tool.
|
|
5
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
|
+
|
|
44
|
+
|
|
6
45
|
## against an instance
|
|
7
46
|
|
|
8
47
|
```ruby
|
|
48
|
+
require 'dispatio'
|
|
49
|
+
|
|
9
50
|
class Foo
|
|
10
51
|
|
|
11
52
|
def consume(name, data)
|
|
12
53
|
|
|
13
|
-
dtable.dispatch(name, data)
|
|
54
|
+
dtable.dispatch(name, data) # or
|
|
55
|
+
#dtable.call(name, data) #
|
|
56
|
+
#dtable.(name, data) #
|
|
57
|
+
#dtable[name].call(data) # or
|
|
58
|
+
#dtable[name].(data) #
|
|
59
|
+
#dtable[name][data] #
|
|
60
|
+
#
|
|
61
|
+
# just use the one you like
|
|
14
62
|
end
|
|
15
63
|
|
|
16
64
|
protected
|
|
@@ -24,6 +72,8 @@ class Foo
|
|
|
24
72
|
end
|
|
25
73
|
|
|
26
74
|
def dtable; @dtable ||= Dispatio.make_table(self, 'validate_'); end
|
|
75
|
+
# or
|
|
76
|
+
#def dtable; @dtable ||= Dispatio.make_table(self, prefix: 'validate_'); end
|
|
27
77
|
end
|
|
28
78
|
|
|
29
79
|
foo = Foo.new
|
|
@@ -31,19 +81,31 @@ foo = Foo.new
|
|
|
31
81
|
foo.consume(:foo, 'hello') # ==> [ Foo, :vfoo, 'hello' ]
|
|
32
82
|
foo.consume(:bar, 'world') # ==> [ Foo, :vbar, 'world' ]
|
|
33
83
|
|
|
34
|
-
foo.consume(
|
|
84
|
+
foo.consume('foo', 'hello') # ==> [ Foo, :vfoo, 'hello' ]
|
|
85
|
+
foo.consume('bar', 'world') # ==> [ Foo, :vbar, 'world' ]
|
|
86
|
+
|
|
87
|
+
foo.consume(:nada, 'nemo') # ==> NoMethodError, 'no :validate_nada method'
|
|
35
88
|
```
|
|
36
89
|
|
|
37
90
|
## against a singleton class
|
|
38
91
|
|
|
39
92
|
```ruby
|
|
93
|
+
require 'dispatio'
|
|
94
|
+
|
|
40
95
|
class Bar
|
|
41
96
|
|
|
42
97
|
class << self
|
|
43
98
|
|
|
44
99
|
def consume(name, data)
|
|
45
100
|
|
|
46
|
-
dtable.dispatch(name, data)
|
|
101
|
+
dtable.dispatch(name, data) # or
|
|
102
|
+
#dtable.call(name, data) #
|
|
103
|
+
#dtable.(name, data) #
|
|
104
|
+
#dtable[name].call(data) # or
|
|
105
|
+
#dtable[name].(data) #
|
|
106
|
+
#dtable[name][data] #
|
|
107
|
+
#
|
|
108
|
+
# just use the one you like
|
|
47
109
|
end
|
|
48
110
|
|
|
49
111
|
protected
|
|
@@ -57,12 +119,17 @@ class Bar
|
|
|
57
119
|
end
|
|
58
120
|
|
|
59
121
|
def dtable; @dtable ||= Dispatio.make_table(self, 'validate_'); end
|
|
122
|
+
# or
|
|
123
|
+
#def dtable; @dtable ||= Dispatio.make_table(self, prefix: 'validate_'); end
|
|
60
124
|
end
|
|
61
125
|
end
|
|
62
126
|
|
|
63
127
|
Bar.consume(:foo, 'hello') # ==> [ Bar, :vfoo, 'hello' ]
|
|
64
128
|
Bar.consume(:bar, 'world') # ==> [ Bar, :vbar, 'world' ]
|
|
65
129
|
|
|
130
|
+
Bar.consume('foo', 'hello') # ==> [ Bar, :vfoo, 'hello' ]
|
|
131
|
+
Bar.consume('bar', 'world') # ==> [ Bar, :vbar, 'world' ]
|
|
132
|
+
|
|
66
133
|
Bar.consume(:nada, 'nemo') # ==> NoMethodError, 'no :validate_nada method'
|
|
67
134
|
```
|
|
68
135
|
|
data/lib/dispatio.rb
CHANGED
|
@@ -1,20 +1,38 @@
|
|
|
1
1
|
|
|
2
2
|
module Dispatio
|
|
3
3
|
|
|
4
|
-
VERSION = '1.
|
|
4
|
+
VERSION = '1.2.0'.freeze
|
|
5
5
|
|
|
6
6
|
class << self
|
|
7
7
|
|
|
8
|
-
def make_table(point,
|
|
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
|
-
|
|
27
|
+
opts,
|
|
12
28
|
point
|
|
13
29
|
.methods
|
|
14
|
-
.select { |m|
|
|
15
|
-
m.to_s.start_with?(prefix) }
|
|
16
30
|
.inject({}) { |h, m|
|
|
17
|
-
|
|
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,18 +40,31 @@ module Dispatio
|
|
|
22
40
|
|
|
23
41
|
class Table
|
|
24
42
|
|
|
25
|
-
def initialize(
|
|
43
|
+
def initialize(opts, table)
|
|
26
44
|
|
|
27
|
-
@
|
|
45
|
+
@opts = opts
|
|
28
46
|
@table = table.freeze
|
|
29
47
|
end
|
|
30
48
|
|
|
31
|
-
def
|
|
49
|
+
def [](name)
|
|
50
|
+
|
|
51
|
+
@table[name.to_s]
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def call(name, *args, **opts, &block)
|
|
55
|
+
|
|
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
|
|
32
64
|
|
|
33
|
-
(
|
|
34
|
-
fail(NoMethodError.new("no :#{@prefix}#{name} method"))
|
|
35
|
-
).call(*args, **opts, &block)
|
|
65
|
+
m.call(*args, **opts, &block)
|
|
36
66
|
end
|
|
67
|
+
alias dispatch call
|
|
37
68
|
end
|
|
38
69
|
end
|
|
39
70
|
|