Project

dispatio

0.0
The project is in a healthy, maintained state
A stupid dispatch table for convenience.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.6
 Project Readme

dispatio

Gem Version

A stupid dispatch table tool.

require 'dispatio'

class MyClass

  def consume(type, msg)

    (@ctable ||= Dispatio.make_table(self, 'consume_').call(type, msg)
      # or
    #(@ctable ||= Dispatio.make_table(self, prefix: 'consume_').call(type, msg)
  end

  def post(type, msg)

    (@ptable ||= Dispatio.make_table(self, '_post').call(type, msg)
      # or, more explicitely
    #(@ptable ||= Dispatio.make_table(self, suffix: '_post').call(type, msg)
  end

  protected

  def consume_this(msg)
    # ...
  end
  def consume_that(msg)
    # ...
  end
  def this_post(msg)
    # ...
  end
  def that_post(msg)
    # ...
  end
end

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.

against an instance

require 'dispatio'

class Foo

  def consume(name, data)

    dtable.dispatch(name, data) # or
    #dtable.call(name, data)    #
    #dtable.(name, data)        #
    #dtable[name].call(data) # or
    #dtable[name].(data)     #
    #dtable[name][data]      #
      #
      # just use the one you like
  end

  protected

  def validate_foo(data)
    [ self.class, :vfoo, data ]
  end

  def validate_bar(data)
    [ self.class, :vbar, data ]
  end

  def dtable; @dtable ||= Dispatio.make_table(self, 'validate_'); end
    # or
  #def dtable; @dtable ||= Dispatio.make_table(self, prefix: 'validate_'); end
end

foo = Foo.new

foo.consume(:foo, 'hello') # ==> [ Foo, :vfoo, 'hello' ]
foo.consume(:bar, 'world') # ==> [ Foo, :vbar, 'world' ]

foo.consume('foo', 'hello') # ==> [ Foo, :vfoo, 'hello' ]
foo.consume('bar', 'world') # ==> [ Foo, :vbar, 'world' ]

foo.consume(:nada, 'nemo') # ==> NoMethodError, 'no :validate_nada method'

against a singleton class

require 'dispatio'

class Bar

  class << self

    def consume(name, data)

      dtable.dispatch(name, data) # or
      #dtable.call(name, data)    #
      #dtable.(name, data)        #
      #dtable[name].call(data) # or
      #dtable[name].(data)     #
      #dtable[name][data]      #
        #
        # just use the one you like
    end

    protected

    def validate_foo(data)
      [ self, :vfoo, data ]
    end

    def validate_bar(data)
      [ self, :vbar, data ]
    end

    def dtable; @dtable ||= Dispatio.make_table(self, 'validate_'); end
      # or
    #def dtable; @dtable ||= Dispatio.make_table(self, prefix: 'validate_'); end
  end
end

Bar.consume(:foo, 'hello') # ==> [ Bar, :vfoo, 'hello' ]
Bar.consume(:bar, 'world') # ==> [ Bar, :vbar, 'world' ]

Bar.consume('foo', 'hello') # ==> [ Bar, :vfoo, 'hello' ]
Bar.consume('bar', 'world') # ==> [ Bar, :vbar, 'world' ]

Bar.consume(:nada, 'nemo') # ==> NoMethodError, 'no :validate_nada method'

LICENSE

MIT, see LICENSE.txt