Sha256: f06e4474fce1cc94850770315a35be818a6c6ccfa6fb56fc35b86320118488ed

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

require "message_router/version"

class MessageRouter

  autoload :Context,  'message_router/context'
  autoload :Matcher,  'message_router/matcher'
  autoload :Mount,    'message_router/mount'

  class << self
    def match(*args, &block)
      route Matcher.new(*args, &block)
    end

    def context(proc, &block)
      route Context.new(proc, &block)
    end

    def mount(mounted_router_klass)
      route Mount.new(mounted_router_klass)
    end

    def routes
      @routes ||= []
    end

    def route(proc)
      routes.push proc
    end

    def dispatch(message)
      new(message).dispatch
    end
  end

  attr_accessor :message, :halted_value

  def initialize(message)
    @message = message
  end

  def halt(val=nil)
    @halted = true
    @halted_value = val
  end

  def halted?
    !!@halted
  end

  # Iterate through all of the matchers, find the first one, and call the block on it.
  def dispatch
    self.class.routes.each do |route|
      # Break out of the loop if a match is found
      if match = route.call(self)
        return match
      elsif halted?
        return halted_value
      end
    end
    return nil # If nothing is matched, we get here and we should return a nil
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
message_router-0.0.1 lib/message_router.rb