class Railbus::RouteSet

Constants

MAP_NAMES
Route

Example: get, /users/:id, ['/users/', {a: 'id'}], ['id'], user

Attributes

paths[R]

Public Class Methods

new(app, include, exclude) click to toggle source
# File lib/railbus/route_set.rb, line 19
def initialize(app, include, exclude)
  @paths = add_route_names(paths_for_app(app, include, exclude))
end

Private Instance Methods

add_route_names(paths) click to toggle source
# File lib/railbus/route_set.rb, line 89
def add_route_names(paths)
  paths.group_by(&:path).flat_map do |_, routes|
    name = routes.find(&:name)&.name
    if name
      routes.reject!(&:patch?) if routes.find(&:put?)
      routes.map do |route|
        route.name ||= "#{MAP_NAMES[route.verb]}#{route_name(route, name)}"
        route
      end
    else
      []
    end
  end.uniq
end
include_path?(path, include, exclude) click to toggle source
# File lib/railbus/route_set.rb, line 83
def include_path?(path, include, exclude)
  return false if !include.empty? && include.none? { |re| path =~ re }
  return false if exclude.any? { |re| path =~ re }
  true
end
join_parts(route_parts) click to toggle source
# File lib/railbus/route_set.rb, line 75
def join_parts(route_parts)
  route_parts.map { |x| x.respond_to?(:<<) ? x : ":#{x[:a]}" }.join
end
parts_combined(parts) click to toggle source
# File lib/railbus/route_set.rb, line 48
def parts_combined(parts)
  parts.reduce([]) do |a, pt|
    e = parts_element(pt)
    next a if e.empty?
    if a.empty?
      a = [e]
    elsif a[-1].respond_to?(:<<) && e.respond_to?(:<<)
      # Both vars are strings -> concat them.
      a[-1] << e
    else
      a << e
    end
    a
  end
end
parts_element(part) click to toggle source
# File lib/railbus/route_set.rb, line 64
def parts_element(part)
  if part.respond_to?(:name)
    # Is a path parameter (e.g. `:id`).
    {a: part.name}
  else
    # Is a `format` parameter -> skip it.
    # Otherwise it is a string.
    (part.respond_to?(:evaluate) ? '' : part).dup
  end
end
paths_for_app(app, include, exclude) click to toggle source
# File lib/railbus/route_set.rb, line 25
def paths_for_app(app, include, exclude)
  mounted_at = app.routes.find_script_name({})

  app.routes.set.routes.map do |route|
    formatter = route.instance_variable_get(:@path_formatter)
    raw_parts = formatter.instance_variable_get(:@parts)
    raw_parts.unshift(mounted_at)

    route_parts = parts_combined(raw_parts)

    path = join_parts(route_parts)
    next nil unless include_path?(path, include, exclude)

    Route.new(
      route.verb.downcase,
      path,
      route_parts,
      required_params(route_parts),
      route.name
    )
  end.compact
end
required_params(route_parts) click to toggle source
# File lib/railbus/route_set.rb, line 79
def required_params(route_parts)
  route_parts.reject { |x| x.respond_to?(:<<) }.map { |x| x[:a] }
end
route_name(route, base_name) click to toggle source
# File lib/railbus/route_set.rb, line 104
def route_name(route, base_name)
  if route.post?
    route_name_for_create(base_name)
  else
    base_name
  end
end
route_name_for_create(base_name) click to toggle source
# File lib/railbus/route_set.rb, line 112
def route_name_for_create(base_name)
  if base_name.end_with?('_index')
    base_name.sub(/_index$/, '')
  else
    base_name.singularize
  end
end