Class: MVCLI::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/mvcli/router.rb,
lib/mvcli/router/pattern.rb

Defined Under Namespace

Classes: Pattern, Route

Constant Summary collapse

RoutingError =
Class.new StandardError
InvalidRoute =
Class.new RoutingError

Instance Method Summary collapse

Constructor Details

#initialize(actions = nil) ⇒ Router

Returns a new instance of Router.



10
11
12
13
# File 'lib/mvcli/router.rb', line 10

def initialize(actions = nil)
  @actions = actions || Map.new
  @routes = []
end

Instance Method Details

#call(command) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/mvcli/router.rb', line 21

def call(command)
  @routes.each do |route|
    if match = route.match(command)
      return match.call command
    end
  end
  fail RoutingError, "no route matches '#{command.argv.join ' '}'"
end

#match(options) ⇒ Object



15
16
17
18
19
# File 'lib/mvcli/router.rb', line 15

def match(options)
  pattern, action = options.first
  options.delete pattern
  @routes << Route.new(pattern, @actions, action, options)
end