summaryrefslogtreecommitdiff
path: root/lib/gitlab/graphql/connection_redaction.rb
blob: 5e037bb9f6394fdba4aadc044015437eae01ab3e (plain)
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
# frozen_string_literal: true

module Gitlab
  module Graphql
    module ConnectionRedaction
      class RedactionState
        attr_reader :redactor
        attr_reader :redacted_nodes

        def redactor=(redactor)
          @redactor = redactor
          @redacted_nodes = nil
        end

        def redacted(&block)
          @redacted_nodes ||= redactor.present? ? redactor.redact(yield) : yield
        end
      end

      delegate :redactor=, to: :redaction_state

      def nodes
        redaction_state.redacted { super.to_a }
      end

      private

      def redaction_state
        @redaction_state ||= RedactionState.new
      end
    end
  end
end