class Makanai::Router::Route

Constants

DYNAMIC_PREFIX

Attributes

method[R]
path[R]
process[R]
url_args[R]

Public Class Methods

new(path:, process:, method:) click to toggle source
# File lib/makanai/router.rb, line 41
def initialize(path:, process:, method:)
  @path = path
  @process = process
  @method = method
  @url_args = {}
end

Public Instance Methods

match?(path:, method:) click to toggle source
# File lib/makanai/router.rb, line 50
def match?(path:, method:)
  build_url_args(path: path)
  path_match?(path) && self.method == method
end

Private Instance Methods

build_url_args(path:) click to toggle source
# File lib/makanai/router.rb, line 71
def build_url_args(path:)
  request_path, route_path = [path, self.path].map(&->(array) { array.split('/') })
  dynamic_indx.each do |i|
    @url_args.merge!({ route_path[i].gsub(DYNAMIC_PREFIX, '') => request_path[i] })
  end
end
dynamic_indx() click to toggle source
# File lib/makanai/router.rb, line 57
def dynamic_indx
  @dynamic_indx ||= path.split('/').map.with_index do |val, i|
    i if val.include?(DYNAMIC_PREFIX)
  end.compact
end
path_match?(path) click to toggle source
# File lib/makanai/router.rb, line 63
def path_match?(path)
  request_path, route_path = [path, self.path].map(&->(array) { array.split('/') })
  request_path.each.with_index do |val, i|
    route_path[i] = val if dynamic_indx.include?(i)
  end
  route_path == request_path
end