Last active
July 25, 2017 12:23
-
-
Save Groogy/694dd0085b1118ec43b31f768095e71a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Glue | |
abstract def interested?(input) : Bool | |
abstract def execute(input) : Nil | |
end | |
class GlueImp(T, P) < Glue | |
def initialize(@bar : T, @callback : P) | |
end | |
def interested?(input) : Bool | |
@bar.interested? input | |
end | |
def execute(input) : Nil | |
args = @bar.translate(input) | |
@callback.call(*args) | |
end | |
end | |
struct SelectiveInput | |
def translate(input) | |
{25, "Hello " + input} | |
end | |
def interested?(input) | |
input.size > 3 | |
end | |
end | |
struct PassThrough | |
def translate(input) | |
{input} | |
end | |
def interested?(input) | |
true | |
end | |
end | |
class Router | |
@stored_uniform_callbacks = [] of Glue | |
def register(translator_type, proc) | |
@stored_uniform_callbacks << GlueImp.new(translator_type.new, proc) | |
end | |
def push(input) | |
@stored_uniform_callbacks.each do |glue| | |
glue.execute(input) if glue.interested? input | |
end | |
end | |
end | |
router = Router.new | |
router.register SelectiveInput, ->(a : Int32, b : String) { pp a, b; nil } | |
router.register PassThrough, ->(a : String) { puts a } | |
router.push("Foo") | |
router.push("Foobar") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment