Class: MVCLI::Router

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

Defined Under Namespace

Classes: DSL, Macro, Pattern, Route, RoutingError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



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

def initialize
  @routes = []
  @macros = []
end

Instance Attribute Details

#macrosObject (readonly)

Returns the value of attribute macros.



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

def macros
  @macros
end

#routesObject (readonly)

Returns the value of attribute routes.



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

def routes
  @routes
end

Instance Method Details

#call(command) ⇒ Object Also known as: []



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

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