Sha256: 8052266ab616a964b593147e93335312eaa5ff47b123ed565cf31e71b6614a7b

Contents?: true

Size: 884 Bytes

Versions: 1

Compression:

Stored size: 884 Bytes

Contents

require "map"

module MVCLI
  class Router
    RoutingError = Class.new StandardError
    InvalidRoute = Class.new RoutingError

    def initialize(actions = nil)
      @actions = actions || Map.new
      @root = Map.new
    end

    def root(options = {})
      action = options[:to] or fail InvalidRoute, "root routes must specify an action with ':to =>' E.g. root :to => 'foo#bar'"
      verbs = [options[:via] || :help].flatten
      verbs.each do |verb|
        @root[verb] = action
      end
    end

    def call(command)
      verb = command.argv.first || 'help'
      path = command.argv.slice(1..-1) || []
      if path.empty?
        if action_name = @root[verb]
          if action = @actions[action_name]
            return action.call command
          end
        end
      end
      fail RoutingError, "#{path.join(':')} does not respond to #{verb}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mvcli-0.0.1 lib/mvcli/router.rb