Sha256: e2b5e28010e645cbc42350eda495c0fcd0dfef63dc37be809323608b21f4462f

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

require "hanami/slice/router"

module Hanami
  # App routes
  #
  # Users are expected to inherit from this class to define their app
  # routes.
  #
  # @example
  #   # config/routes.rb
  #   # frozen_string_literal: true
  #
  #   require "hanami/routes"
  #
  #   module MyApp
  #     class Routes < Hanami::Routes
  #       root to: "home.show"
  #     end
  #   end
  #
  #   See {Hanami::Slice::Router} for the syntax allowed within the `define` block.
  #
  # @see Hanami::Slice::Router
  # @since 2.0.0
  class Routes
    # @api private
    def self.routes
      @routes ||= build_routes
    end

    class << self
      # @api private
      def build_routes(definitions = self.definitions)
        return if definitions.empty?

        proc do
          definitions.each do |(name, args, kwargs, block)|
            if block
              public_send(name, *args, **kwargs, &block)
            else
              public_send(name, *args, **kwargs)
            end
          end
        end
      end

      # @api private
      def definitions
        @definitions ||= []
      end

      private

      # @api private
      def supported_methods
        @supported_methods ||= Slice::Router.public_instance_methods
      end

      # @api private
      def respond_to_missing?(name, include_private = false)
        supported_methods.include?(name) || super
      end

      # Capture all method calls that are supported by the router DSL
      # so that it can be evaluated lazily during configuration/boot
      # process
      #
      # @api private
      def method_missing(name, *args, **kwargs, &block)
        return super unless respond_to?(name)
        definitions << [name, args, kwargs, block]
        self
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hanami-2.0.0.beta4 lib/hanami/routes.rb
hanami-2.0.0.beta3 lib/hanami/routes.rb