Sha256: 565d9b9b98b680f1304ec0cf83530f01b08c04f473d17e178543b69f7c731084

Contents?: true

Size: 1022 Bytes

Versions: 1

Compression:

Stored size: 1022 Bytes

Contents

# frozen_string_literal: true

module Hanami
  module Routing
    class Resource
      # Helper class to calculate nested path
      #
      # @api private
      # @since 0.4.0
      class Nested
        # @api private
        # @since 0.4.0
        SEPARATOR = "/"

        # @api private
        # @since 0.4.0
        def initialize(resource_name, resource)
          @resource_name = resource_name.to_s.split(SEPARATOR)
          @resource      = resource
          @path          = []
          _calculate(@resource_name.dup, @resource)
        end

        # @api private
        # @since 0.4.0
        def to_path
          @path.reverse!.pop
          @resource_name.zip(@path).flatten.join
        end

        private

        # @api private
        # @since 0.4.0
        def _calculate(param_wildcard, resource = nil)
          return if resource.nil?

          @path << resource.wildcard_param(param_wildcard.pop)
          _calculate(param_wildcard, resource.parent)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hanami-router-2.0.0.alpha1 lib/hanami/routing/resource/nested.rb