Class: MVCLI::Router

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

Defined Under Namespace

Classes: Match, 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.



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

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

Instance Method Details

#call(command) ⇒ Object



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

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



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

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