Sha256: 6beba6598067c38b4dddcac8b5044eead9e7ac5b1ba47ae3e8545a8fb9967f5c

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 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, options, &block)
        parent = options.delete(:parent) || @root
        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, options|
          parent = options.delete(:parent)
          child = Node::Operation.new(name, options.merge(action: action))
          parent.append(child)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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