Sha256: 7d4cf92f27ee3c6d683a4ff979054b97e9ed3efe230d2b8d283e2a18ca81cfd6

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

module Apitizer
  module Routing
    class Map
      extend Forwardable

      def_delegator :@root, :define_address

      def initialize(&block)
        @root = Node::Root.new
        define(&block) if block_given?
      end

      def trace(action, *arguments)
        path = @root.trace(*arguments) or raise Error, 'Not found'
        raise Error, 'Not permitted' unless path.permit?(action)
        path
      end

      def define(&block)
        proxy = Proxy.new(self)
        proxy.instance_eval(&block)
      end

      def define_resources(name, parent: @root, **options, &block)
        child = Node::Collection.new(name, **options)
        parent.append(child)
        return unless block_given?
        proxy = Proxy.new(self, parent: child)
        proxy.instance_eval(&block)
      end

      Apitizer.actions.each do |action|
        define_method "define_#{ action }" do |name, parent:, **options|
          child = Node::Operation.new(name, action: action, **options)
          parent.append(child)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
apitizer-0.0.2 lib/apitizer/routing/map.rb