Class: Kontrol::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/kontrol/route.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, pattern, options, block) ⇒ Route

Returns a new instance of Route.



6
7
8
9
10
11
12
13
# File 'lib/kontrol/route.rb', line 6

def initialize(name, pattern, options, block)
  @name = name
  @pattern = pattern
  @block = block
  @options = options || {}
  @format = pattern.gsub(/\(.*?\)/, '%s')
  @regexp = /^#{pattern}/
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



4
5
6
# File 'lib/kontrol/route.rb', line 4

def block
  @block
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/kontrol/route.rb', line 4

def name
  @name
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/kontrol/route.rb', line 4

def options
  @options
end

#patternObject

Returns the value of attribute pattern.



4
5
6
# File 'lib/kontrol/route.rb', line 4

def pattern
  @pattern
end

Instance Method Details

#generate(*args) ⇒ Object



22
23
24
25
26
# File 'lib/kontrol/route.rb', line 22

def generate(*args)
  @format % args.map { |arg|
    arg.respond_to?(:to_param) ? arg.to_param : arg.to_s
  }
end

#recognize(request) ⇒ Object



15
16
17
18
19
20
# File 'lib/kontrol/route.rb', line 15

def recognize(request)
  match = request.path_info.match(@regexp)
  valid = @options.all? { |key, val| request.send(key).match(val) }

  return match if match and valid
end