Sha256: 8a5ebee9ceed4a6a9ff970a1dde75b685c2ab543edae6565c466fecbfdd7e787

Contents?: true

Size: 1.82 KB

Versions: 6

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

module Wayfarer
  module Routing
    module DSL
      def url(url, options = {}, &block)
        add_child_route(Matchers::URL.new(url), path_offset, options, &block)
      end

      def host(host, options = {}, &block)
        add_child_route(Matchers::Host.new(host), path_offset, options, &block)
      end

      def path(path, options = {}, &block)
        offset = File.join(path_offset, path)
        add_child_route(nil, offset, options, &block).tap do |route|
          route.matcher = Matchers::Path.new(offset, route)
        end
      end

      def query(fields, options = {}, &block)
        add_child_route(Matchers::Query.new(fields), path_offset, options, &block)
      end

      def scheme(scheme, options = {}, &block)
        add_child_route(Matchers::Scheme.new(scheme), path_offset, options, &block)
      end

      def suffix(suffix, options = {}, &block)
        add_child_route(Matchers::Suffix.new(suffix), path_offset, options, &block)
      end

      def to(action, options = {}, &block)
        add_child_route(Matchers::Custom.new { true }, path_offset, TargetRoute, options, &block).tap do |route|
          route.action = action
        end
      end

      def custom(delegate, options = {}, &block)
        add_child_route(Matchers::Custom.new(delegate), path_offset, options, &block)
      end

    private

      # rubocop:disable Style/OptionalArguments
      def add_child_route(matcher, path_offset, klass = Route, options, &block)
        klass.new(matcher, path_offset, &block).tap do |route|
          route.parent = self
          leaf = options.reduce(route) { |acc, (key, val)| acc.public_send(key, val) }
          children.push(route)
          Docile.dsl_eval(leaf, &block) if block_given?
        end
      end
      # rubocop:enable Style/OptionalArguments
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
wayfarer-0.4.6 lib/wayfarer/routing/dsl.rb
wayfarer-0.4.5 lib/wayfarer/routing/dsl.rb
wayfarer-0.4.4 lib/wayfarer/routing/dsl.rb
wayfarer-0.4.3 lib/wayfarer/routing/dsl.rb
wayfarer-0.4.2 lib/wayfarer/routing/dsl.rb
wayfarer-0.4.1 lib/wayfarer/routing/dsl.rb