-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathstack.rb
109 lines (90 loc) · 3.14 KB
/
stack.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# frozen_string_literal: true
module Grape
module Middleware
# Class to handle the stack of middlewares based on ActionDispatch::MiddlewareStack
# It allows to insert and insert after
class Stack
extend Forwardable
class Middleware
attr_reader :args, :block, :klass
def initialize(klass, args, block)
@klass = klass
@args = args
@block = block
end
def name
klass.name
end
def ==(other)
case other
when Middleware
klass == other.klass
when Class
klass == other || (name.nil? && klass.superclass == other)
end
end
def inspect
klass.to_s
end
def build(builder)
# we need to force the ruby2_keywords_hash for middlewares that initialize contains keywords
# like ActionDispatch::RequestId since middleware arguments are serialized
# https://rubyapi.org/3.4/o/hash#method-c-ruby2_keywords_hash
args[-1] = Hash.ruby2_keywords_hash(args[-1]) if args.last.is_a?(Hash) && Hash.respond_to?(:ruby2_keywords_hash)
builder.use(klass, *args, &block)
end
end
include Enumerable
attr_accessor :middlewares, :others
def_delegators :middlewares, :each, :size, :last, :[]
def initialize
@middlewares = []
@others = []
end
def insert(index, klass, *args, &block)
index = assert_index(index, :before)
middlewares.insert(index, self.class::Middleware.new(klass, args, block))
end
alias insert_before insert
def insert_after(index, *args, &block)
index = assert_index(index, :after)
insert(index + 1, *args, &block)
end
def use(klass, *args, &block)
middleware = self.class::Middleware.new(klass, args, block)
middlewares.push(middleware)
end
def merge_with(middleware_specs)
middleware_specs.each do |operation, klass, *args|
if args.last.is_a?(Proc)
last_proc = args.pop
public_send(operation, klass, *args, &last_proc)
else
public_send(operation, klass, *args)
end
end
end
# @return [Rack::Builder] the builder object with our middlewares applied
def build
Rack::Builder.new.tap do |builder|
others.shift(others.size).each { |m| merge_with(m) }
middlewares.each do |m|
m.build(builder)
end
end
end
# @description Add middlewares with :use operation to the stack. Store others with :insert_* operation for later
# @param [Array] other_specs An array of middleware specifications (e.g. [[:use, klass], [:insert_before, *args]])
def concat(other_specs)
use, not_use = other_specs.partition { |o| o.first == :use }
others << not_use
merge_with(use)
end
protected
def assert_index(index, where)
i = index.is_a?(Integer) ? index : middlewares.index(index)
i || raise("No such middleware to insert #{where}: #{index.inspect}")
end
end
end
end