• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ruby-grape / grape / 13501493749

24 Feb 2025 03:21PM UTC coverage: 98.389% (+0.02%) from 98.367%
13501493749

Pull #2538

github

web-flow
Merge ab35c7e0b into 6e6958f35
Pull Request #2538: Handle json array

7 of 7 new or added lines in 2 files covered. (100.0%)

4 existing lines in 3 files now uncovered.

3542 of 3600 relevant lines covered (98.39%)

76444.42 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.49
/lib/grape/middleware/stack.rb
1
# frozen_string_literal: true
2

3
module Grape
60✔
4
  module Middleware
60✔
5
    # Class to handle the stack of middlewares based on ActionDispatch::MiddlewareStack
6
    # It allows to insert and insert after
7
    class Stack
60✔
8
      extend Forwardable
60✔
9
      class Middleware
60✔
10
        extend Forwardable
60✔
11

12
        attr_reader :args, :block, :klass
60✔
13

14
        def_delegators :klass, :name
60✔
15

16
        def initialize(klass, *args, &block)
60✔
17
          @klass = klass
206,003✔
18
          @args = args
206,003✔
19
          @block = block
206,003✔
20
        end
21

22
        def ==(other)
60✔
23
          case other
1,813✔
24
          when Middleware
UNCOV
25
            klass == other.klass
×
26
          when Class
27
            klass == other || (name.nil? && klass.superclass == other)
1,813✔
28
          end
29
        end
30

31
        def inspect
60✔
UNCOV
32
          klass.to_s
×
33
        end
34

35
        def use_in(builder)
60✔
36
          builder.use(klass, *args, &block)
204,680✔
37
        end
38
      end
39

40
      include Enumerable
60✔
41

42
      attr_accessor :middlewares, :others
60✔
43

44
      def_delegators :middlewares, :each, :size, :last, :[]
60✔
45

46
      def initialize
60✔
47
        @middlewares = []
66,201✔
48
        @others = []
66,201✔
49
      end
50

51
      def insert(index, *args, &block)
60✔
52
        index = assert_index(index, :before)
588✔
53
        middleware = self.class::Middleware.new(*args, &block)
539✔
54
        middlewares.insert(index, middleware)
539✔
55
      end
56
      ruby2_keywords :insert if respond_to?(:ruby2_keywords, true)
60✔
57

58
      alias insert_before insert
60✔
59

60
      def insert_after(index, *args, &block)
60✔
61
        index = assert_index(index, :after)
245✔
62
        insert(index + 1, *args, &block)
196✔
63
      end
64
      ruby2_keywords :insert_after if respond_to?(:ruby2_keywords, true)
60✔
65

66
      def use(...)
60✔
67
        middleware = self.class::Middleware.new(...)
205,464✔
68
        middlewares.push(middleware)
205,464✔
69
      end
70

71
      def merge_with(middleware_specs)
60✔
72
        middleware_specs.each do |operation, *args|
131,079✔
73
          if args.last.is_a?(Proc)
1,471✔
74
            last_proc = args.pop
196✔
75
            public_send(operation, *args, &last_proc)
196✔
76
          else
77
            public_send(operation, *args)
1,275✔
78
          end
79
        end
80
      end
81

82
      # @return [Rack::Builder] the builder object with our middlewares applied
83
      def build(builder = Rack::Builder.new)
60✔
84
        others.shift(others.size).each { |m| merge_with(m) }
130,981✔
85
        middlewares.each do |m|
65,515✔
86
          m.use_in(builder)
204,680✔
87
        end
88
        builder
65,515✔
89
      end
90

91
      # @description Add middlewares with :use operation to the stack. Store others with :insert_* operation for later
92
      # @param [Array] other_specs An array of middleware specifications (e.g. [[:use, klass], [:insert_before, *args]])
93
      def concat(other_specs)
60✔
94
        @others << Array(other_specs).reject { |o| o.first == :use }
67,035✔
95
        merge_with(Array(other_specs).select { |o| o.first == :use })
67,035✔
96
      end
97

98
      protected
60✔
99

100
      def assert_index(index, where)
60✔
101
        i = index.is_a?(Integer) ? index : middlewares.index(index)
833✔
102
        i || raise("No such middleware to insert #{where}: #{index.inspect}")
833✔
103
      end
104
    end
105
  end
106
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc