Class: Rory::Route

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mask, options = {}) ⇒ Route

Returns a new instance of Route.



5
6
7
8
9
# File 'lib/rory/route.rb', line 5

def initialize(mask, options = {})
  @mask = mask.gsub(/^\//, '')
  @options = options
  @controller, @action = options[:to].split('#')
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



3
4
5
# File 'lib/rory/route.rb', line 3

def action
  @action
end

#controllerObject (readonly)

Returns the value of attribute controller.



3
4
5
# File 'lib/rory/route.rb', line 3

def controller
  @controller
end

#maskObject (readonly)

Returns the value of attribute mask.



3
4
5
# File 'lib/rory/route.rb', line 3

def mask
  @mask
end

Instance Method Details

#==(other) ⇒ Object



15
16
17
# File 'lib/rory/route.rb', line 15

def ==(other)
  to_h == other.to_h
end

#matches_request?(path, method) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
# File 'lib/rory/route.rb', line 31

def matches_request?(path, method)
  @match = regex.match(path)
  @match &&
    (methods.empty? ||
      methods.include?(method.to_sym))
end

#methodsObject



27
28
29
# File 'lib/rory/route.rb', line 27

def methods
  @options[:methods] || []
end

#moduleObject



23
24
25
# File 'lib/rory/route.rb', line 23

def module
  @options[:module]
end

#nameObject



11
12
13
# File 'lib/rory/route.rb', line 11

def name
  "#{controller}_#{action}"
end

#path_params(path) ⇒ Object



38
39
40
41
42
# File 'lib/rory/route.rb', line 38

def path_params(path)
  @match ||= regex.match(path)
  symbolized_param_names = @match.names.map { |name| name.to_sym }
  Hash[symbolized_param_names.zip(@match.captures)]
end

#regexObject



19
20
21
# File 'lib/rory/route.rb', line 19

def regex
  /^#{@mask.gsub(/:([\w_]+)/, "(?<\\1>\[\^\\\/\]+)")}$/
end

#to_hObject



44
45
46
47
48
49
50
51
52
# File 'lib/rory/route.rb', line 44

def to_h
  {
    :mask => @mask,
    :controller => @controller,
    :action => @action,
    :module => @module,
    :methods => @methods
  }
end