Class: DeprecationCollector::Web::Router::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/deprecation_collector/web/router.rb

Overview

:nodoc:

Constant Summary collapse

NAMED_SEGMENTS_PATTERN =
%r{/([^/]*):([^.:$/]+)}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_method, pattern, block) ⇒ Route

Returns a new instance of Route.



83
84
85
86
87
# File 'lib/deprecation_collector/web/router.rb', line 83

def initialize(request_method, pattern, block)
  @request_method = request_method
  @pattern = pattern
  @block = block
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



79
80
81
# File 'lib/deprecation_collector/web/router.rb', line 79

def block
  @block
end

#patternObject

Returns the value of attribute pattern.



79
80
81
# File 'lib/deprecation_collector/web/router.rb', line 79

def pattern
  @pattern
end

#request_methodObject

Returns the value of attribute request_method.



79
80
81
# File 'lib/deprecation_collector/web/router.rb', line 79

def request_method
  @request_method
end

Instance Method Details

#compile_matcherObject



93
94
95
96
97
98
# File 'lib/deprecation_collector/web/router.rb', line 93

def compile_matcher
  return pattern unless pattern.match?(NAMED_SEGMENTS_PATTERN)

  regex_pattern = pattern.gsub(NAMED_SEGMENTS_PATTERN, '/\1(?<\2>[^$/]+)') # /some/:id => /some/(?<id>[^$/]+)
  Regexp.new("\\A#{regex_pattern}\\Z")
end

#match(request_method, path) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/deprecation_collector/web/router.rb', line 100

def match(request_method, path)
  # this is actually not needed because of groupping by method, but just in case:
  return unless self.request_method == request_method

  case matcher
  when String
    {} if path == matcher
  else
    path_match = path.match(matcher)
    path_match&.named_captures&.transform_keys(&:to_sym)
  end
end

#matcherObject



89
90
91
# File 'lib/deprecation_collector/web/router.rb', line 89

def matcher
  @matcher ||= compile_matcher
end