class Pakyow::Reflection::Builders::Endpoints

@api private

Public Instance Methods

build(endpoints) click to toggle source
# File lib/pakyow/reflection/builders/endpoints.rb, line 18
def build(endpoints)
  endpoints.each do |endpoint|
    define_endpoint(endpoint)
  end
end

Private Instance Methods

define_endpoint(endpoint) click to toggle source
# File lib/pakyow/reflection/builders/endpoints.rb, line 26
def define_endpoint(endpoint)
  controller = if within_resource?(endpoint.view_path)
    find_or_define_resource_for_scope_at_path(
      resource_source_at_path(endpoint.view_path),
      controller_path(endpoint.view_path),
      endpoint.type
    )
  else
    find_or_define_controller_at_path(controller_path(endpoint.view_path))
  end

  # TODO: Make this the responsibility of the helpers.
  #
  ensure_controller_has_helpers(controller)

  if controller.expansions.include?(:resource)
    endpoint_name = String.normalize_path(
      endpoint.view_path.gsub(String.collapse_path(controller.path_to_self), "")
    ).split("/", 2)[1]

    endpoint_name = if endpoint_name.empty?
      :list
    else
      endpoint_name.to_sym
    end

    case endpoint_name
    when :new, :edit, :list, :show
      # Find or define the route by resource endpoint name.
      #
      route = controller.routes.values.flatten.find { |possible_route|
        possible_route.name == endpoint_name
      } || controller.send(endpoint_name) do
        reflect
      end
    end
  end

  unless route
    # Find or define the route by path.
    #
    # TODO: This should look across all controllers, not just the current one. Look through endpoints?
    #
    route = controller.routes.values.flatten.find { |possible_route|
      possible_route.path == route_path(endpoint.view_path)
    } || controller.get(route_name(endpoint.view_path), route_path(endpoint.view_path)) do
      operations.reflect(controller: self)
    end
  end

  if route.name
    controller.action :set_reflected_endpoint, only: [route.name] do
      connection.set(:__reflected_endpoint, endpoint)
    end
  else
    # TODO: warn the user that a reflection couldn't be installed for an unnamed route
  end
end